The Computer Tutor: READ, DATA & RESTORE

Computer Tutor illustration, tutor pointing at a computer
Computer Tutor illustration, tutor pointing at a computer

The authors of the BASIC language used in the ZX-81 ROM, decided that the commands READ, DATA and RESTORE were to be ommitted. For those of you who are not familiar with the operation of these commands, let me show you how valuable their use can be.

In the following program, we can see these commands in action:

Program #1

10 FOR X = 1 TO 6
20 READ X
30 PRINT X;" ";
40 NEXT X
50 PRINT
60 RESTORE
70 GOTO 10
80 DATA 15,23,17,99,42,36,12,
87,71,91,13,24

If this program could be run on the ZX-81, you would see the screen fill up with repeating identical lines like the ones that follow:

15  23  17  99  42  36
15  23  17  99  42  36
15  23  17  99  42  36

You will notice that as the program runs, it never gets a chance to execute line #80 and yet the required information from that line is being used.

You will also notice that only the first 6 numbers in the DATA statement are utilized.

The way this works is that whenever the program, as it is running, encounters a READ command, it will automatically branch off to the program line that has the DATA statement. It then proceeds to identify the numbers between the commas, in sequence with the FOR/NEXT loop and returns to the proper part of the program. This is similiar in function to the GOSUB command but without the extra keyboard entry.

The RESTORE command resets the – Numeric Counter – in the DATA line back to the first position.

If we were to remove line #60, the first three lines appearing on the screen when the program runs would be:

15  23  17  99  42  36
12  87  71  91  13  24
15  23  17  99  42  36

And the sequence would be repeated until the screen is full. In this program, with the RESTORE command eliminated, we are able to PRINT all of the DATA by having the program READ the DATA values into the single variable, X.

These commands are very useful in saving memory space because they allow a single routine to pull values into it as opposed to setting up a whole bunch of variables such as the following:

Program #2

10 LET X1 = 15
20 LET X2 = 23
....
120 LET X12 = 24
130 PRINT X1;" ";X2;" ";
....;X12

Other techniques of handling this problem, suggest that you use the LET command to assign the values to each variable, such as in the program above, and then delete these lines from the program. Now whenever you want to execute the program and keep those variables intact, you cannot enter RUN. You must enter the GOTO (line-number) command.

This method will however allow you to SAVE the program on tape and when the program is reloaded, the variables will be there. This is true with either Numeric (numbers) variables or String (characters, alpha – graphic – numbers – reverse video) variables.

It is a shame that we don’t have a better method that would simulate the READ, DATA and RESTORE commands, or do we?

Observe the following program:

Program #3

10 LET A$ ="1523179942361287719
11324"
20 FOR X=1TO(LEN A$-1)/2 STEP 2
30 LET B$ = A$( X TO X+1)
40 PRINT B$;" ";
50 NEXT X
60 PRINT
70 GOTO 20

If you run this program on the ZX-81, it will accomplish the exact same results as the first program in about the same amount of program space.

Change line # 20 to read:

20 FOR X = 1 TO LEN A$-1 STEP 2

Now we have the same results as in the second program. The ZX-81 has two strange capabilities that I have never seen in any other 8 bit microcomputer. Even though it only allows 26 string variables from A$ to Z$, each one can contain as many letters, numbers, graphics characters etc. in regular or reverse video as the computer’s memory will allow. The following program generates a string variable whose length is N utilizing the first 64 characters of the ZX-81 character set. You enter a number in the place of N (on a 1K machine, the largest N can be is 231).

Program #4

10 LET A$ = ""
20 FOR X = 1 TO N
30 LET B$ = CHR$ (64 * RND)
40 LET A$ = A$ + B$
50 NEXT X
60 PRINT LEN A$

If you have a 16K machine, you might want to enter the following lines:

15 FAST
55 SLOW

On every other 8 bit microcomputer that I am familiar with, the maximum amount of characters that a string variable can hold is 255. On a 16K ZX-81, I have generated over 4000 into a variable that I have used in a program.

The other strange capability of the ZX-81 is its ability to break apart a string variable using the TO String Operator as in line #30 of Program #3.

This one String Operator takes the place of three String Operators in other BASICs. The LEFT$, RIGHT$ and MID$ String Operators are explained on page 101 of the ZX-81 manual.

Combining the effects of these two capabilities brings about extremely powerful and exciting results that in most cases will not only equal but outperform the READ, DATA and RESTORE commands.

Let us take program #3 and change the following lines:

30 LET B = VAL A$ (X TO X + 1)
40 PRINT B ** .5 ; " ",

And now we find a repetition of the square roots of those two digit numbers. The VAL String Operator was introduced to tell the computer to change the portion of the string variable A$ into the numeric variable B. If this was not done, an error message would occur because A$ does not recognize the numbers it contains as having numeric properties that can be added to, subtracted from have or any of the other mathmatical functions applied to. The VAL String Operator tells the computer to extract that part of the string variable, A$, and change it into a number that will have mathmatical properties. With out this happening, any numbers in A$ are seen by the computer the same way it looks at a letter or graphic character.

But still, so far in the back of our minds, we feel that this method still leaves a lot to be desired. When we look at programs written for other BASICs, we see DATA statements such as:

100 DATA"PENNY","NICKEL","DIME"
,"QUARTER","4 BITS","DOLLAR"
,"FIN","SAWBUCK","DOUBLE
SAWBUCK","C-NOTE"

You say that all of the items are of different configurations and lengths and are neatly separated by commas and quotation marks making them much easier to read in the program listing.

But all that I have shown you so far how to break apart a variable that has the sub-variables of equal lengths and without the commas and quotation marks. What good is that, you say.

I promised that the methods I would show you would be equal or superior to the READ, DATA, RESTORE methods, you say.

We must crawl before we walk. All this stuff was preparatory for,

THE BIG SECRET !!!

And here it is. Observe, if you will, the following program:

Program #5

10 LET A$ = "PENNY,NICKEL,DIME,
QUARTER,4 BITS,DOLLAR,FIN,
SAWBUCK,DOUBLE SAWBUCK,
C-NOTE,"
20 LET M = 1
30 FOR N = 1 TO LEN A$
40 IF A$ (N TO N)=","THEN GOSUB
100
50 NEXT N
60 STOP
100 LET B$ = A$ (M TO N - 1)
110 PRINT B$
120 LET M = N + 1
130 RETURN

Variable (M) is the counter that finds the first character of each item. The comma is the Separator that is located by the routine in line 40. Sub-Routine 100 breaks out each item from between the commas and assigns it to string variable B$. In line 120, the new position is established for the first character of the next item.

And there it is. The secret is at last unveiled. Now you can convert all those Adventure and Star Trek programs for your ZX-81, can’t you.

The 16K program on cassette, BIORYTHM is an example how one can use these techniques to create a DATA dump with a program of relatively small size and using the same string variable, (A$), for two different sets of Data items, the names of the months in line #140 and then re-asigning (A$) to contain the amounts of the days in each month in line #450.

Study the listing of this program to see how these items are extracted from variable (A$) and put in their proper positions.

Stay tuned next month when we shall study how to create a dummy memory map for the CRT screen like the one used in the program, BOUNCING BOMBS.

Study what you learned today because there will be a test.

CLASS DISMISSED

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