8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

90AB Set String variable

Submitted by Steve Fewell

Routine:setstringvar
Name: Store string value in the specified Variable
Starting Address: &90AB
Entry criteria: The SWA contains the String value &
the variable address and type details are stored in the Integer value on the BASIC stack.
Exit: The specified variable has been set to the value in the SWA.

Description:

Pop the Integer from the BASIC Stack. Now &2A-&2B will contain the address of the variable's value,
and &2C will contain the variable's return type.

If the variable's return type is #&80 then we are not setting a variable, but a direct memory access
via a statement such as: LET $&0F55="HELLO"; so, as we are not dealing with a variable we can skip the
checking of the variable's existing length and go directly to storing the value.
To store the value of a non-variable String, we first store a carriage return as the next free position in the
SWA (as String values must terminate with a carriage return character). If SWA length (&36) is zero then
store the carriage return character at the Direct Memory address given and exit; otherwise, copy the SWA value
to the Direct Memory address (and subsequent bytes, as necessary) and exit.
Note: The SWA value is stored last character first, and first character last.

Otherwise we are dealing with a String variable (as the return type is probably #&81).
The String variable's address points to a 4-byte parameter block, this parameter
block contains the following: A 2-byte pointer to the current value of the String,
followed by the maximum size allocated for the String variable value (1 byte),
followed by the current size of the String value.

Compare the maximum length assigned for the String Variable value (the 3rd byte in the
variable's parameter block) with the length of the SWA (&36).
if the current Maximum length is >&eq; the SWA length then we do not need to allocate
more space for the variable, so [&910E] Store the SWA value in variable location.

Otherwise we need to allocate more space, as follows:
Store VARTOP address (the next free Variable storage address) in &2C-&2D, this
will be the location of the variable's new String value.
If the new String length is less than 8 and the new string length + 8 is less then #&FF
then set the new String length to #&FF. [This will resolve possible problems with String values
more than #&F7 characters long]. Otherwise set the new string length to &36 (SWA length).
Push the new String length to the Stack.

If the current VARTOP value is equal to the Variable value address (first 2 bytes of
variable's parameter block) plus the variable length (4th byte of the variable parameter
block) then the Variable's current value was the last item added to VARTOP, so
Store zero in location &2D (this will tell the allocate space routine not to
change the current Variable value address) and Subtract the old String length from the
new String length (in X), so that we only allocate the remaining number of bytes required!

Add the new string length (in X) to VARTOP address (store result in X & Y).
This will be the new VARTOP value, but first check whether this value would overlap with
the BASIC Stack, if it would then there is no more variable storage space, so issue a No Room
error.
Otherwise, update VARTOP (&02-&03) to the new VARTOP value (in X and Y).

Next, we need to set up the Variable's parameter block with the newly allocated information.
Retrieve the new variable length from the Stack.
Store the new variable length in the Max Size Allocated byte of the variable's parameter
block (the 3rd byte).
Check location &2D, if it is zero then we do not need to change the address of the variable's
value, as the last location (which was at the top of the BASIC variable Heap) has been extended.
Otherwise, store the new located (in &2C-&2D) as the address of the variable's value
in the first 2 bytes of the variable's parameter block.
Now, that we have the required space, continue to &910E to store the SWA value.

[&910E] Store the String SWA value in the variable location
Firstly, store the SWA length (&36) in byte 4 of the String variable's parameter block, this is the
1-byte which represents the current length of the String variable.
If the String is blank (SWA length is 0) then exit.

Store the variable's address (the first 2 bytes of the variable's parameter block)
in locations &2C-&2D.
Store the SWA value to the memory location pointed to by &2C-&2D. [Note: The SWA
value is stored first character first, last character last].
When we have stored all characters of the SWA's String value then exit, as the variable's
value is now complete.


Disassembly for the Set String variable routine

90AB   032 230 188 20 E6 BC JSR &BCE6 Pop Integer from BASIC Stack
90AE , 165 044 A5 2C LDA &2C
90B0   201 128 C9 80 CMP#&80
90B2 x 240 120 F0 78 BEQ 120 --> &912C Set String Variable to value
90B4   160 002 A0 02 LDY#&02
90B6 * 177 042 B1 2A LDA (&2A),Y
90B8 6 197 054 C5 36 CMP &36
90BA R 176 082 B0 52 BCS 82 --> &910E
90BC   165 002 A5 02 LDA &02
90BE , 133 044 85 2C STA &2C
90C0   165 003 A5 03 LDA &03
90C2 - 133 045 85 2D STA &2D
90C4 6 165 054 A5 36 LDA &36
90C6   201 008 C9 08 CMP#&08
90C8   144 006 90 06 BCC 6 --> &90D0
90CA i 105 007 69 07 ADC#&07
90CC   144 002 90 02 BCC 2 --> &90D0
90CE   169 255 A9 FF LDA#&FF
90D0   024 18 CLC
90D1 H 072 48 PHA
90D2   170 AA TAX
90D3 * 177 042 B1 2A LDA (&2A),Y
90D5 r* 114 042 72 2A ADC (&2A)
90D7 E 069 002 45 02 EOR &02
90D9   208 015 D0 0F BNE 15 --> &90EA
90DB   136 88 DEY
90DC q*E 113 042 71 2A ADC (&2A),Y
90DE   069 003 45 03 EOR &03
90E0   208 008 D0 08 BNE 8 --> &90EA
90E2 - 133 045 85 2D STA &2D
90E4   138 8A TXA
90E5   200 C8 INY
90E6 8 056 38 SEC
90E7 * 241 042 F1 2A SBC (&2A),Y
90E9   170 AA TAX
90EA   138 8A TXA
90EB   024 18 CLC
90EC e 101 002 65 02 ADC &02
90EE   168 A8 TAY
90EF   165 003 A5 03 LDA &03
90F1 i 105 000 69 00 ADC#&00
90F3   170 AA TAX
90F4   196 004 C4 04 CPY &04
90F6   229 005 E5 05 SBC &05
90F8   176 167 B0 A7 BCS -89 --> &90A1 No Room error
90FA   132 002 84 02 STY &02
90FC   134 003 86 03 STX &03
90FE h 104 68 PLA
90FF   160 002 A0 02 LDY#&02
9101 * 145 042 91 2A STA (&2A),Y
9103   136 88 DEY
9104 - 165 045 A5 2D LDA &2D
9106   240 006 F0 06 BEQ 6 --> &910E
9108 * 145 042 91 2A STA (&2A),Y
910A , 165 044 A5 2C LDA &2C
910C * 146 042 92 2A STA (&2A)
910E   160 003 A0 03 LDY#&03
9110 6 165 054 A5 36 LDA &36
9112 * 145 042 91 2A STA (&2A),Y
9114   240 021 F0 15 BEQ 21 --> &912B
9116   160 001 A0 01 LDY#&01
9118 * 177 042 B1 2A LDA (&2A),Y
911A - 133 045 85 2D STA &2D
911C * 178 042 B2 2A LDA (&2A)
911E , 133 044 85 2C STA &2C
9120   136 88 DEY
9121   185 000 006 B9 00 06 LDA &0600,Y
9124 , 145 044 91 2C STA (&2C),Y
9126   200 C8 INY
9127 6 196 054 C4 36 CPY &36
9129   208 246 D0 F6 BNE -10 --> &9121
912B ` 096 60 RTS
912C + 032 043 190 20 2B BE JSR &BE2B Store carriage return at end of SWA
912F   192 000 C0 00 CPY#&00
9131   240 011 F0 0B BEQ 11 --> &913E
9133   185 000 006 B9 00 06 LDA &0600,Y
9136 * 145 042 91 2A STA (&2A),Y
9138   136 88 DEY
9139   208 248 D0 F8 BNE -8 --> &9133
913B   173 000 006 AD 00 06 LDA &0600
913E * 146 042 92 2A STA (&2A)
9140 ` 096 60 RTS

Store #&0D (Carriage Return) at end of SWA and set (&37-&38) to point to the SWA
BE25 d7 100 055 64 37 STZ &37
BE27   169 006 A9 06 LDA#&06
BE29 8 133 056 85 38 STA &38
BE2B 6 164 054 A4 36 LDY &36
BE2D   169 013 A9 0D LDA#&0D
BE2F   153 000 006 99 00 06 STA &0600,Y
BE32 ` 096 60 RTS

 


 Back to 8BS
Or