8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

BC6A Set Variable to value from BASIC Stack

Submitted by Steve Fewell

Description:

If the variable type (location &39) is #&80 then the variable is a String (which needs to be appended with a
terminating '<cr>' character); so:
* [&BC95] Load A with the last byte on the BASIC stack (&04-&05).
    This is the length of the String value. Store this value in X.
* If the length of the String is zero then store #&0D '&lt;cr&gt;' at the end of the String
    value and jump to &BCE1 to restore the stack space used by the String value (without overwriting the SWA).
* Set Y to 1
*   1) [&BC9C] Load the character at the BASIC stack location offset with Y
*   2) Decrement Y
*   3) Store the character loaded from the BASIC stack in the variable's value location (pointed to
          by &37-&38)
*   4) Increment Y by 2 - so that Y points to the next character to read from the BASIC Stack
*   5) Decrement X (the number of characters that we still need to process)
*   6) If X is not zero then jump back to step 1) to get the next character from the BASIC stack
* [&BCA6] Store #&0D '<cr>' at the end of the String value and jump to &BCE1 to restore
    the stack space used by the String value (without extracting it to the SWA).
If the variable type (location &39) is less than #&80 then the variable is an Integer (#&04), or a byte
(#&00), or Floating-Point (#&05); [i.e. Numeric], so:
* [&BCAA] Load the byte from the top of the BASIC stack (pointed to by &04-&05)
* Store the byte loaded from the BASIC stack to the variable's value location byte 1 (pointed to by
    &37-&38)
* Set Y to #&04 (as the length of the Integer will be 4 bytes)
* If the value in location &39 is zero then the value on the BASIC stack was 1-byte in length,
    so jump to &BCCE to set A to Y, clear the carry and jump to &BCFD to move the BASIC
    stack up A (i.e. 4) bytes [to ignore the other 3 bytes (which are zero)] and exit.
* Load the next byte from the BASIC Stack (pointed to by [&04-&05] + 1) & set Y to 1
* Store the byte in byte 2 of the variable's value location (pointed to by (&37-&38) + 1)
* Load the next byte from the BASIC Stack (pointed to by [&04-&05] + 2) & set Y to 2
* Store the byte in byte 3 of the variable's value location (pointed to by (&37-&38) + 2)
* Load the next byte from the BASIC Stack (pointed to by [&04-&05] + 3) & set Y to 3
* Store the byte in byte 4 of the variable's value location (pointed to by (&37-&38) + 3)
* Increment Y to 4
* Compare Y with the value at location &39 (i.e. the variable type, or the variable byte length)
* If Y is more than or equal to the value of location #&39, then jump to &BCCE to set A to Y,
    clear the carry and jump to &BCFD to move the BASIC Stack up A (i.e. 4) bytes
    and exit.
* Otherwise:
       1) Load the next byte from the BASIC Stack (pointed to by [&04-&05] + 4) & set Y to 4
       2) Store the byte in byte 5 of the variable's value location (pointed to by (&37-&38) + 4)
       3) Increment Y to 5
       4) [&BCCE] Set A to Y (5) and clear the carry flag
       5) Jump to &BCFD to move the BASIC Stack up A (i.e. 5) bytes
Otherwise, the variable type (location &39) is #&81 (String - without a '<cr>'), so:
* [&BC72] Set X to the Byte at the top of the BASIC Stack (pointed to by (&04-&05))
* If X is zero then [&BC8D] Set Y to #&03, Store the byte from the BASIC stack (#&00)
    in the location pointed to by (&37-&38) + Y and jump to &BCE1 to restore the stack space
    used by the String (Y bytes) without overwriting the SWA
* Load the first 2 bytes pointed to by (&37-&38) i.e. the String variable value's address and
    subtract 1 from the variable's address.
* [&BC83] Store the String variable value's address [minus 1] in locations &39-&3A
* Set Y to 1
* [&BC85] Keep on loading the next byte from the BASIC stack (offset Y), storing it at the address
    of the variable's value (&39-&3A, offset Y), incrementing Y and decrementing X
    until X has reached zero (i.e. we have copied the entire String value from the BASIC stack to the String
    variable's value - thus replacing the String variable's current value with its original value).
* [&BC8D] Load A with the first byte from the BASIC stack (the length of the String variable on the
    Stack) and store the String length byte as the String variable's length (i.e. the address pointed to by
    &37-&38 - plus an offset value of 3).
* Jump to &BCE1 to restore the Stack Space used by the String (i.e. the number of bytes specified in A)
* Note: As we are restoring the String variable's original value - there is no need to check whether the
    String variable has enough space allocated to hold the String value.


Disassembly for the Set Variable to value from BASIC Stack routine

BC6A 9 165 057 A5 39 LDA &39
BC6C   201 128 C9 80 CMP#&80
BC6E % 240 037 F0 25 BEQ 37 --> &BC95
BC70 8 144 056 90 38 BCC 56 --> &BCAA
BC72   178 004 B2 04 LDA (&04)
BC74   170 AA TAX
BC75   240 022 F0 16 BEQ 22 --> &BC8D
BC77 7 178 055 B2 37 LDA (&37)
BC79   233 001 E9 01 SBC#&01
BC7B 9 133 057 85 39 STA &39
BC7D   160 001 A0 01 LDY#&01
BC7F 7 177 055 B1 37 LDA (&37),Y
BC81   233 000 E9 00 SBC#&00
BC83 : 133 058 85 3A STA &3A
BC85   177 004 B1 04 LDA (&04),Y
BC87 9 145 057 91 39 STA (&39),Y
BC89   200 C8 INY
BC8A   202 CA DEX
BC8B   208 248 D0 F8 BNE -8 --> &BC85
BC8D   178 004 B2 04 LDA (&04)
BC8F   160 003 A0 03 LDY#&03
BC91 7 145 055 91 37 STA (&37),Y
BC93 L 128 076 80 4C BRA 76 --> &BCE1 Restore Stack Space used by String (without overwriting the SWA)
BC95   178 004 B2 04 LDA (&04)
BC97   170 AA TAX
BC98   240 012 F0 0C BEQ 12 --> &BCA6
BC9A   160 001 A0 01 LDY#&01
BC9C   177 004 B1 04 LDA (&04),Y
BC9E   136 88 DEY
BC9F 7 145 055 91 37 STA (&37),Y
BCA1   200 C8 INY
BCA2   200 C8 INY
BCA3   202 CA DEX
BCA4   208 246 D0 F6 BNE -10 --> &BC9C
BCA6   169 013 A9 0D LDA#&0D
BCA8   208 231 D0 E7 BNE -25 --> &BC91
BCAA   178 004 B2 04 LDA (&04)
BCAC 7 146 055 92 37 STA (&37)
BCAE   160 004 A0 04 LDY#&04
BCB0 9 165 057 A5 39 LDA &39
BCB2   240 026 F0 1A BEQ 26 --> &BCCE
BCB4   160 001 A0 01 LDY#&01
BCB6   177 004 B1 04 LDA (&04),Y
BCB8 7 145 055 91 37 STA (&37),Y
BCBA   200 C8 INY
BCBB   177 004 B1 04 LDA (&04),Y
BCBD 7 145 055 91 37 STA (&37),Y
BCBF   200 C8 INY
BCC0   177 004 B1 04 LDA (&04),Y
BCC2 7 145 055 91 37 STA (&37),Y
BCC4   200 C8 INY
BCC5 9 196 057 C4 39 CPY &39
BCC7   176 005 B0 05 BCS 5 --> &BCCE
BCC9   177 004 B1 04 LDA (&04),Y
BCCB 7 145 055 91 37 STA (&37),Y
BCCD   200 C8 INY
BCCE   152 98 TYA
BCCF   024 18 CLC
BCD0 + 128 043 80 2B BRA 43 --> &BCFD Move BASIC Stack pointer up A (+ carry) bytes (End of Pop Integer routine)

 


 Back to 8BS
Or