×   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

9C82 Compare Float values

Submitted by Steve Fewell

Starting Address: &9C82 (or &9C65 to convert Integer first)
Entry criteria: FWA contains the value to compare.
BASIC Text pointer points to the expression to compare FWA with.
Exit: Zero flag = 1 if values are equal. Carry is set if value 1 > value 2; clear otherwise.
Y = 00.

Description:

9C65 Convert Integer to Float and initialise Float values:
This routine is called during Compare Integer values if the second value in the
compare is a Float value (not an Integer value, as expected).
Pop the IWA value from the processor stack (the first value to compare).
Push the FWA value (second value to compare) to the BASIC Stack.
Convert the IWA (first value to compare) to a Float value.
Copy this result from the FWA to the FWB.
Pop the Float from the BASIC Stack (second value to compare) and unpack the returned
variable (pointed to by &4A, &4B) to the FWA.
Now, FWA = second value to compare & FWB = first value to compare. Go to &9C92 to perform
the compare.

9C82 Initialise and get the Float values:
Push FWA to 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].
Copy A to Y [to update processor flags] and call &96DD to check that the result obtained
is a Float value - if it is an Integer it will be converted to Float. If it is a String then a
Type Mismatch error will be generated.
Pop the Float variable from the stack and unpack this variable to the FWB.
Now, the FWB = the first value to compare, and the FWA = second value to compare.

9C92 Compare the Float values:
Y = #&00
Reset bottom 7 bits of the FWB sign byte.
Load the FWA sign bit & reset the bottom 7 bits of the loaded value, then compare
this sign with the FWB sign byte. If the signs are not equal then exit (with C=1 if FWA>FWB).

Compare the FWB exponent with the FWA exponent (if not equal then stop as we have result).
Compare the FWB Mantissa byte 1 with the FWA Mantissa byte 1 (if not equal then stop as we have result).
Compare the FWB Mantissa byte 2 with the FWA Mantissa byte 2 (if not equal then stop as we have result).
Compare the FWB Mantissa byte 3 with the FWA Mantissa byte 3 (if not equal then stop as we have result).
Compare the FWB Mantissa byte 4 with the FWA Mantissa byte 4 (if not equal then stop as we have result).
If the Mantissa and exponents are equal, then the values are equal, so exit with A = 0.
Otherwise, C = 1 (if FWA > FWB). However, if both values are negative (Sign byte is 1), then
we need to reverse the carry, so that C = 0 (if it was 1), or C = 1 (if it was zero)
this is because the value that was higher is actually lower (as it is negative).
Exit with A = &01 (as values not equal).


Disassembly for the Compare Float values routine

9C65 h 104 68 PLA
9C66 * 133 042 85 2A STA &2A
9C68 h 104 68 PLA
9C69 + 133 043 85 2B STA &2B
9C6B h 104 68 PLA
9C6C , 133 044 85 2C STA &2C
9C6E h 104 68 PLA
9C6F - 133 045 85 2D STA &2D
9C71   032 250 187 20 FA BB JSR &BBFA Push FWA to Stack
9C74   032 133 129 20 85 81 JSR &8185 Convert Integer to Float
9C77   032 011 164 20 0B A4 JSR &A40B Copy FWA to FWB
9C7A   032 232 187 20 E8 BB JSR &BBE8 Pop Float from Stack
9C7D A 032 065 165 20 41 A5 JSR &A541 Unpack Float variable to FWA
9C80   128 016 80 10 BRA 16 --> &9C92
9C82   032 250 187 20 FA BB JSR &BBFA Push FWA to Stack
9C85 L 032 076 158 20 4C 9E JSR &9E4C Expression Handler (+, - level onwards)
9C88   168 A8 TAY
9C89   032 221 150 20 DD 96 JSR &96DD Check Float value (conv int to float, etc.)
9C8C   032 232 187 20 E8 BB JSR &BBE8 Pop Float from Stack (to &4A, &4B)
9C8F   032 224 164 20 E0 A4 JSR &A4E0 Unpack Float variable to FWB
9C92   160 000 A0 00 LDY#&00
9C94   169 127 A9 7F LDA#&7F
9C96 ; 020 059 14 3B TRB &3B
9C98 . 165 046 A5 2E LDA &2E
9C9A ) 041 128 29 80 AND#&80
9C9C ; 197 059 C5 3B CMP &3B
9C9E   208 030 D0 1E BNE 30 --> &9CBE
9CA0 < 165 060 A5 3C LDA &3C
9CA2 0 197 048 C5 30 CMP &30
9CA4   208 025 D0 19 BNE 25 --> &9CBF
9CA6 = 165 061 A5 3D LDA &3D
9CA8 1 197 049 C5 31 CMP &31
9CAA   208 019 D0 13 BNE 19 --> &9CBF
9CAC > 165 062 A5 3E LDA &3E
9CAE 2 197 050 C5 32 CMP &32
9CB0   208 013 D0 0D BNE 13 --> &9CBF
9CB2 ? 165 063 A5 3F LDA &3F
9CB4 3 197 051 C5 33 CMP &33
9CB6   208 007 D0 07 BNE 7 --> &9CBF
9CB8 @ 165 064 A5 40 LDA &40
9CBA 4 197 052 C5 34 CMP &34
9CBC   208 001 D0 01 BNE 1 --> &9CBF
9CBE ` 096 60 RTS
9CBF j 106 6A ROR A
9CC0 E; 069 059 45 3B EOR &3B
9CC2 * 042 2A ROL A
9CC3   169 001 A9 01 LDA#&01
9CC5 ` 096 60 RTS

 


 Back to 8BS
Or