Variable Conversions in the ZX80

See all articles from SYNC v1 n3
Author(s): Joseph Sutton
Publication: SYNC
Pub Details: SYNC v1 n3 (Volume 1, Issue 3)
Date: May/June 1981
Pages: 26

There are many cases when it is convenient to convert one type of variable to another, such as numeric to string, or string to numeric. In the ZX80 variable conversions from numeric to string are done with the STR$( ) function. The reverse conversion is not available with the 4K Basic. Given a string variable containing numbers, the program below looks at each number individually to determine its magnitude. The CODES for the numbers go from 28 to 37 (CODE (“0”) = 28, CODE (“1”) = 29, etc) so that by subtracting 28 from the CODE you get the number itself. By using CODE (A$) – 28 then remove the first character with TL$ ( ).

108 LET A$ = TL$ (A$)

X is then tested with an IF statement to verify that it is a number from 0 to 9, and if it is, it is put into the numeric N with LET N = (N * 10) * X.

109 IF NOT X < 0 AND X < 10 THEN
LET N = (N * 10) + X

Let’s assume we have a string containing “123.” Line 107 sets X equal to 1 (29-28), then line 108 removes the “1” from A$, and line 109 sets X equal to 1 (N = (0 * 10) + 1). If we return to 106 and start again, A$ = “23”, X = 2 and N = 12 (N = (1 * 10) + 2). If we go through a third time, A$ = “3”, X = 3 and N = 123 (N = (12 * 10) + 3). Now we need something to test for the end of the string.

113 IF X = -27 THEN RETURN
115 GO TO 107

In the ZX80 all strings end with a “null” character with a CODE of 1. Line 113 tests for this and returns to the main program when the end is detected. Line 115 keeps the routine going until line 113 finds the end. Now, with one more line, we have a Basic subroutine for doing the missing conversion.

103 LET N = 0
107 LET X = CODE (A$) - 28
-108 LET A$ = TL$ (A$)
109 IF NOT X < 0 AND X < 10 THEN
LET N = (N * 10) + X
113 IF X = -27 THEN RETURN
115 GO TO 107

This subroutine will search the string A$ until it finds a number. It will then put the numbers into N, stopping when it finds the end of the string, ignoring all other characters. The routine will also destroy the contents of A$, so A$ must be saved if you want it for later use.

With only minor modifications it can be made to accept decimal numbers and scientific notation. First N becomes an ARRAY containing the integer portion in N (0), the fractional portion in N (1), and the exponent (for scientific notation) in N (2). To use this routine as a subroutine all three portions of the ARRAY must be set to zero at the beginning. S is the counter to tell the ZX80 in which part of the ARRAY to place the numbers. We also have to add lines to detect decimal points and “E” if scientific notation of the form 1.5 E 3 is required (1.5 * 10^3). Line 110 is added to detect minus signs and lines 116 and 117 properly locate the minus signs. When the fractional portion of the number is processed, the leading zeros are removed. If they are not kept track of, errors will occur; 1.005 would become 1.5, etc. Line 114 detects leading zeros and Z equals the number of leading zeros. In line 30 of the main program you will notice the expression TL$ (STR$ (10 ** Z)). IF Z = 2 (2 leading zeros), 10 ** Z = 100 and the expression becomes the string “100” with the “1” removed or “00”. This replaces the zeros in the print statement. No [illegible] four leading zeros can be [illegible] 5 = 100,000 and y[illegible] error message 6, arithmetic overflow).

 10 INPUT A$
20 GOSUB 100
30 PRINT N(0);".";TL$(STR$(10**Z));N(1);"*10**";N(2)
40 STOP

100 LET Z=0
101 LET Y=0
102 DIM N(2)
103 LET N(0)=0
104 LET N(1)=0
105 LET N(2)=0
106 LET S=0
107 LET X=CODE(A$)-28
108 LET A$=TL$(A$)
109 IF NOT X<0 AND X<10 THEN LET N(S)=(N(S)*10)+X
110 IF X=192 THEN LET Y=Y+S+1
111 IF X=-1 THEN LET S=1
112 IF X=14 THEN LET S=2
113 IF X=-27 GO TO 116
114 IF X=0 AND S=1 AND N(1)=0 THEN LET Z=Z+1
115 GO TO 107
116 IF Y=1 OR Y=4 THEN LET N(0)=N(0)*-1
117 IF Y=3 OR Y=4 THEN LET N(2)=N(2)*-1
118 RETURN

This is the full subroutine with all the fancy things added and a small “main” program.

ENTERING          PRINTS ON SCREEN
1.414E-3          1.414*10**-3
150               150.0*10**0
-500.8E6          -500.8*10**6
-5.003E-9         -5.003*10**-9

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