8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

9D02 Compare String values

Submitted by Steve Fewell

Starting Address: &9D02
Entry criteria: SWA contains the value to compare.
BASIC Text pointer points to the expression to compare SWA with.
Exit: Zero Flag = 1 if values are equal. Carry is set if value 1 > value 2; clear otherwise.
Y = 00.

Description:

Push the SWA to the Stack (first value to compare).
Gosub &9E4C to get the result of the expression (in the BASIC program text line),
[we need to process expression level (+,-) upwards as any operators below this level should
only be evaluated once this compare has been evaluated].
If the second value is not a string, then issue a Type Mismatch error.

Set &37 to the number of bytes to compare (the length of the shortest string).
If the number of bytes to compare is 0 then stop comparing [&9D23].
Keep comparing each character in both strings until we either encounter a difference
between the strings, in which case we have our result (C = 1 if first value > second value) [goto &9D27],
or until we have compared the number of bytes that we needed to compare (&37) [goto &9D23].

[9D23:]
Now we have compared the number of bytes we needed to compare, and the values we have
compared so far are equal.
Compare the length of the first value with the length of the second value.
Whichever string is the longer will be the greater. The Zero flag will be set if
the lengths are equal, and thus the strings are equal. (C = 1 if the first value length
is greater than the second value length).

[9D27:]
We have our result, so store result (PHP), restore stack space used by the string, then
restore the result value (PLP) and exit.


Disassembly for the Compare String values routine

9D02 Q 032 081 188 20 51 BC JSR &BC51 Push String to Stack
9D05 L 032 076 158 20 4C 9E JSR &9E4C Expression Handler [level (+, -) and above]
9D08   168 A8 TAY
9D09   208 187 D0 BB BNE -69 --> &9CC6 [JMP &9092]
9D0B   178 004 B2 04 LDA (&04)
9D0D 6 197 054 C5 36 CMP &36
9D0F   144 002 90 02 BCC 2 --> &9D13
9D11 6 165 054 A5 36 LDA &36
9D13 7 133 055 85 37 STA &37
9D15 7 196 055 C4 37 CPY &37
9D17   240 010 F0 0A BEQ 10 --> &9D23
9D19   200 C8 INY
9D1A   177 004 B1 04 LDA (&04),Y
9D1C   217 255 005 D9 FF 05 CMP &05FF,Y
9D1F   240 244 F0 F4 BEQ -12 --> &9D15
9D21   128 004 80 04 BRA 4 --> &9D27
9D23   178 004 B2 04 LDA (&04)
9D25 6 197 054 C5 36 CMP &36
9D27   008 08 PHP
9D28   032 225 188 20 E1 BC JSR &BCE1 Restore Stack Space used by String
9D2B   160 000 A0 00 LDY#&00
9D2D ( 040 28 PLP
9D2E ` 096 60 RTS

 


 Back to 8BS
Or