×   Main Menu ALL The 8BS News Manuals (New menu) Links Worth a Look Tools Disc and Basic Webring Site Map 8BS Guestbook Old Guest Book Me The Barnsley Rovers   
8-Bit Software

The BBC and Master Computer Public Domain Library

B618 FOR

Submitted by Steve Fewell

Description:

Call routine &98AE to evaluate the variable name at the BASIC Text Pointer A location, and to create it if it doesn't
exist, and return the variable value's memory address in locations &2A-&2B and its type in &2C.
If the zero flag is set on return from &98AE, then the variable name is not a valid variable name, so issue
'FOR variable' error.
If the carry flag is set on return from &98AE, then the variable is a String variable, so issue 'FOR variable'
error, as a FOR variable must be numeric.

Call routine &BC43 to store the variable information data (&2A-&2C) to the 6502 stack.
Call routine &9B86 to check whether the next non-space character is '=' (equals), if not then issue 'Mistake' error;
otherwise, skip over the '=' character.

Call routine &B328 to evaluate the expression (after the '=' character), retrieve the variable information from the
6502 stack and set the Numeric FOR variable to the result of the expression.
If the next non-space character at BASIC Text Pointer B is not the 'TO-token', then issue 'No TO' error, as the TO-Keyword
must follow the expression giving the variable's start value.

Load Y with the current FOR level (location &26).
If the current FOR level is greater than or equal to #&96 (150) then issue 'Too many FORs' error, as only a maximum
of 10 FOR loops can be nested (each FOR loop uses 15 bytes on the FOR stack - and increases the FOR level by 15).

Add #&0F (15) to the current FOR level, and update location &26 with the new value.
Now, the &26 location has been updated, but Y still contains the original FOR level (before the addition).
Store the variable value address (&37-&38) in locations &0528 + Y and &0529 + Y; and store the
variable type (&39) in location &052A + Y.

If the variable type (&39) is Floating-Point (i.e. it is #&05) then:
* [&B6A1] Call routine &9D3B to evaluate the expression after the TO-Keyword
* Call routine &96DD to convert the result to a Floating-point value if it is Integer, or issue 'Type
    mismatch' error if it is a String value.
* Add #&21 (33) to the current FOR level (location &26) and store the result in location &4A.
* Store #&05 in location &4B
* Store the TO-value (the value in the FWA) in the 5-byte location pointed to by argp (&4A-&4B).
* Call routine &A5D8 to set the FWA to 1.0 (the default FOR STEP value)
* Get the next non-space character after the TO-expression (at the location pointed to by BASIC text
    pointer B)
* If the character is the 'STEP-token' then: Call routine &9D3B to evaluate the expression after the
    STEP-Keyword, Call &96DD to convert the result to Floating-Point (if it was an
    integer, or issue 'Type mismatch' error if it was a String) and Set Y to the BASIC
    text pointer B offset.
* [&B6C7] Update BASIC Text Pointer A to point to the end of the FOR-statement (by setting it to the
    value of Y (which is the current position on the Program Line)).
* Add #&1C to the current FOR level (location &26) and store the result in location &4A
* Set location &4B to #&05
* Call &A519 to store the STEP value (the value in the FWA) to the location pointed to by argp
    (&4A-&4B)
* Jump to &B68F to Check and skip the end of the program statement (issuing 'Syntax error' if the end of
    statement character (':','<cr>','ELSE') wasn't found), Store the current BASIC Text pointer A value
    to &0526 + Y - &0527 + Y (where Y is the FOR level - from location &26), Jump to &900B to
    continue executing the program - starting at the next program statement after the FOR-Statement.
Otherwise, the variable type (&39) must be Integer (i.e. it is #&04), so:
* [&B64D] Call routine &96AF to evaluate the expression after the TO-Keyword and convert the
    expression result to an Integer (if it was a Floating-Point value), or issue 'Type
    mismatch' error if the result was a String value.
* Set Y to the current FOR level (from location &26)
* Store the TO-value (in the IWA) in locations &0521 + Y to &0524 + Y (LSB first)
* Set the IWA to 1 (the default FOR STEP value)
* Get the next non-space character after the TO-expression (at the location pointed to by BASIC text
    pointer B)
* If the character is the 'STEP-token' then: Call routine &96AF to evaluate the expression after the
    TO value, and convert the result to Integer (if it was Floating-Point, or issue 'Type mismatch' error if
    the result was a String value), Set Y to the BASIC text pointer B offset value (&1B).
* [&B677] Update BASIC Text Pointer A to point to the end of the FOR-statement (by setting it to the
    value of Y (which is the current position on the Program Line)).
* Set Y to the current FOR level (from location &26).
* Store the STEP value (the current IWA value) to the FOR stack at starting at location &051C + Y (for the
    LSB byte - &2A) and ending at location &051F + Y (for the MSB byte - &2D).
* [&B68F] Check and skip the end of the program statement (issuing 'Syntax error' if the end of
    statement character (':','<cr>','ELSE') wasn't found) - also reset the BASIC text pointer A base
    value (&0B-&0C) to take into account the offset value (&0A).
* Store the current BASIC Text pointer A value to &0526 + Y and &0527 + Y (where Y is the FOR
    level - from location &26)
* Jump to &900B to continue executing the program - starting at the next program statement after the FOR-
    statement.
The memory layout of the FOR Stack (&0528-&05CB) is as follows:
The stack contains details for a maximum of 10 FOR loops. Each FOR block is 15 bytes long.
In each FOR block, the bytes have the following meaning:
Byte 1 (&0528) is the variable location - LSB.
Byte 2 (&0529) is the variable location - MSB
Byte 3 (&052A) is the variable type
Byte 4 (&052B) - Byte 8 (&052F) is the STEP value (Float (5-byte) / Integer (4-byte))
Byte 9 (&0530) - Byte 13 (&0534) is the TO value (Float (5-byte) / Integer (4-byte))
Byte 14 (&0535) is the program location of the start of the first statement within the FOR loop - LSB
Byte 15 (&0536) is the program location of the start of the first statement within the FOR loop - MSB


Disassembly for the FOR routine

B618   032 174 152 20 AE 98 JSR &98AE Evaluate variable name & create it if it's a new variable
B61B   240 219 F0 DB BEQ -37 --> &B5F8 'FOR variable' error
B61D   176 217 B0 D9 BCS -39 --> &B5F8 'FOR variable' error
B61F C 032 067 188 20 43 BC JSR &BC43 Push &2A, &2B & &2C (the variable info) to the 6502 Stack
B622   032 134 155 20 86 9B JSR &9B86 Check whether the next non-space character is '=' ('Mistake' error if not)
B625 ( 032 040 179 20 28 B3 JSR &B328 Evaluate expression and set Numeric variable
B628   032 213 142 20 D5 8E JSR &8ED5 Get next non-space character (PTR B)
B62B   201 184 C9 B8 CMP#&B8
B62D   208 226 D0 E2 BNE -30 --> &B611 'No TO' error
B62F & 164 038 A4 26 LDY &26
B631   192 150 C0 96 CPY#&96
B633   176 207 B0 CF BCS -49 --> &B604 'Too many FORs' error
B635   152 98 TYA
B636 i 105 015 69 0F ADC#&0F
B638 & 133 038 85 26 STA &26
B63A 7 165 055 A5 37 LDA &37
B63C ( 153 040 005 99 28 05 STA &0528,Y
B63F 8 165 056 A5 38 LDA &38
B641 ) 153 041 005 99 29 05 STA &0529,Y
B644 9 165 057 A5 39 LDA &39
B646 * 153 042 005 99 2A 05 STA &052A,Y
B649   201 005 C9 05 CMP#&05
B64B T 240 084 F0 54 BEQ 84 --> &B6A1
B64D   032 175 150 20 AF 96 JSR &96AF Get expression result & convert it to Integer
B650 & 164 038 A4 26 LDY &26
B652 * 165 042 A5 2A LDA &2A
B654 ! 153 033 005 99 21 05 STA &0521,Y
B657 + 165 043 A5 2B LDA &2B
B659 " 153 034 005 99 22 05 STA &0522,Y
B65C , 165 044 A5 2C LDA &2C
B65E # 153 035 005 99 23 05 STA &0523,Y
B661 - 165 045 A5 2D LDA &2D
B663 $ 153 036 005 99 24 05 STA &0524,Y
B666   169 001 A9 01 LDA#&01
B668   032 024 174 20 18 AE JSR &AE18 Set IWA to the 8-bit value in A
B66B   032 213 142 20 D5 8E JSR &8ED5 Get next non-space character (PTR B)
B66E   201 136 C9 88 CMP#&88
B670   208 005 D0 05 BNE 5 --> &B677
B672   032 175 150 20 AF 96 JSR &96AF Get expression result & convert it to Integer
B675   164 027 A4 1B LDY &1B
B677   132 010 84 0A STY &0A
B679 & 164 038 A4 26 LDY &26
B67B * 165 042 A5 2A LDA &2A
B67D   153 028 005 99 1C 05 STA &051C,Y
B680 + 165 043 A5 2B LDA &2B
B682   153 029 005 99 1D 05 STA &051D,Y
B685 , 165 044 A5 2C LDA &2C
B687   153 030 005 99 1E 05 STA &051E,Y
B68A - 165 045 A5 2D LDA &2D
B68C   153 031 005 99 1F 05 STA &051F,Y
B68F   032 207 155 20 CF 9B JSR &9BCF Check & skip end of program line
B692 & 164 038 A4 26 LDY &26
B694   165 011 A5 0B LDA &0B
B696 & 153 038 005 99 26 05 STA &0526,Y
B699   165 012 A5 0C LDA &0C
B69B ' 153 039 005 99 27 05 STA &0527,Y
B69E L 076 011 144 4C 0B 90 JMP &900B Process next BASIC program statement
B6A1 ; 032 059 157 20 3B 9D JSR &9D3B Evaluate expression at BASIC Text pointer B
B6A4   032 221 150 20 DD 96 JSR &96DD Check Float value (Convert if Integer, or 'Type mismatch' error if String)
B6A7 & 165 038 A5 26 LDA &26
B6A9   024 18 CLC
B6AA i! 105 033 69 21 ADC#&21
B6AC J 133 074 85 4A STA &4A
B6AE   169 005 A9 05 LDA#&05
B6B0 K 133 075 85 4B STA &4B
B6B2   032 025 165 20 19 A5 JSR &A519 Store FWA's value to argp address (&4A-&4B)
B6B5   032 216 165 20 D8 A5 JSR &A5D8 Set FWA to 1.0
B6B8   032 213 142 20 D5 8E JSR &8ED5 Get next non-space character (PTR B)
B6BB   201 136 C9 88 CMP#&88
B6BD   208 008 D0 08 BNE 8 --> &B6C7
B6BF ; 032 059 157 20 3B 9D JSR &9D3B Evaluate expression at BASIC Text pointer B
B6C2   032 221 150 20 DD 96 JSR &96DD Check Float value (Convert if Integer, or 'Type mismatch' error if String)
B6C5   164 027 A4 1B LDY &1B
B6C7   132 010 84 0A STY &0A
B6C9 & 165 038 A5 26 LDA &26
B6CB   024 18 CLC
B6CC i 105 028 69 1C ADC#&1C
B6CE J 133 074 85 4A STA &4A
B6D0   169 005 A9 05 LDA#&05
B6D2 K 133 075 85 4B STA &4B
B6D4   032 025 165 20 19 A5 JSR &A519 Store FWA's value to argp address (&4A-&4B)
B6D7   128 182 80 B6 BRA -74 --> &B68F

 


 Back to 8BS
Or