String handling on the ZX80 is reasonably good. The 4K Integer Basic lets the user print, input, and compare strings, and do specialized routines that will transform numbers into strings or characters.
There are also some large holes in its string handling abilities though such as limited string truncation and no concatenation. It is not hard to figure out why: Integer Basic takes up less than 3600 bytes of space, since the character generator, about 500 bytes long, is also in the 4K ROM. The only command for changing the size of a string is TL$ (which stands for Truncate (shorten) from the Left of the $tring). PRINT TL$(“FRED”) produces “RED”; TL$ chops off the leftmost character of the string in parentheses whether it is a literal string (like “FRED”) or a variable (A$, for example). With TL$ you can trim as much as you like from a string—but only one byte at a time, and only from the left side.
Sinclair’s Integer Basic has no string concatenation commands at all. In other words, there is nothing like LET A$=A$+B$. You cannot lengthen a string.
Other small Basics—for example, Radio Shack’s Level 1—allow fewer string variables and only INPUT and PRINT commands. The ZX80 looks good by comparison, but comparison cannot fill those string-handling holes.
This program can.
Enter the program in Figure 1. Line 10 should contain 52 zeroes.
10 REM 0000000000000000000000
000000000000000000000000000000
20 FOR A=1 TO 52
30 PRINT A;" ";
40 INPUT B
50 POKE 16426+A,B
60 PRINT B,
70 NEXT A
80 INPUT A
90 IF A=0 THEN STOP
100 INPUT B
110 POKE 16426+A,B
120 PRINT A;" ";B,
130 GO TO 80
[Figure: Figure 1.]
Run the program and enter the following numbers in order. The numbers in parentheses are just entry numbers. Do not key them in.
(1) 175 (2) 235 (3) 30 (4) 10 (5) 197 (6) 225 (7) 57 (8) 235 (9) 249 (10) 235 (11) 227 (12) 43 (13) 43 (14) 43 (15) 40 (16) 9 (17) 227 (18) 209 (19) 213 (20) 227 (21) 35 (22) 3 (23) 3 (24) 24 (25) 21 (26) 60 (27) 237 (28) 185 (29) 35 (30) 235 (31) 33 (32) 0 (33) 0 (34) 35 (35) 12 (36) 32 (37) 252 (38) 4 (39) 32 (40) 249 (41) 227 (42) 193 (43) 213 (44) 227 (45) 35 (46) 35 (47) 237 (48) 176 (49) 235 (50) 227 (51) 249 (52) 201
When you have keyed in the 52nd value, stop and proofread the contents of your screen very carefully.
If you find a mistake, type the entry number, hit NEWLINE, then enter the correct value. The new version will appear at the end of the list of numbers. When you have corrected all mistakes, enter 0.
The program listing should now look something like Figure 2. Delete lines 20 through 130, and you are ready to key in your own program.
10 REM ▒ FOR 2▒? OR T FOR CONT
INUE FOR =FFFC▒=? THEN =7▒</W P
OKE ▒7 FOR 5 7£4 LOAD ▒4 CONTIN
UE =? THEN =77 POKE ▒FOR = CONTI
NUE ?
[Figure: Figure 2.]
You now have two new string functions. The first will truncate a string from the right side; a TR$, if it existed, would do the same thing. To perform the equivalent of
LET G$=TR$(G$)
you write:
LET G$=G$
RANDOMISE USR(16428)
After the routine is complete, a PRINT G$ would show that the final character is gone.
The second function is even more useful; it is the equivalent of
LET L$=L$+M$
The appropriate program lines are:
LET L$=L$
LET M$=M$
RANDOMISE USR(16427)
At the end of the routine, M$ no longer exists, and L$ contains both strings.
Each function will work with any string variable, A$-Z$.
A few caveats:
You must perform the LET commands immediately before the USR line. LET creates a new entry at the end of the list of variables in RAM. By performing those LETs, you put the strings you are working on at the end of the variable list so the USR routine knows just where to go to find them.
You can substitute LET, PRINT, IF…THEN, and most other commands for RANDOMISE. Remember, though, if you create a new variable with LET, you will have to LET A$=A$ etc. again before performing any more of these string functions.
Do not try to TR$ a string with nothing inside. You can do it if, for example, Q$=” “—but not if Q$=””. If CODE (Q$)=1, do not use the TR$ routine.
These routines will not work with literals—only string variables. You must put a string into a variable before you can perform these operations on it.
If you want to use line 10 for your program, you can renumber the REM line using the EDIT function. It will work without change if it is still the first line of the program; if not, you will have to find its new location to call the routines with USR. The routines should never begin beyond 16639, or you will have to alter the machine language program.
Figure 3 lists the routine in assembly language. The procedure is relatively straightforward. Each routine loads HL with the E-LINE value (which points to the end of the variable list); finds the end of the variable it wants to work on; shifts the variable list down, one byte at a time, until it reaches the end of the list; then loads the new value of E-LINE into the proper location.
Parts of the routine may seem more complicated than necessary. The extra code is used to avoid instructions using values between 64 and 127 decimal (40h-7Fh). These values cause screen distortion and quickly crash the system if the ZX80 tries to display them as characters in a string or program line. A program that cannot be safely listed is too impractical for general use, so this one avoids those values. The routine begins, for example, not with LD HL, (400A), but with a longer instruction sequence that avoids the unlistable values.
At the beginning of the routine, certain things are taken for granted: BC=0, the Z flag is reset, and H=40h. The first two are always true when a routine is called with a USR command. The third is true for any routine between USR(16384) and USR(16639), since the ZX80 simply loads HL with the starting point and does a JP (HL).
Finally, notice that the end of the routine HL=SP. Thus, PRINT USR(16427) will not only add two strings together, but will also tell you where the top of available memory is. PRINT USR(16427)-PEEK(16400)-256*PEEK(16401) will return the amount free memory remaining—but be sure that you have the strings in place to be worked on, or you will scramble your variables and you may crash the system.
decimal hex assembler
175 AF XOR A A=0, Z=1
235 EB EX DE, HL D=40h
30, 10 1E0A LD E, 0Ah E=0Ah
197 C5 PUSH BC HL=0
225 E1 POP HL
57 39 ADD HL, SP HL=SP
235 EB EX DE, HL DE=SP, HL=400Ah
249 F9 LD SP, HL SP=400Ah
235 EB EX DE, HL HL=SP
227 E3 EX (SP), HL HL=(400Ah), (SP)=SP
43 2B DEC HL (HL)=80h
43 2B DEC HL (HL)=01h
43 2B DEC HL (HL)=character
40, 9 2809 JR Z, 09h jump to $+$
227 E3 EX (SP), HL DE=HL
209 D1 POP DE (DE)=character
213 D5 PUSH DE
227 E3 EX (SP), HL
35 23 INC HL (HL)=01h
3 03 INC BC BC=0002h
3 03 INC BC
24, 21 1815 JR 15h jump to finish
60 3C INC A $+$: A=01h
237, 185 EDB9 CPDR BC=displacement
35 23 INC HL (HL)=01h
235 EB EX DE, HL (DE)=01h
33, 0, 0 210000 LD HL, 0000 HL=0
35 23 INC HL HL=-BC
12 0C INC C
32, 252 20FC JR NZ, FCh
4 04 INC B
32, 249 20F9 JR NZ, F9h
227 E3 EX (SP), HL
193 C1 POP BC BC=HL=displacement
213 D5 PUSH DE
227 E3 EX (SP), HL HL=DE
35 23 INC HL
35 23 INC HL (HL)=character
237, 176 EDB0 LDIR finish: shift until
BC=0, DE=E-LINE
235 EB EX DE, HL
227 E3 EX (SP), HL HL=SP, E-LINE=E-LINE
249 F9 LD SP, HL SP=SP
201 C9 RET
[Figure: Figure 3.]
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.