9 Reading and Writing Data

The reader should now be familiar with the three types of data that can be manipulated using ATOM BASIC, namely:

  1. Words i.e. numbers between -2000 million and 2000 million (approximately).
    Storage required: 4 bytes
    e.g.
    variables A to Z
    arrays AA(1) ... etc.
    word vectors A!4 ...etc.
    indirection !A ...etc.
  2. Bytes i.e. numbers between 0 and 255, or single characters, or
    logical values.
    Storage required: 1 byte
    e.g.
    byte vectors A?1 ... etc.
    indirection ?A ...etc.
  3. Strings i.e. sequences of between 0 and 255 characters, followed by
    a 'return'.
    Storage required: Length+1 bytes
    e.g.
    quoted string "A STRING"
    string variable $A ...etc.

All these types of data can be written to cassette and read from cassette, making it very simple to make files of data generated by programs.

The ATOM BASIC functions and statements for cassette input and output are designed to be fully compatible with the disk operating system, should that be added at a later stage. When the disk operating system is used, several files can be used by one program, and the individual files are identified by a 'file handle', a number specifying which file is being referred to. Although this facility is not available when working with a cassette system, the file handle is still required for compatibility.

9.2 Output

To output a word to cassette the PUT statement is used. Its form is:

      PUT A,W

where A and W are the file handle, and word for output, respectively.

To output a byte to cassette the BPUT statement is used; the form is:

      BPUT A,B

where A is the file handle, and B is the byte for output.

To output a string the SPUT statement is used. The form is:

      SPUT A,S

where A is the file handle, and S is the base address of the string.

9.3 Input

To read a word from cassette the GET function is used. Its form is:

      GET A

where A is the file handle. The function returns the value of the word.

To read a byte the BGET function is used. Its form is:

      BGET A

where A is the file handle. The BGET function returns the value of the byte, and can therefore be used in expressions; for example:

      PRINT BGET A + BGET A

will read two bytes from cassette and print their sum.

To read strings the SGET statement is used. The form is:

      SGET A, S

where A is the file handle, and S is the base address where the string will be stored. The string S should be large enough to accomodate the string being read.

Note the difference between SGET, which is a statement, and the functions BGET and GET; SGET cannot be used in expressions.

9.4 Find Input and Find Output

The functions FIN (find input) and FOUT (find output) can optionally be called before inputting from, or outputting to, cassette. The functions are called with a null string as the argument, and they return the value 13; when used with a disk system the argument is the file name, and the value returned is the file handle.

The FOUT function is called as follows:

      A=FOUT""

and it will cause the message:

RECORD TAPE

to be printed, and the program will wait for a key to be pressed before continuing execution.

The FIN function is called as follows:

      A=FIN""

and it causes the message:

PLAY TAPE

to be printed, and again the program will wait for a key to be pressed. A dummy variable, such as A in this example, should be used to hold the file handle.

9.4.1 Data on Cassette

The following program prompts for a series of values, terminated by a zero, and saves them on a cassette tape. The first byte saved on the tape is the number of words of data saved.

    1 REM Data to Cassette
   10 DIM VV(20)
   20 N=0
   30 DO INPUT J
   40 VV(N)=J; N=N+1
   50 UNTIL J=0 OR N>20
   60 A=FOUT""
   70 BPUT A,(N-1)
   80 FOR M=O TO N-1
   90 PUT A,VV(M) 100 NEXT M
  110 END
Description of Program:
30-50     Input numbers
60        Warn user to start tape
70        Output number of bytes
80-100    Save values on cassette
Variables:
A - Dummy file handle
J - Temporary variable for values input
M - Counter
N - Counter for number of values
VV(0...20) - Array of numbers

The next program reads the values back in and plots a histogram of the values. The program automatically scales the values if they are too large to fit onto the screen.

    1 REM Plot Histogram from Cassette
   10 DIM VV(20)
   20 A=FIN""; N=BGET A
   30 FOR M=0 TO N
   4O VV(M)= GET A
   50 NEXT M
   60 REM X=Maximum, Y=Minimum
   70 X=VV(0); Y=VV(0)
   80 FOR M=1 TO N
   90 IF X<VV(K) THEN X=VV(M)
  100 IF Y>VV(M) THEN Y-VV(M)
  110 NEXT M
  120 S=(X-Y+63)/64
  130 REM Plot Histogram
  135 CLEAR 0
  140 FOR M=0 TO N
  150 MOVE 0,M
  160 DRAW ((VV(M)-Y)/S),M
  170 NEXT M
  180 GOTO 180
Description of Program:
20-50     Read values into array
70-110    Find maximum and minimum values in array
120       Calculate scaling factor
140-170   Plot scaled histogram 
180       Wait for ESC key.
Variables:
A - Dummy file handle
M - Counter
N - Number of values in array
S - Scale factor for array
VV(0...20) - Array of values
X - Maximum value
Y - Minimum value

9.5 Reading and Writing Speed

When writing data to the cassette it is important to remember that the program reading the data back will not be able to control the cassette; it will have to read the data before it has passed under the tape head. If the program to read the data will spend a substantial time between reading, it may miss bytes passing under the tape head unless a delay is inserted between bytes when writing to tape.

As a general guide, the program to read the data should take no longer to read each byte than the program to write the data takes to write it.

9.6 Animal Learning Program

The following program illustrates how a computer can be 'taught' information, so that a 'database' of replies to questions can be built up. The computer plays a game called 'Animals'; the human player thinks of an animal and the computer tries to guess it by asking questions to which the answer is either 'yes' or 'no'. Initially the computer only knows about a dog and a crow, but as the game is played the computer is taught about all the animals that it fails to guess.

The program uses the cassette input/output statements to load the database, or tree, from cassette at the start of the game, and to save the enlarged database at the end of the game.

First create a database by typing:

GOSUB 9000;

and record the database on a cassette. Then RUN the program and load the database you have just recorded. When the reply 'NO' is given to the question 'ARE YOU THINKING OF AN ANIMAL' the program will save the new, enlarged, database on cassette. Also given is a sample run which was obtained after several new animals had been introduced to the computer.

    1 REM Animals
   10 REM Load Tree
   20 F=FIN""
   23 DO UNTIL BGET F=#AA
   25 FOR T=TOP TO TOP+GET F
   30 ?T=BGET F; NEXT T
   35 DO X=TOP
   40 PRINT'"ARE YOU THINKING OF AN ANIMAL"
   45 GOSUB q
   48 IF Q=0 THEN GOSUB z; END
   50 DO PRINT $X+1
   60 GOSUB q
   65 P=X+LENX+1+Q; X=!P+TOP
   70 UNTIL ?X<CH"*"
   75 PRINT"IS IT "$X
   80 GOSUB q
   85 IF Q=4 PRINT "HO-HO";UNTIL 0
   90 DO INPUT"WHAT WERE YOU THINKING OF"ST
   95 UNTIL LEN T>2
   98 L=T; GOSUB s
  100 PRINT"   TELL ME A QUESTION " 
  110 PRINT"THAT WILL"'"DISTINGUISH " 
  120 PRINT "BETWEEN "$L" AND " $X ' 
  130 $T="*"; R=T+1
  140 INPUT $R; !P=T-TOP; GOSUB s
  145 K=T; T=T+8; GOSUB j
  150 GOSUB q
  160 K!Q=X-TOP; K!(4-Q)=L-TOP
  170 UNTIL 0
 1000qINPUT $T
 1010 IF ?T=CH"Y"THEN Q=4; RETURN
 1020 IF ?T=CH"Q"THEN END
 1030 Q=0; RETURN
 2000j$T=$R; A=i
 2010 DO A=A+1
 2020 V=T?(A+4); ST+A+4=""
 2030 IF $T+A=" IT "UNTIL 1; GOTO k
 2035 T?(A+4)=V
 2040 UNTIL A=LEN T-5
 2100 PRINT"WHAT WOULD THE ANSWER BE"'
 2110 PRINT"FOR " $X
 2120 RETURN
 2150kT?(A+4)=V; $T+A+1=""
 2160 PRINT $T,$X,$T+A+3
 2170 RETURN
 3000sT=T+LEN T+1; RETURN
 9000 REM Set-Up File
 9010 T=TOP; ST="*DOES IT HAVE FOUR LEGS"
 9015 GOSUB s; P=T; T=T+8; !P=T-TOP
 9020 $T="A CROW"; GOSUB s; P!4=T-TOP
 9025 $T="A DOG"; GOSUB s
 9100zREN Save Tree
 9110 F=FOUT "1'
 9112 BPUT F,#AA; WAIT
 9115 PUT F,(T-TOP-1)
 9120 FOR N=TOP TO T-1
 9130 RPUT F, ?N
 9140 NEXT N
 9150 RETURN
Description of Program:
20-30     Load previous tree
23        Look for start flag
35        Reset X to top of tree
50        Print next question
70        Carry on until not a question
75        Guess animal
90-95     Wait for a sensible reply
98        Find end of reply
1000-1030 q: Look for Y, N, or Q; set Q accordingly
2000-2120 j: Look for "IT "in question and print question with "IT" 
          replaced by name of animal.
3000      s: Move T to end of string $T.
9000      Set up tree file
9100      z: Save tree file.
Variables:
F - Dummy file handle
K - Pointer to addresses of next two branches of tree
L - Pointer to animal typed in
P - Pointer to address of next question or animal.
Q - Value of reply to question; no=0, yes=4.
R - Pointer to question typed in
T - Pointer to next free location
X - Pointer to current position on tree
Program size: 1254 bytes
Additional storage: as required for tree.
Sample run:
>RUN
ARE YOU THINKING OF AN ANIMAL?Y
DOES IT HAVE FOUR LEGS?Y
CAN YOU RIDE IT?N
DOES IT HAVE STRIPES?N
IS IT A DOG?N
WHAT WERE YOU THINKING OF?A MOUSE
   TELL ME A QUESTION THAT WILL
DISTINGUISH BETWEEN A MOUSE AND A DOG
?DOES IT SQUEAK
DOES A DOG SQUEAK?NO
ARE YOU THINKING OF AN ANIMAL?Y
DOES IT HAVE FOUR LEGS?Y
CAN YOU RIDE IT?N
DOES IT HAVE STRIPES?N
DOES IT SQUEAK?Y
IS IT A MOUSE?Y
HO-HO
ARE YOU THINKING OF AN ANIMAL?N
RECORD TAPE
>

Next chapter