READ-DATA-RESTORE for Sinclair ZX-81
by David Nowotnik
from Home Computing Weekly, 28 June - 4 Jul 1983

[The routine was reprinted in ZX COmputing, February 1987]

Soon after the launch of the ZX81, there was a rumour circulating that the
BASIC commands READ, DATA, and RESTORE were included on the original design
specification of the ZX81, but these has to be dropped through lack of
space on the 8K ROM.

While the omission has not caused many problems to programmers, there are
circumstance when their absence has meant that valuable RAM space cannot be
used economically.

One example is in filling a large array with data stored in a program.
Without READ/DATA, the usual method is to have one LET statement for each
element of the array - and that can consume a lot of space!

To overcome this problem, I wrote two machine code routines to imitate READ
and RESTORE.

In most other computers, READ works by searching through a program from the
beginning until a DATA line is found. The first number or word after DATA
is read, and stored for future use as a variable. The next time READ is
used, the next number or word is read, and so on.

A DATA line can contain several words or numbers; each item is separated by
a comma. Once you come to the end of a DATA line, READ will search for the
next DATA statement in a program. If you try to READ more items of data
than are present, you should get a system error message. RESTORE resets the
data search 'pointer' to the beginning of the program.

My routine works in much the same way. Instead of commands READ and
RESTORE, numeric variables with these names are declared which hold the
addresses if tge appropriate machine code routine. RESTORE is mimicked by
the program line:

RAND USR RESTORE

Thre are two versions of the READ command, one for numeric, and one for
string data. Typical examples of these are:

string: 100 LET C$=A$(TO USR READ)

numeric: 100 LET C=VAL A$(TO USR READ)

Data is stored in a psecial REM line. To differentiate it from a normal
REM, REM is followed by a graphic shift A (CHR$ 8). The machine code
routine recognises this combination as a DATA command.

Line 9999 contains a REM command followed by an inverse space character
(CHR$ 128). This allows the routine to recognise the end of the program.
Should you try to READ more data than is present, the machine code routine
recognises the end of the program marker, and prints the message 'OUT OF
DATA ERROR' at the base of the screen.

An automatic RESTORE then occurs to prevent the program crashing.

The combination of CLEAR and DIM A$(32) ensures that A$ is the first
variable in the variables storage area of RAM.

The machine code depends on this; it uses A$ as a temporary store. A$
shoudl be dimensioned to at least the length of the longest DATA item.

Note that you can mix numeric and string data in one data line. Unlike the
Spectrum, quote marks are required for string data.

[The first item does not have to be the number of data items, which is only
required for the example program presented. If there are no data items in
the program then an error message is displayed and an infinite will occur
because the routine attempts to wrap back to the beginning of the program
and search for the first data item. If an out of data condition occurs then
an error message is displayed but the routine assumes that an expanded
display file exists, i.e. there is 3.25K or more of RAM present, and so
corruption of the variables area will occur if this is not the case, e.g.
if the SCROLL command has been used.]
