8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

8368 Floating-Point Addition

Submitted by Steve Fewell

Routine:aplus1
Name: Floating-Point Addition (FWA=FWA+FWB Normalised & Unrounded)
Starting Address: &8368
Entry criteria: The FWA and FWB contain the required numbers.
Exit: The FWA contains the result.

Description:

If FWA Mantissa byte 1 = 0 then FWA= 0.0, so return FWB
in FWA (AcopyB routine).

A = FWA Exponent - FWB Exponent
If exponents are equal then goto 83E2 to do the addition.

If the FWA exponent > FWB exponent then goto 83A9 to adjust the exponents to be equal.
This routine does the following:
  Inverse and increment (by 1) the exponent difference. This will make it a two's
  complement value and complement the value (exponent diff=-exponent diff).
  If the exponent difference > &24 then the FWA is too small to add, so return FWA=FWB.
  
  Otherwise, Set the Results exponent [FWA exponent] = FWB exponent.

  If the exponent difference is a multiple of 8 (8, 16, 24, 32) then shift
  the FWA mantissa right a byte [divide FWA Mantissa by 16] and reduce the
  exponent diference by 8. [Keep doing this until FWA exponent = FWB exponent].

  If the exponent difference is zero (it will be if the byte shifting was done (above))
  then do the addition [83E2].
  Otherwise shift the FWA Mantissas bits right a bit [divide Mantissa by 2] and decrement
  the exponent difference. Repeat this until the difference is zero.

Lastly, goto 83E2 to do the additional as the exponents are now equal.

Otherwise, (FWB exponent > FWA exponent) so need to make the exponents equal as follows:
  If the exponent difference > &24 then the FWB is too small to add, so return with FWA unchanged.
  
  Otherwise, if the exponent difference is a multiple of 8 (8, 16, 24, 32) then shift
  the FWB Mantissa right a byte [divide FWA Mantissa by 16] and reduce the
  exponent diference by 8. [Keep doing this until FWB exponent = FWA exponent].

  If the exponent difference is zero (it will be if the byte shifting was done (above))
  then do the addition [83E2].
  Otherwise shift the FWB Mantissa right one bit [divide Mantissa by 2] and decrement
  the exponent difference. Repeat this until the difference is zero.

Lastly, goto 83E2 to do the additional as the exponents are now equal.

83E2: Addition (where the exponents are equal):
A = FWA Sign; EOR with FWB Sign.
If different signs then goto 83EC to do subtraction (described below).

Clear carry and jump to &A447 (in Floating-Point Multiply by 10 routine).
A447 does the following:
  FWA Mantissa 5 = FWA Mantissa 5 + FWB Mantissa 5
  FWA Mantissa 4 = FWA Mantissa 4 + FWB Mantissa 4
  FWA Mantissa 3 = FWA Mantissa 3 + FWB Mantissa 3
  FWA Mantissa 2 = FWA Mantissa 2 + FWB Mantissa 2
  FWA Mantissa 1 = FWA Mantissa 1 + FWB Mantissa 1

  If no overflow from adding the Mantissas then end (RTS).
  (Note: The exponent and sign bytes are unchanged as they are the same for both numbers.)
  Otherwise, divide the Mantissa by 2 (keeping the top bit set) & increment exponent.

83EC: Signs are not equal - do subtraction:
If FWA = FWB then Clear FWA to return with FWA = 0.0 (numbers add to zero)
else 840D: If FWA > FWB then goto 8435 (Subtract FWA from FWA) [Described below]

Otherwise, copy the FWB sign to FWA sign (as the result will have the same sign as FWB)
& subtract FWA Mantissa from FWB Mantissa storing the result in the FWA Mantissa.
Jump to &81F9 to Normalise FWA (A = FWA Mantissa 1 on entry)

8435: Subtract FWB Mantissa from FWA Mantissa:
This routine subtracts the FWB value from FWA, keeping the FWA Exponent and FWA sign unaltered
as the result will have the same sign as the FWA.

Subtract FWA Mantissa from FWB Mantissa storing the result in the FWA Mantissa.
Jump to &81F9 to Normalise FWA (A = FWA Mantissa 1 on entry)


Disassembly for the Floating-Point Addition routine

8368 1 165 049 A5 31 LDA &31
836A   240 221 F0 DD BEQ -35 --> &8349 AcopyB
836C 8 056 38 SEC
836D 0 165 048 A5 30 LDA &30
836F < 229 060 E5 3C SBC &3C
8371 o 240 111 F0 6F BEQ 111 --> &83E2
8373 4 144 052 90 34 BCC 52 --> &83A9
8375 % 201 037 C9 25 CMP#&25
8377   176 238 B0 EE BCS -18 --> &8367 (RTS)
8379   168 A8 TAY
837A )8 041 056 29 38 AND#&38
837C   240 023 F0 17 BEQ 23 --> &8395
837E 8 056 38 SEC
837F @ 166 064 A6 40 LDX &40
8381 A 134 065 86 41 STX &41
8383 ? 166 063 A6 3F LDX &3F
8385 @ 134 064 86 40 STX &40
8387 > 166 062 A6 3E LDX &3E
8389 ? 134 063 86 3F STX &3F
838B = 166 061 A6 3D LDX &3D
838D > 134 062 86 3E STX &3E
838F d= 100 061 64 3D STZ &3D
8391   233 008 E9 08 SBC#&08
8393   208 234 D0 EA BNE -22 --> &837F
8395   152 98 TYA
8396 ) 041 007 29 07 AND#&07
8398 H 240 072 F0 48 BEQ 72 --> &83E2
839A F= 070 061 46 3D LSR &3D
839C f> 102 062 66 3E ROR &3E
839E f? 102 063 66 3F ROR &3F
83A0 f@ 102 064 66 40 ROR &40
83A2 fA 102 065 66 41 ROR &41
83A4 : 058 3A DEC A
83A5   208 243 D0 F3 BNE -13 --> &839A
83A7 9 128 057 80 39 BRA 57 --> &83E2

83A9: Adjust FWA exponent to equal FWB Exponent (where FWA exp > FWB exp)

83A9 I 073 255 49 FF EOR#&FF
83AB   026 1A INC A
83AC % 201 037 C9 25 CMP#&25
83AE   176 153 B0 99 BCS -103 --> &8349 AcopyB
83B0 < 164 060 A4 3C LDY &3C
83B2 0 132 048 84 30 STY &30
83B4   168 A8 TAY
83B5 )8 041 056 29 38 AND#&38
83B7   240 023 F0 17 BEQ 23 --> &83D0
83B9 8 056 38 SEC
83BA 4 166 052 A6 34 LDX &34
83BC 5 134 053 86 35 STX &35
83BE 3 166 051 A6 33 LDX &33
83C0 4 134 052 86 34 STX &34
83C2 2 166 050 A6 32 LDX &32
83C4 3 134 051 86 33 STX &33
83C6 1 166 049 A6 31 LDX &31
83C8 2 134 050 86 32 STX &32
83CA d1 100 049 64 31 STZ &31
83CC   233 008 E9 08 SBC#&08
83CE   208 234 D0 EA BNE -22 --> &83BA
83D0   152 98 TYA
83D1 ) 041 007 29 07 AND#&07
83D3   240 013 F0 0D BEQ 13 --> &83E2
83D5 F1 070 049 46 31 LSR &31
83D7 f2 102 050 66 32 ROR &32
83D9 f3 102 051 66 33 ROR &33
83DB f4 102 052 66 34 ROR &34
83DD f5 102 053 66 35 ROR &35
83DF : 058 3A DEC A
83E0   208 243 D0 F3 BNE -13 --> &83D5

83E2: FWA = FWA + FWB [where the exponents are equal]

83E2 . 165 046 A5 2E LDA &2E
83E4 E; 069 059 45 3B EOR &3B
83E6 0 048 004 30 04 BMI 4 --> &83EC
83E8   024 18 CLC
83E9 LG 076 071 164 4C 47 A4 JMP &A447
83EC 1 165 049 A5 31 LDA &31
83EE = 197 061 C5 3D CMP &3D
83F0   208 027 D0 1B BNE 27 --> &840D
83F2 2 165 050 A5 32 LDA &32
83F4 > 197 062 C5 3E CMP &3E
83F6   208 021 D0 15 BNE 21 --> &840D
83F8 3 165 051 A5 33 LDA &33
83FA ? 197 063 C5 3F CMP &3F
83FC   208 015 D0 0F BNE 15 --> &840D
83FE 4 165 052 A5 34 LDA &34
8400 @ 197 064 C5 40 CMP &40
8402   208 009 D0 09 BNE 9 --> &840D
8404 5 165 053 A5 35 LDA &35
8406 A 197 065 C5 41 CMP &41
8408   208 003 D0 03 BNE 3 --> &840D
840A L 076 180 166 4C B4 A6 JMP &A6B4 Clear FWA
840D & 176 038 B0 26 BCS 38 --> &8435
840F ; 165 059 A5 3B LDA &3B
8411 . 133 046 85 2E STA &2E
8413 8 056 38 SEC
8414 A 165 065 A5 41 LDA &41
8416 5 229 053 E5 35 SBC &35
8418 5 133 053 85 35 STA &35
841A @ 165 064 A5 40 LDA &40
841C 4 229 052 E5 34 SBC &34
841E 4 133 052 85 34 STA &34
8420 ? 165 063 A5 3F LDA &3F
8422 3 229 051 E5 33 SBC &33
8424 3 133 051 85 33 STA &33
8426 > 165 062 A5 3E LDA &3E
8428 2 229 050 E5 32 SBC &32
842A 2 133 050 85 32 STA &32
842C = 165 061 A5 3D LDA &3D
842E 1 229 049 E5 31 SBC &31
8430 1 133 049 85 31 STA &31
8432 L 076 249 129 4C F9 81 JMP &81F9 Normalise FWA#1

8435: Subtract FWB Mantissa from FWA Mantissa:

8435 8435 5 165 053 A5 35 LDA &35
8437 A 229 065 E5 41 SBC &41
8439 5 133 053 85 35 STA &35
843B 4 165 052 A5 34 LDA &34
843D @ 229 064 E5 40 SBC &40
843F 4 133 052 85 34 STA &34
8441 3 165 051 A5 33 LDA &33
8443 ? 229 063 E5 3F SBC &3F
8445 3 133 051 85 33 STA &33
8447 2 165 050 A5 32 LDA &32
8449 > 229 062 E5 3E SBC &3E
844B 2 133 050 85 32 STA &32
844D 1 165 049 A5 31 LDA &31
844F = 229 061 E5 3D SBC &3D
8451 1 133 049 85 31 STA &31
8453 L 076 249 129 4C F9 81 JMP &81F9 Normalise FWA#1

 


 Back to 8BS
Or