×   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

Convert Integer to Floating-Point

Submitted by Steve Fewell

Routine: ConvIWAtoFWA
Name: Convert Integer to Floating-Point
Starting Address: &8185
Entry criteria: The IWA contains an Integer number.
Exit: The value of the IWA has been transferred to the FWA.

Note: The FWA Mantissa is stored Most significant byte first, least significant byte last (the opposite way to Integers).

Description:
Set the FWA Rounding (Mantissa 5) and Exponent Overflow bytes to zero. Store the IWA Sign Byte (?&2D) in the FWA Sign Byte (The top bit of this byte indicates whether the number is positive or negative, and its value is directly transferable between the IWA and FWA).

If the Integer in the IWA is negative, then complement the Integer, and load A with the most significant byte of the Integer (We can deal directly with a positive value as the FWA will be stored as a positive number, with its Sign Byte indicating its sign).

If the Most Significant Byte of the IWA [A] isn't zero, then the number is 4 bytes long, so store IWA Byte 3 in FWA Mantissa Byte 2,  IWA Byte 2 in FWA Mantissa Byte 3 and  IWA Byte 1 in FWA Mantissa Byte 4 (opposite order), then normalise the FWA (Make the Most Significant Bit of the FWA's Mantissa equal to 1, and store A back to FWA Mantissa Byte 1, and Y back to the FWA Exponent), with an initial exponent [Y] value of 160 (that is 2^32, as there are 32 bits in a 4 byte number), and exit.

Otherwise, store zero in FWA Mantissa Byte 4, and check to see if IWA Byte 3 is zero.

If IWA Byte 3 isn't zero, then the number is 3 bytes long, so store IWA Byte 2 in FWA Mantissa Byte 2 and  IWA Byte 1 in FWA Mantissa Byte 3 (opposite order), then normalise the FWA (Make the Most Significant Bit of the FWA's Mantissa equal to 1, and store A back to FWA Mantissa Byte 1, and Y back to the FWA Exponent), with A = IWA Byte 3 and an initial exponent [Y] value of 152 (that is 2^24, as there are 24 bits in a 3 byte number), and exit.

Otherwise, store zero in FWA Mantissa Byte 3, and check to see if IWA Byte 2 is zero.

If IWA Byte 2 isn't zero, then the number is 2 bytes long, so store IWA Byte 1 in FWA Mantissa Byte 2, then normalise the FWA (Make the Most Significant Bit of the FWA's Mantissa equal to 1, and store A back to FWA Mantissa Byte 1, and Y back to the FWA Exponent), with A = IWA Byte 2 and an initial exponent [Y] value of 144 (that is 2^16, as there are 16 bits in a 2 byte number), and exit.

Otherwise, store zero in FWA Mantissa Byte 2 and normalise the FWA (Make the Most Significant Bit of the FWA's Mantissa equal to 1, and store A back to FWA Mantissa Byte 1, and Y back to the FWA Exponent), with A = IWA Byte 1 and an initial exponent [Y] value of 136 (that is 2^8, as there are 8 bits in a 1 byte number). The normalisation will decide whether the number is zero, or a 1-byte number, and adjust the FWA accordingly.

Disassembly for the Convert Integer to Floating-Point routine

8185 d5 100 053 64 35 STZ &35
8187 d/ 100 047 64 2F STZ &2F
8189 - 165 045 A5 2D LDA &2D
818B . 133 046 85 2E STA &2E
818D   016 005 10 05 BPL 5 --> &8194
818F   032 222 172 20 DE AC JSR &ACDE   icomp
8192 - 165 045 A5 2D LDA &2D
8194 & 208 038 D0 26 BNE 38 --> &81BC
8196 d4 100 052 64 34 STZ &34
8198 , 165 044 A5 2C LDA &2C
819A   208 020 D0 14 BNE 20 --> &81B0
819C d3 100 051 64 33 STZ &33
819E + 165 043 A5 2B LDA &2B
81A0   208 006 D0 06 BNE 6 --> &81A8
81A2 d2 100 050 64 32 STZ &32
81A4 * 165 042 A5 2A LDA &2A
81A6 8 128 056 80 38 BRA 56 --> &81E0
81A8 * 164 042 A4 2A LDY &2A
81AA 2 132 050 84 32 STY &32
81AC   160 144 A0 90 LDY#&90
81AE 2 128 050 80 32 BRA 50 --> &81E2
81B0 + 164 043 A4 2B LDY &2B
81B2 2 132 050 84 32 STY &32
81B4 * 164 042 A4 2A LDY &2A
81B6 3 132 051 84 33 STY &33
81B8   160 152 A0 98 LDY#&98
81BA & 128 038 80 26 BRA 38 --> &81E2
81BC , 164 044 A4 2C LDY &2C
81BE 2 132 050 84 32 STY &32
81C0 + 164 043 A4 2B LDY &2B
81C2 3 132 051 84 33 STY &33
81C4 * 164 042 A4 2A LDY &2A
81C6 4 132 052 84 34 STY &34
81C8   160 160 A0 A0 LDY#&A0
81CA   128 022 80 16 BRA 22 --> &81E2    Normalise FWA#2

Normalise FWA with an initial exponent of 136 (&88)

81E0   160 136 A0 88 LDY#&88
81E2 Normalise FWA#2

 


 Back to 8BS
Or