With regard to the remaining keywords, some such as ADVAL, which operate between BBC BASIC and a peripheral device such as a games paddle, and which were a popular feature of early books for the BBC Micro, may depend now on whether you have a suitable interface card fitted to your PC. Equally it cannot be taken for granted that the sound system linked to BBC BASIC, in any given PC, will be anything other than a tiny monitor speaker, which warbles the dialling tone when you go online. Sound, therefore, will not be covered.
At this point it would be helpful to revise the elementary filing techniques discussed in Unit 2. These techniques dealt with saving and loading complete files both from and into a program. Veterans of the BBC Micro should also at this point revise filing techniques from the manuals, although there are clear differences between BBC BASIC on the Beeb and BBC BASIC(86) for the PC. These will be pointed out as we go along. One thing that can be dispensed with, in making comparisons with the BBC Micro, is the business of becoming expert in the use of "sequential files" before proceeding to "random access files". To cut a long story short, the BBC Micro was equipped to use three different filing devices: a cassette recorder, a disk drive and a network system (we will ignore the network system at this stage). For cassettes, you recorded and accessed files in sequence, and for each file you made a note of the turns counter number. This, in general terms, is known as a sequential filing system. A disk drive, however, is like a CD player, in that the read/write head within the machine can be moved over the surface of the disk to the point where a file begins: in other words, you have random access to your files. This will form the focus of our attention in reviewing and extending filing techniques.
To answer this you need to revise, or learn, the concept of a base in arithmetic. Take the figure 100 in the everyday decimal system. If you start counting from 1, you can write every number up to 9 in one column. But as soon as you reach 10, you put the figure 1 in the tens column, to denote one lot of ten, and you put zero in the units column. Then when the tens column exceeds 9 by one figure, you enter the new figure 1 in the hundreds column to denote one hundred. In binary notation, however, we work to base 2: you can have 0 or 1 in the first column, but as soon as you add up your 1's to reach 2, you use the figure 1 to record one lot of 2 in the next column to the left and put 0 in the column that has been emptied, and so on through the columns. Since, in the world of computer electronics, the switch position "on" is represented by the figure 1 in binary code, and the switch position "off" by 0, it follows that banks of on-off switches in silicon chips can form the electronic coding basis of vast and complex calculations and messages.
So far so good - but how does a "bit" relate to a "byte"? A byte is eight bits: the minimum number of bits that the computer can handle as a unit of memory. If you want to dig deeper than this and see how binary code can actually make up a character of the alphabet, then consult one of the best classic textbooks, John Smith's BBC BASIC for Beginners (ISBN 0 86161 126 8).
Another classic textbook which contains a great deal of minute analysis of the hardware and software of filing techniques is R.I.M Sadek's The Complete Disc Manual for the BBC Microcomputer (ISBN 0-333-40930-2). This contains some extremely useful programs, detailing both serial filing and random access filing in BBC BASIC. However, the point of departure for the work was the Intel 8271 floppy disc controller, which Sadek acknowledged to be obsolescent at the time of writing. And of course the BBC floppy disk drives themselves are also now obsolete. That said, the specimen filing programs are worth detailed study. In revising classic textbooks, therefore, we must note that on the BBC Micro, as stated above, there were three kinds of variable, each of which needed a different number of bytes to file it. These were as follows:
Herewith, firstly, the INPUT program from Section 31, on File Handling, from the BBC Micro User Guide for the Model B+. The INPUT program itself is correct, but is needed here to illustrate the error in the subsequent retrieval program:
10 X=OPENOUT "DRINKS"
20 REPEAT
30 INPUT "What is the drink called ",D$
40 PRINT#X, D$
50 UNTIL D$= "STOP"
60 CLOSE# X
Clearly the user is intended to enter Whisky, Vodka, Gin, Rum, etc., after the prompt in line 30. Herewith the corresponding program, as printed on p. 165 of the User Guide, in which there is an error. Can you spot it?
10 Y=OPENIN "DRINKS"
20 REPEAT
30 INPUT Y , R$
40 PRINT R$
50 UNTIL R$= "STOP"
60 CLOSE#Y
The omission, of course, is the hash sign in line 30. The line should read:
30 INPUT#Y,R$
Without it, the program crashes. However, take a look at the improved version of this
program on the next page of the manual, which
(a) uses the EOF# ("End Of File") keyword to obviate the necessity of typing in STOP
and
(b) puts in the hash sign where it belongs in line 30:
10 Y=OPENIN "DRINKS"
20 REPEAT
30 INPUT#Y, A$
40 PRINT A$
50 UNTIL EOF#Y
60 CLOSE#Y
Admittedly, the above program is a schoolroom illustration of filing for the sake of filing: nobody would ever need it for a practical purpose. However, the Welcome Guide for the BBC Master contains two "real life" programs for creating and using a file containing the names and telephone numbers of friends. The program below is the second of a series of two programs, designed to create and use such a list. Here, alas, there is another line missing, this time in the Welcome Guide, at the point where the program moves from page 87 to page 88. Can you work out what the line should be? (The page break in the Welcome Guide is shown as a continuous line below):
10 MODE 135
20 PROCread_file
30 PROCfind_number
40 END
50 DEFPROCread_file
Clearly line 60, which ought to be the last line on p. 87 of the Welcome Guide, is missing.
By extrapolation from line 30, and from the previous program, which has
PROCmake_file in line 30, the missing line should read:
60 DIM friend$(100), numb$(100)
Once this has been inserted into the program, and the program runs, the vast potential of
database routines begins to open up for the student. (There is an excellent book by A.P.
and D.J. Stephenson, in the same Granada series as Ian Sinclair's Introducing the
BBC Micro, entitled Filing Systems and Databases for the BBC Micro:
ISBN 0-246-12423-7).
For this, however, we need to explore how our filing keywords can save labour. So let us return to the keywords READ, DATA and RESTORE in our program designed to record marks for a school class in Unit 6. The key practical point in this program was that since a given class remained constant, whilst lists of marks varied, the class list could be used for a READ...DATA routine, whilst the individual marks could be INPUT into the program, the whole then being filed with the PRINT# command.
10 DIM M(5)
20 FOR J%=1 TO 5
30 INPUT M(J%)
40 NEXT
50 X=OPENOUT "MARKS"
60 FOR J%=1 TO 5
70 BPUT#X,M(J%)
80 NEXT
90 CLOSE#X
Obviously, in lines 1 to 40 you enter the marks, whilst lines 50-90 file them, each as a separate figure. To confirm this, you can use BGET# to retrieve each mark as an individual byte, and G=GET to list each one individually, as follows:
10 Y=OPENIN "MARKS"
20 FOR J%=1 TO 5
30 G=GET
40 M=BGET#Y
50 PRINT M
60 NEXT
70 CLOSE#Y
The above program depends, for its successful operation, on knowing the number of marks in the list, so that the loop can be set at five passes. But suppose we are not sure of the number of marks? In that case we can use the keyword EXT#, which sets the pointer at the extent of the file, as follows:
10 Y=OPENIN "MARKS"
20 FOR J%=1 TO EXT#Y
30 G=GET
40 M=BGET#Y
50 PRINT M
60 NEXT
70 CLOSE#Y
Alternatively, if we don't know the extent of the file, we can use a REPEAT...UNTIL loop, and terminate it when the program reaches the end of the file - which, appropriately enough, is identified with the keyword EOF#, (END OF FILE) as follows:
10 Y=OPENIN "MARKS"
20 REPEAT
30 G=GET
40 M=BGET#Y
50 PRINT M
60 UNTIL EOF#Y
70 CLOSE#Y
In this Tutor, however, examples of the use of PTR# will take advantage of the fact that data items stored with BPUT#, and read with BGET#, each occupy a single byte in the file, irrespective of the version of BBC BASIC in use. Herewith two programs, for recording and retrieving marks for a tutorial group in which the students are numbered from 0 to 19. The first program records each mark as a single byte:
10 DIM MARK(20)
20 FOR J%=0 TO 19
30 CLS
40 PRINT' ' "Enter the mark for candidate number ";J%
50 INPUT MARK(J%)
60 NEXT
70 X=OPENOUT("MARKS")
80 FOR J%=0 TO 19
90 BPUT#X,MARK(J%)
100 NEXT
110 CLOSE#X
To retrieve any given mark from the file, as a single byte, we use BGET#, as above. However, which byte do we want? How do we select it? Since the students in the group are numbered 1 to 20 (but starting from 0 as 0 to 19), and each number is stored in 1 byte, then the file pointer PTR#Y in line 70 is moved along by an amount equal to the student number:
10 REPEAT
20 Y=OPENUP("MARKS")
30 CLS
40 PRINT' ' "Enter the number of the record you want, starting from 0 "
50 INPUT number
60 IF number>19 THEN CLS:PRINT' ' "That is not a valid student
number":END
70 PTR#Y=number
80 mark=BGET#Y
90 CLS:PRINT' ' "The mark for student number ";number;" is ";mark
100 CLOSE#Y
110 PRINT' "Press any key to continue "
120 G=GET
130 UNTIL FALSE
The program above locates the file pointer by means of a simple calculation. But the fact that it does so points towards more complex possibilities, in which names of varying length can extended by blanks to fill a uniform number of spaces, by means of the string-slicing keywords discussed in Units 3 and 5. The whole can then be encompassed in user-defined functions, using the keyword FN. This topic, however, belongs to a somewhat higher level of study than that covered by the present Tutor. It is excellently illustrated in A.P and D.J Stephenson's Filing Systems and Databases for the BBC Micro, mentioned above.
Underline: | BPUT#prt,5:BPUT#prt,66 |
Bold: | BPUT#prt,5:BPUT#prt,88 |
Italics: | BPUT#prt,5:BPUT#prt,76 |
These statements input printer codes as individual bytes to the printer "file", and are prefaced to whatever PRINT#prt line you then type in, as discussed in Unit 3. To review the full range of possibilities, consult D.J. Mounter's The BBCBASIC(Z80) Reference Manual for the Z88, ISBN 1 871895 00 6
The use of procedures and functions was detailed in Units 3 to 5 above, and the student who knows his/her way around mathematics will find that the following mathematical specialist keywords, once mastered, can be incorporated into both procedures and functions. The keywords are listed alphabetically at the outset, and are then grouped under headings. The reason for this is that Richard Russell's online Manual for BBC BASIC 86, when originally written, was intended to supplement the existing BBC Micro/Master manuals, and so did not duplicate the standard literature. Since the BBC machines, and their manuals, are now commercially obsolete, the full list is given below, and not just those which differ from the now classic manuals. The list itself is given in alphabetical order, although the keywords themselves are grouped according to convenience:
ABS, ACS, ASN, ATN, COS, DEG, EVAL, EXP, INT, LOG, LN, PLOT,RAD, SIN, TAN.
ABS is a function which gives the absolute value of its argument. It converts negative numbers into positive numbers and can thus give the difference between two numbers, irrespective of which is the larger. The following program illustrate this: it gives the absolute value of D. Try it as it is, then try omitting the keyword ABS:
10 INPUT X
20 INPUT Y
30 D=ABS(X-Y)
40 PRINT D
50 D=ABS(Y-X)
60 PRINT D
INT converts a number with a decimal fraction into a whole number if you are working with integers. So if the whole number (Y) is 99.9, it will be rounded down to 99 if you type the following:
10 Y=99.9
20 X=INT(Y)
30 PRINT X
COS is another trigonometric function. It goes together with SIN and will calculate the
cosine, if you know the given angle in radians So if R=angle in radians and C is the cosine,
the formula will be:
C=COS(R)
TAN is the corollary of ATN, and enables you to work out the tangent of a given angle,
provided the angle is expressed in radians. So if X is the angle in radians, you find the
tangent as follows:
Y=TAN(X)
ASN (arc-sine) is used to calculate an angle, in radians, of which you know the sine. Thus
if R= angle in radians, and Y is the sine, the formula will be as follows:
R=ASN(Y)
ACS (arc-cosine) is similar to ASN and is used to calculate an angle, in radians, of which
you know the cosine, as in the following example, where R is the angle in radians and Y is
the cosine:
R=ACS(Y)
The computer can only work in radians, so if you need degrees for a practical job, where you need to use a protractor, then you will have to to cross-reference the ACS function with DEG, below. Conversely, if your homework in trigonometry requires you to work out sines, tangents, cosines, etc, where you are given an angle in degrees, you convert from degrees to radians using they keyword RAD, as detailed below.
ATN belongs to the same group as ACS and ASN, and enables you to work out an angle
in radians if you know the tangent. So if R=angle in radians and T is the tangent, the
formula will be:
R=ATN(T)
RAD converts an angle expressed in degrees into an angle expressed in radians, so as to enable you to incorporate it into the trigonometric functions detailed above. (A radian is about 57 degrees).
DEG forms the link between working in radians, which is the only option on the
computer, and working in degrees, which is handier if you are doing a practical job such as
joinery. Let us assume you have used the formula R=ATN(T) above. To convert to
degrees you will need the following:
D=DEG(ATN(T))
LOG will calculate the logarithm to base 10 of a given number. It saves you looking it up
in log tables. So if Y is your given number, you simply type:
X=LOG(Y)
LN is a function which will work out the natural logarithm to the base 'e' of its argument.
(The natural number 'e' is about 2.71828183). The formula is:
X=LN(Y)
EXP is a function which works out the value of e (see under LN above) raised to any
power you want:
Y=EXP(X)
PLOT is a statement used for placing points, drawing lines and drawing triangles. It is best first to illustrate what it does, then take a look at the syntax. Try the following program:
10 MODE 4
20 DRAW 500,0
30 PLOT 85,500,700
As you will have seen, the program draws a filled triangle. Lines 10 and 20 are a matter of
revision (the two figures which follow DRAW in line 10, measuring the distance to cover
along the X and Y co-ordinates respectively, were explained in Unit 2). The three figures
following PLOT consist, in order, of
1) the item in a menu of functions identified by number;
2) and 3) the X and Y co-ordinates respectively identifying the location at which the
function selected by the first figure is to be carried out. The menu of functions which
can be selected by the first figure will be found in the online Manual for BBC BASIC
86.
POINT can be used in games programs, to identify the colour at a certain point on the screen, specified by the X,Y co-ordinates. If , for example, you are moving the cursor as a player about the screen and it hits a patch of red, you can write a conditional branching line to determine the fate of the cursor-mover at that point. Let us assume you have pre- determined the value of the variable colour from the list available in section 4,1 of the manual, you might have some such line as:
300 colour = POINT(X,Y)
310 IF colour = 2 THEN PRINT "Miss a turn."
EOR is used in conditional branching where either A is true or B is true, but not if both A and B are true simultaneously. An illustrative example would be entry to a beginner's course in a foreign language for science students at university: if the entrant has either done (a) only a year's study at school and failed the exam at the end of it, or (b) has already taken the beginner's course at the university and failed it, then in either case they are eligible to join the beginners' course, but not if they have done both. So if we say that S=school mark, U=university mark and E(entry) = -1 if true, then the selection could be governed by the following formula:
IF S< 40 EOR U <40 THEN E=-1: PRINT "Entry permissible."
VPOS is another graphically orientated keyword. If you are roughing out a text for display and want to know how much space you have left, then PRINT VPOS will return the vertical position of the text cursor on the screen, from which you will know how many lines you have left.
UNIT 9 |