
In past issues, we have read articles on how to simulate the commands READ, DATA and RESTORE. Many of us have never used the real thing and wonder what the hub-hub is all about.
We have been led to believe that a BASIC program executes itself with the computer reading program lines by the numerical line numbers starting with the lowest number and executing the commands in that line and then moving on to the next number and so on. The program will continue to do this unless a GOTO or GOSUB command is encountered which tells it to jump to another higher or lower line number.
This has a certain amount of truth to it but in actuality, the computer is aware at all times of every line and everything that is in that line. There are commands which can demonstrate this effect. The following program is such an example:
10 INPUT "Do you want to
restore? ";a$: IF a$(1)
="y" THEN RESTORE
20 READ a$: PRINT a$
30 GOTO 10
40 DATA "APPLE","BANANA",
"CHERRY","DATE","EGG",
"FEMALE","GOD","HAIR",
"JUMPER"
If we observe the listing of the program and follow the logic, we can see that at no time is line 40 going to be executed. One may argue that the READ command acts as a GOSUB but this is not true – there is no line number destination for READ to identify with. The DATA line can be anywhere in the program – at the beginning, end or anywhere in between. If the DATA line is encountered during normal execution, it is ignored the same way REM is ignored. The line is simply skipped over.
If we RUN this program and respond to each prompt with the ENTER key being pressed, the words in the data line will be displayed one at a time. If we enter “yes” for the prompt, the next displayed word will be the first in the DATA line because the DATA pointer will be RESTOREd to that position.
If we wished to put numbers in a DATA line, we would leave out the quotation marks and have line #20 changed to:
20 READ a: PRINTa
More than one DATA line can be entered but the items in the lines are treated as if they were all in one big line and in order as they appear in the program.
Uses for READ, DATA and RESTORE are many, but one of the most common uses is to POKE a machine language routine into memory. Let us say that we had a routine which involved certain numbers to be put into certain memory locations. Let us say that the numbers were 156, 32, 222, 118, 255, 7, 45, 2, 89 and 201 (not a real machine language routine) and they were to be put in consecutive memory locations starting with location 30,000. Now with the Timex computers, we could put these numbers into a variable array,and POKE them into memory with the following routine.
10 DIM A(10)
20 FOR N=1 TO 10
30 INPUT A(N)
40 NEXT N
50 FOR N=1 TO 10
60 POKE 29999+N, A(N)
70 NEXT N
If we ran this program, we could input the numbers and after the last one was entered, the routine would be POKEd into memory. If we then entered RAND USR 30000, the machine language program would execute.
The disadvantage of this method is that we cannot directly observe the numbers in the program listing. Therefore, we might write the routine in such a manner where the numbers are held in a string variable such as described in previous articles regarding the simulation of READ, DATA & RESTORE. The disadvantages of these methods is that a more complicated routine has to be written to split the string variable apart into the proper data item segments. The above numbers have 1, 2 and 3 characters which would make a routine take up a lot of program lines and space. We could convert these numbers to hexadecimal which would mean that each one would occupy exactly 2 spaces and a routine to POKE them into memory would be easy to write. However, a routine to convert the numbers into hexidecimal would be something else.
But with READ, DATA and RESTORE, all you need is:
10 DATA 156,32,22,118,255,
7,45,2,89,201
20 FOR n=1 to 10
30 READ a: POKE 29999 + n, a
40 NEXT n
This represents an easy and efficient method of putting a machine language program into high memory. It is also a lot easier for the programmer to reference the DATA numbers to a mnemonics chart to see their functions than it is to study the gobbledeegook held in a REM statement such as the methods commonly used with the TS-1000.