I finally got my hands on an IBM PC this summer. After having worked on a main frame for so long, I found the IBM to be a pleasant experience. I also discovered that the file system on the IBM was similar to that of the main frame–though much slower. Since I was still hacking away on my ZX 81, I decided to simulate a file system on the Sinclair. The following program is what I came up with.
Before getting to the program, I’ll answer the question some of you are probably asking: “What is a file?”. A file is a place to permanently store information. Programs such as text editors and checkbook balancers use files to store their text and data, respectively. A file is made up of many records, each of which contains information. The records are stored sequentially (i.e. back to back) in the file for easy access. As an analogy, think of a file as a page of a book. Each record would thus be a single line of text on that page.
The first part of the program is a machine code subroutine that is used to allocate space for each new record in the file. You’ll need 39 characters in the “REM” statement.
1 REM 123456789012345678901234567890123456789
10 LET A$="017,126,064,042,127,064,229,025,001,032,000,197,205,158,009,193,225,009,034,127,064,042,041,064,009,034,041,064,201,"
15 FOR N=16514 TO 16542
20 POKE N,VAL A$( TO 3)
25 LET A$=A$(5 TO )
30 NEXT N
Make sure to double check line 10 before running the program. After you have run it, delete lines 10-30, making sure not to delete line 1. Finally, enter the rest of the (BASIC) program below.
10 REM ***INITIALIZE***
15 LET WRITE=150
20 LET READ=200
25 LET TOP=16553
30 LET SIZE=32
35 DIM R$(SIZE)
40 LET REC=1
45 LET EOF=0
50 GOTO 5000
100 REM ***CHECK***
105 LET BOTTOM=16512+PEEK 16511+256* PEEK 16512
110 LET EOF=TOP+REC*SIZE>BOTTOM
115 IF EOF THEN LET REC=INT ((BOTTOM-TOP)/SIZE)+1
120 IF REC<1 THEN LET REC=1
125 LET POS=TOP+(REC-1)*SIZE
130 RETURN
150 REM ***WRITE***
155 FAST
160 GOSUB 100
165 IF EOF THEN RAND USR 16514
170 FOR R=POS TO POS+SIZE-1
175 POKE R, CODE R$(R-POS+1)
180 NEXT R
185 LET REC=REC+1
190 SLOW
195 RETURN
200 REM ***READ***
205 FAST
210 GOSUB 100
215 IF EOF THEN GOTO 240
220 FOR R=POS TO POS+SIZE-1
225 LET R$(R-POS+1)=CHR$ (PEEK R)
230 NEXT R
235 LET REC=REC+1
240 SLOW
245 RETURN
Save the program before running it so you have a clean copy to reuse in the future. To use the program, first run it. This will initialize all of the necessary variables. You now have two new commands and one function at your disposal.
1) WRITE command:
Set REC to desired record number (1,2,…), R$ to desired record content and execute via GOSUB WRITE.
2) READ command:
Set REC to desired record number (1,2,…), execute via GOSUB READ and desired record will be in R$.
3) EOF function:
Check for end-of-file condition while reading (i.e. IF EOF THEN STOP).
Here is a sample program to test your new commands:
5000 REM *** EXAMPLE ***
5005 PRINT "ENTER A STRING:"
5010 INPUT R$
5015 GOSUB WRITE
5020 LET REC=1
5025 GOSUB READ
5030 IF EOF THEN GOTO 5045
5035 PRINT R$
5040 GOTO 5025
5045 PRINT "THAT IS ALL..."
There are a wide variety of things you can do with this file system. You can write your own text editor, checkbook balancer, or anything else that needs to store information. I myself wrote a checkbook balancer in a couple of days; a program that otherwise would have taken much longer to write.
Here is a list of variables that are used by the program. You should not change any of these or you’ll cause the program to crash.
Variables: TOP, BOTTOM, SIZE, POS, R, WRITE, READ, and EOF.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.