Decimal to Floating Point Binary

Author(s): C. C. Stalder
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v4 n5 (Volume 4, Issue 5)
Date: May 1983
Pages: 8-9

John Bowden describes an interesting pencil-and-paper procedure to convert decimal numbers to ZX/TS floating point binary (Jan.83).

His procedure works fine, but is slow and can only handle positive integers smaller than 2**32. Your ZX/TS ROM has a number conversion routine that is faster, not as limited and easy to use. You can even use it in command mode:

LET X0X=N
LET Y0Y=PEEK 16404+256*PEEK 16405
PRINT PEEK (Y0Y-6),,PEEK (Y0Y-5),,PEEK (Y0Y-4),,PEEK (Y0Y-3),,PEEK (Y0Y-2)

These three commands display a list of decimal values of the five bytes for number n, in Sinclair-type floating point format. But this routine requires a lot of typing to convert one number.

It works by assigning a decimal number to the variable X0X, the last item stored in the variables area of program memory. The last-defined variable is always located just before the address held in system variable E_LINE. The second command PEEKs E_LINE to get the address. The third command backs up from that address and displays bytes containing the floating point number, exponent byte first, followed by the mantissa (decimal part of a logarithm) bytes in order of decreasing significance.

Yet, a BASIC program modeled after the preceding command sequence gives problems–primarily insuring X0X is always the last-defined variable. Avoid this problem by using the variables start area marked by system variable VARS, rather than the variables end area marked by E_LINE.

Listing 1 shows 11 lines you can use to start any program needing decimal to binary conversion.

To use this program assign the decimal number (or numeric expression) to variable X0X, and then GOSUB 50. When executing the program by RUN or GOTO, line 10 clears the variables area of program memory. Thus, variable X0X initially gets the value 0 and becomes the first-defined variable. System variable VARS always holds variable X0X’s address.

On executing a program line that assigns a value to a variable, the computer automatically converts the value to floating point binary form and stores it that way. Line 50 gets the X0X address from VARS. Lines 60-68 put each byte of the binary number into its own variable. After the subroutine returns, the main program can do anything it likes with the information, held in variables Z1Z to Z5Z.

This conversion routine uses unique variable names to minimize inadvertent re-use of the same names later in the main program. Reusing variable names won’t bother the conversion routine, but the main program might go berserk.

This routine is reasonably fast, and can convert any number–positive, negative, fractional, or any ZX/TS numeric expression.

Listing 1.

  10 CLEAR
20 LET X0X=0
30 GOTO 80
40 REM SUBROUTINE TO CONVERT DECIMAL NUMBER IN X0X TO FIVE BYTE F.P. BINARY IN Z1Z TO Z5Z
50 LET Y0Y=PEEK 16400+256*PEEK 16401
60 LET Z1Z=PEEK (Y0Y+3)
62 LET Z2Z=PEEK (Y0Y+4)
64 LET Z3Z=PEEK (Y0Y+5)
66 LET Z4Z=PEEK (Y0Y+6)
68 LET Z5Z=PEEK (Y0Y+7)
70 RETURN
80 REM CONTINUE FROM HERE WITH MAIN PROGRAM
SYNTACTIC SUM: 17300, 8K ROM

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.