8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

B97D READ

Submitted by Steve Fewell

Description:

[&B975] Check for a comma, if found - jump to READ (&B97D) otherwise end of READ statement (jump to &9000)
Call routine &8CE5 to check that the next non-space character is a comma ',', if it is then jump to &B97D
to READ the new data value into the variable specified after the comma.
If the next non-space character was not a comma, then we have reached the end of the READ statement so jump to &9000 to
execute the next program statement.

[&B97D] READ statement

Call &98AE to evaluate the variable name after the READ keyword [or after the comma, if we have already
processed the first variable of the READ statement].
If the zero flag is set on return from &98AE then a valid variable name was not found; so, jump to &B975 to check
whether the next non-space character at PTR A is a comma, if it is then try reading a variable name after the comma;
otherwise, jump to &9000, as the end of the READ statement has been found. Note: &9000 will handle any Syntax
Error messages if the command at PTR A is not reconised.
If the carry flag is 1 then the variable is a String variable; so, jump to &B98F to process the String variable
Otherwise, the variable is numeric, so continue with &B984, to READ the numeric value.

[&B984] READ a value into a Numeric variable
Call &B9AC to set the BASIC Text Pointer B to point to the location before the start of the next DATA element.
Call &BC43 to push the variable address & type information (&2A, &2B and &2C) to the 6502 Stack.
Call &B328 to evaluate the numeric expression at BASIC Text Pointer B, retrieve the variable information from
the 6502 stack and set the variable's value to the result of the numeric expression, or issue 'Type mismatch' error
if a non-numeric (i.e. String) value was found.
Jump to &B99D to update the (&1C, &1D) DATA pointer and to continue processing the READ statement.

[&B98F] READ a value into a String variable
Call &B9AC to set the BASIC Text Pointer B to point to the location before the start of the next DATA element.
Call &BC26 to push the IWA value (which contains the Variable address and type information) to the BASIC stack.
Call &ACF8 to Extract the Next Field starting from the location pointed to by BASIC text pointer B, storing the
result in the SWA.
Store A (the result type) in location &27.
Call &90AB to retrieve the String variable details from the BASIC stack and set the String variable to the SWA value.
Continue to &B99D to update the (&1C, &1D) DATA pointer and to continue processing the READ statement.
Note: Because the Extract Next field routine (&ACF8) is used, a value of any type (Integer, Float or String) can
be READ into a String variable (i.e. This is fine: 10 READ A$    20 DATA 2.34).


[&B99D] Update the DATA Pointer and READ the next value (if any more are specified)
Add the BASIC Text Pointer B value (&19, &1A) to the DATA pointer (&1C, &1D).
Jump back to &B975 to check for a comma after the variable name in the READ Statement (BASIC text pointer A), if a
comma is found then process the READ statement for the next variable, otherwise end of READ statement found, so jump
to &9000 to start executing the next Statement in the Program.

[&B9AC] Move BASIC Text Pointer B to next DATA element location
Call &9275 to update the BASIC Text Pointer A offset to equal the BASIC Text Pointer B offset.
Set BASIC text pointer B to point to the value at locations &1C-&1D (this is either the last DATA element READ
or, if nothing has been ready yet, it will be the start of the BASIC program. Zero the BASIC text pointer B offset
Note: If (&1C-&1D) pointed to the start of the program, then the character pointed to will be the carriage return
('<cr>') character which indicates the start of the program.


Get the next non-space character at the location pointed to by BASIC Text Pointer B.
If the character is a comma then exit (RTS).
If the character is the 'DATA-token' (&DC) then exit (RTS).
If the character is a carriage return ('<cr>') then jump to &B9CF to move to the next line.

[&B9C6] Get the next non-space character at the location pointed to by BASIC Text Pointer B.
If the character is a comma then exit (RTS).
If the character is not '<cr>' then jump back to &B9C6 to check the next character.

[&B9CF] Check the next character pointed to by BASIC Text pointer B (plus offset - &1B), if it is negative
(i.e. #&FF) then the end of the program has been reached without finding the required data, so
issue 'Out of DATA' error; Otherwise (not negative), start processing the next program line by:
      * Incrementing Y (offset) by 2 (to skip the Line Number)
      * Setting X to the character at Text pointer B (plus offset) - this is the pointer to the next program line.
      * Incrementing Y (offset) by 1 (to skip the pointer to the next Program Line)
      * Skipping any spaces at the beginning of the Program Line
      * If the first non-space character on the program line is 'DATA-token' then jump to
         &BA13 to increment Y (offset), store Y back in &1B (PTR B
         offset) and exit.
      * Otherwise, Add X to BASIC Text Pointer B (&19, &1A) and junmp back to &B9CF to move to the next line


Thus, in other words, &B9AC simply looks for a Comma or DATA-Token at the current DATA pointer location
(&1C, &1D) and, if neither of these are found, then BASIC will skip the rest of the line until a comma is found,
or if a carriage return character is found (or a comma was not found on the current program line) then this routine will
look for a Program Line that begins with a DATA-token (as the first non-space character). 'Out of DATA' error is issued
if no DATA is found.

This format allows comments to be included inside the DATA statement! This is illustrated by the following program:
        10 READ A, B, C
        20 PRINT A, B, C
        30 DATA 2 (This is A) , 3 'This is B, 4 : This is C
This program correctly outputs 2         3          4
The best characters to use to separate the DATA element from the remark is '(' (open bracket), "'" (quote) or ':' (colon).
However, DATA statements will not be reconised if they do not begin at the start of the line (i.e. 30 : DATA 2, 3, 4)


Disassembly for the READ routine

B975   032 229 140 20 E5 8C JSR &8CE5 Compare next non-space [PTR A] character with ','
B978   240 003 F0 03 BEQ 3 --> &B97D READ
B97A L 076 000 144 4C 00 90 JMP &9000   Check end of statement & execute the next statement on the Command/Program line
B97D   032 174 152 20 AE 98 JSR &98AE Evaluate variable name & create it if it's a new variable
B980   240 243 F0 F3 BEQ -13 --> &B975 Check for a comma (if not found then end of READ statement)
B982   176 011 B0 0B BCS 11 --> &B98F Process String Variable
B984   032 172 185 20 AC B9 JSR &B9AC Set the BASIC Text Pointer B to point to the location before the start of the next DATA element
B987 C 032 067 188 20 43 BC JSR &BC43 Push &2A, &2B & &2C (the variable info) to the 6502 Stack
B98A ( 032 040 179 20 28 B3 JSR &B328 Evaluate expression and set Numeric variable
B98D   128 014 80 0E BRA 14 --> &B99D
B98F   032 172 185 20 AC B9 JSR &B9AC Set the BASIC Text Pointer B to point to the location before the start of the next DATA element
B992 & 032 038 188 20 26 BC JSR &BC26 Push IWA value (variable info) to the BASIC Stack [pushi]
B995   032 248 172 20 F8 AC JSR &ACF8 Extract next field (PTR B)
B998 ' 133 039 85 27 STA &27
B99A   032 171 144 20 AB 90 JSR &90AB Set String variable
B99D   024 18 CLC
B99E   165 027 A5 1B LDA &1B
B9A0 e 101 025 65 19 ADC &19
B9A2   133 028 85 1C STA &1C
B9A4   165 026 A5 1A LDA &1A
B9A6 i 105 000 69 00 ADC#&00
B9A8   133 029 85 1D STA &1D
B9AA   128 201 80 C9 BRA -55 --> &B975 Check for a comma (if not found then end of READ statement)
B9AC u 032 117 146 20 75 92 JSR &9275 Set PTR A Offset to PTR B Offset
B9AF   165 028 A5 1C LDA &1C
B9B1   133 025 85 19 STA &19
B9B3   165 029 A5 1D LDA &1D
B9B5   133 026 85 1A STA &1A
B9B7 d 100 027 64 1B STZ &1B
B9B9   032 235 142 20 EB 8E JSR &8EEB Get next non-space char (PTR B) & compare with ','
B9BC X 240 088 F0 58 BEQ 88 --> &BA16 RTS
B9BE   201 220 C9 DC CMP#&DC
B9C0 T 240 084 F0 54 BEQ 84 --> &BA16 RTS
B9C2   201 013 C9 0D CMP#&0D
B9C4   240 009 F0 09 BEQ 9 --> &B9CF
B9C6   032 235 142 20 EB 8E JSR &8EEB Get next non-space char (PTR B) & compare with ','
B9C9 K 240 075 F0 4B BEQ 75 --> &BA16 RTS
B9CB   201 013 C9 0D CMP#&0D
B9CD   208 247 D0 F7 BNE -9 --> &B9C6
B9CF   164 027 A4 1B LDY &1B
B9D1   177 025 B1 19 LDA (&19),Y
B9D3 0 048 028 30 1C BMI 28 --> &B9F1 Out of DATA error
B9D5   200 C8 INY
B9D6   200 C8 INY
B9D7   177 025 B1 19 LDA (&19),Y
B9D9   170 AA TAX
B9DA   200 C8 INY
B9DB   177 025 B1 19 LDA (&19),Y
B9DD   201 032 C9 20 CMP#&20
B9DF   240 249 F0 F9 BEQ -7 --> &B9DA
B9E1   201 220 C9 DC CMP#&DC
B9E3 . 240 046 F0 2E BEQ 46 --> &BA13
B9E5   138 8A TXA
B9E6   024 18 CLC
B9E7 e 101 025 65 19 ADC &19
B9E9   133 025 85 19 STA &19
B9EB   144 226 90 E2 BCC -30 --> &B9CF
B9ED   230 026 E6 1A INC &1A
B9EF   128 222 80 DE BRA -34 --> &B9CF

Disassembly for BA13 - Increment Y & Update Text Pointer Offset (&1B=Y)

BA13   200 C8 INY
BA14   132 027 84 1B STY &1B
BA16 ` 096 60 RTS

 


 Back to 8BS
Or