Most versions of BASIC have DATA, READ, and RESTORE statements. If these statements can be simulated for the Timex/Sinclair 1000/ZX81, one has a powerful addition to Sinclair BASIC. The simulated DATA, READ, and RESTORE statements make it possible to translate many programs into Sinclair BASIC without making major changes in the program.
First let us review how DATA, READ, and RESTORE statements work in most microcomputers. Suppose that one has three numbers, 2, 77, and 25. These numbers could be entered in the following DATA statement.
10 DATA 2, 77, 25
The number 10 is a statement number, DATA is a key work, and 2, 77, and 25 are the three numbers. Suppose that a program contains the statements
40 READ Z,R
The first number in DATA statement 10 would be assigned to the first variable in the READ statement. The second number in the DATA statement would be assigned to the second variable in the READ statement. Thus, the variable Z would be assigned 2, and the variable R would be assigned 77. If the statement
70 READ Q
occurred later in the program, the number 25 would be assigned to the variable Q. Note that the numbers are assigned to variables in the order that they appear in the DATA statement. If a program has more than one DATA statement, the data are read from the lowest numbered DATA statement, then the next lowest statement, etc. Variables in the read statement are not limited to variables without subscripts. Thus
80 READ A,A(2,1),B(A),Q
is a valid READ statement.
BASIC has a built in pointer that is incremented every time an observation is read from the data list. The pointer then points to the next observation that will be read from the DATA list. A RESTORE statement such as
90 RESTORE
resets the pointer to the first observation in the first DATA statement. Consider the following program sequence.
10 DATA 4, 4, 2E22
20 DATA 2, 9E22
30 READ A
40 READ B, Z(1,1), L
50 RESTORE
60 READ Q
After the sequence is completed, A is equal to 4, B is equal to 3, Z(1,1) is equal to 2E22, L is equal to 2, and Q is equal to 4. The RESTORE in statement 50 resets the pointer so that statement 60 causes the first observation in the first DATA statement to be assigned to Q.
Simulated DATA, READ, and RESTORE Statements
The simulated DATA, READ, and RESTORE statements for Sinclair microcomputers are very similar to standard DATA, READ, and RESTORE statements. DATA statement
10 DATA 4,3,2E22
would be replaced with
10 LET A$="4,3,2E22;"
Note that a simulated DATA statement starts with the key word LET followed by a string variable, an equal sign, and a string in quotes. The string contains the individual numbers separated by commas. The last number in the string must be followed by a semicolon. The first simulated DATA statement uses A$ after LET, the next simulated DATA statement uses B$, the next C$, etc. Thus, most versions of BASIC would have the DATA statements
10 DATA 4,3,2E22
20 DATA 2,9E22
while the equivalent simulated DATA statements would be
10 LET A$="4,3,2E22;"
20 LET B$="2,9E22;"
Simulated DATA statements are not affected by extra spaces before or after commas or semicolons. The simulated DATA statements must be placed earlier in the program than the subroutines that will be described below. That is, the BASIC interpreter must process these simulated DATA statements before the first simulated READ statement is encountered in the program. Simulated DATA statements are limited to numeric values. That is, strings cannot be used as DATA in simulated DATA statements.
Simulated READ statements can only contain one variable. The conventional READ statement
30 READ A
would be replaced with
30 GOSUB READ
35 LET A=RES
That is, statement 20 transfers the program to the READ subroutine and statement 35 assigns the RES(ult) of the READ to variable A. A statement such as
40 READ B,Z(1,1),L
would have the following simulated BASIC READ sequence
40 GOSUB READ
42 LET B=RES
45 GOSUB READ
46 LET Z(1,1)=RES
48 GOSUB READ
50 LET L=RES
A conventional RESTORE statement such as 50 RESTORE is simulated with
50 GOSUB RESTORE
The Program
Figure 1 shows the statements that must be added to a program when simulated DATA, READ, and RESTORE statements are required.
Figure 1. Subroutines for Simulating DATA, READ, and RESTORE Statements
1 LET READ=9000
2 LET RESTORE=9180
3 LET AAA=1
4 LET BBB=1
5 LET DDD=0
(simulated Data statements are placed at
this point in the program sequence)
(the main program is placed at this point
in the program sequence)
9000 IF BBB=1 THEN LET V$=A$
9010 IF BBB=2 THEN LET V$=B$
9020 IF BBB=3 THEN LET V$=C$
9030 IF BBB=4 THEN LET V$=D$
9040 LET CCC=AAA
9050 IF V$(AAA TO AAA)="," THEN GOTO 9110
9060 IF V$(AAA TO AAA)<>";" THEN GOTO 9090
9070 LET DDD=1
9080 GOTO 9110
9090 LET AAA=AAA+1
9100 GOTO 9050
9110 LET RES=VAL (V$(CCC TO (AAA-1)))
9120 LET AAA=AAA+1
9130 IF DDD<>1 THEN RETURN
9140 LET DDD=0
9150 LET BBB=BBB+1
9160 LET AAA=1
9170 RETURN
9180 LET AAA=1
9190 LET BBB=1
9200 RETURN
Statements 1 through 5 should be the first statements in the program. These statements initialize values for the DATA, READ, and RESTORE Subroutines. The simulated DATA statements should be the next statements in the program. This will insure that the simulated DATA statements are processed before the DATA, READ, and RESTORE subroutines are encountered. Then the main body of the program should be inserted. Finally, the DATA, READ, and RESTORE subroutines are placed in unused, high statement numbers to avoid conflicts with the main program. Place a STOP statement between the main body of the program and the DATA, READ, and RESTORE subroutines. This will prevent the microcomputer from executing the subroutines after the program is run.
Examine statements 9000 through 9030 of Figure 1. These statements set up four consecutive simulated DATA statements in strings A$, B$, C$, and D$. If a sixth simulated DATA statement is required, it is
9031 IF BBB=5 THEN LET V$=E$
A seventh simulated DATA statement would require
9032 IF BBB=6 THEN LET V$=F$
Each simulated DATA statement uses one string variable. This string variable is no longer available for the main program. One will seldom need more than four simulated DATA statements because Sinclair microcomputers allow the user to enter a screen full of numbers in each simulated DATA statement. Variables such as AAA, and BBB have been used in the subroutines to minimize the possibility that one will inadvertently have the same variables in the main program. REMark statements have not been inserted in the subroutines to minimize the memory required for the subroutines.
How the Program Works
Statements 3, 4, and 5 initiate the data pointer within a statement (3), the statement pointer (4), and the statement competion pointer (5). The data pointer is started at 1, the start of the first number in the first DATA statement. The statement pointer is also set to 1 to point to the first simulated DATA statement. The statement completion pointer is set to 0 as opposed to 1 because all of the data in the first statement have not been read. Since the statement pointer starts at 1, statement 9000 sets V$ equal to A$. Statements 9010 through 9030 do not affect the program sequence when the statement pointer is at 1. Statement 9040 sets the beginning of a number pointer to the beginning of the current number. Statements 9050 and 9060 check to determine if the end of a number (statement 9050) or the end of a number at the end of a simulated DATA statement (statement 9060) has been reached. If the end of number has not been reached, statement 9090 increments the data pointer and statement 9100 returns control to statement 9050 to test for the end of a number. If the end of a simulated DATA statement is reached, statement 9070 increments the statement completion counter to 1 and statement 9080 transfers control to statement 9110, the same statement that is encountered whenever the end of a number is detected.
Since the beginning of the current number is CCC and the end is AAA-1, statement 9110 uses the VAL function and slicing features of Sinclair microcomputers to change the portion of the string that represents the number into a numeric variable. Then the data pointer within a statement is incremented in statement 9120. If the end of a simulated DATA statement is reached, statement 9130 resets the statement completion counter (statement 9140), and the data pointer within a statement to 1 (the beginning of a statement). The data statement pointer is incremented in statement 9150.
Statements 9180 through 9200 are the simulated RESTORE subroutine. Statement 9180 sets the data pointer within a statement to the beginning of a statement and statement 9190 resets the statement pointer to the first simulated DATA statement.
I have written a book entitled Translating Programs into Timex Sinclair BASIC. The book shows methods for extending the simulation routines to include string variables in DATA statements. I have also written a book of statistics programs that uses the simulation routines as part of the data entry procedure. Send to K.D.V.H.E. Publishers (P.O. Box 6788; Chicago, Ill 60680) for a description of “books for Timex Sinclair/1000, ZX81 microcomputers.”
John Richard Coffey (P.O. Box 448; Scottsburg, Indiana 47170) has written a machine language program to simulate DATA, READ, and RESTORE statements. The machine language program is available on his Super Tape which also includes many other programs.
There are advantages and disadvantages to simulating DATA, READ, and RESTORE statements with machine language subroutines as opposed to BASIC subroutines using strings. Machine language subroutines take less computer memory and run faster. On the other hand the machine language subroutines require loading the subroutines before a program that uses the subroutines can be run. If a user makes an incorrect call to a machine language subroutine while developing a program, the computer can destroy the program that is being developed.
TSH
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.