Unit 10: Keyword Roundup (II)

(A) More about filing

The BBC BASIC keywords which still remain to be covered in our Tutor are concerned principally with maths and filing. Elementary filing techniques were covered in Unit 2, but these were for saving whole files, which could then be loaded back into the program for update. In Unit 10 we will refine and build on these routines by exploring ways of getting into a file in order to record a single byte, or retrieve a single byte, using the keywords BPUT#, BGET#, EOF#, EXT# and PTR#.

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.


If you have by now revised the filing material in Unit 2, we can make a brief historical recap here about filing on the BBC Micro, for four reasons:
  1. New skills require plenty of varied practice, and the classic reference manuals and textbooks constitute a ready source of materials for study.
  2. The online manual for BBC BASIC(86), on this website, is intended to be read in conjunction with a standard BBC BASIC manual. As stated in the Introduction: "Only those features which differ from the standard Acorn versions are documented here." This means that for a full definition of filing keywords, you need to consult a BBC Micro Reference Manual. However, some key programs in these BBC manuals, which illustrated filing techniques, actually contain errors at critical points, causing the programs to fail. Unfortunately these errors are not mentioned in the lists of Errata prefaced to the manuals themselves. It will therefore be necessary to point them out and make them good as we go along.
  3. At the time the BBC Micro Model A was launched, the default filing device was the cassette recorder, and the DFS disk drive was not generally available until some two years later. The result of this delay was that even after the launch of the Model B, many amateur users, who found the technical language of the BBC Micro User Manual impossible to interpret without help, and who found it necessary to obtain a supplementary book, such as Ian Sinclair's widely popular Introduction to the BBC Micro (ISBN 0-246-12146-7), still got no help on disk filing techniques. This was no fault of Ian Sinclair, who himself noted, whilst writing the Introduction in mid-February, 1983, that the DFS disk drive, although widely advertised, was still not on the market. As a result, his classic textbook still treats the cassette recorder as the default filing device. There are no chapters on the routines for filing to disk. This means that only the really dedicated student of BBC BASIC will have gone one stage further and bought a book such as David Smith's BBC BASIC for Beginners (ISBN 0 86161 126 8), which contains good material on random access filing to disk. Since the student needs to consult the classic manuals, the above-mentioned errors will as far as possible be corrected as we go along.
  4. Since the 1980's, the filing routines designed for the BBC Micro have been overhauled and improved on by Richard Russell, both in BBC BASIC(Z80) for the Z88, and in BBC BASIC(86) for the PC, and there are key differences in such matters as the number of bytes used for storing numbers and strings, which in turn create differences in the use of the file pointer (PTR#). This means that many of the classic reference manuals need to be updated as far as PTR# is concerned, although the remaining classic filing routines are still useful for practice. The inherent differences will be explained as we go along.

Of bits and bytes

In order to make complete sense of the manuals and textbooks for the Beeb, we need to note at this point the relation of a byte to a "bit". What, first of all, is a "bit"? If you recall that Morse code - as a well-known specimen of a signalling code - is composed of dots and dashes, then binary code, the fundamental language of the computer, can be seen to be similarly composed of patterns of zero and one, in which one indicates that an electric current in a given location is on, and zero that it is off. But how does this relate to handling numbers, bytes and files?

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:

However, both BBC BASIC 86 for the PC and BBC BASIC(Z80) for the Z88 differ from the BBC Micro version of BASIC, in that they do not distinguish, in filing space, between "integer" and "numeric" variables, both of which, on the PC and on the Z88 versions, take 5 bytes, whilst strings take the number of characters in the string plus one byte, not two.

Errata in the classic manuals

Before proceeding, it would be well to correct the above-mentioned errors in the classic BBC Micro manuals, so that they can be used for additional study.

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:

30 INPUT "What is the drink called ",D$
40 PRINT#X, D$

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?

30 INPUT Y , R$

The omission, of course, is the hash sign in line 30. The line should read:


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:

30 INPUT#Y, A$

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

70 PRINT ' "What name did you give to "
80 INPUT "your data file",file$
90 that_one=OPENIN(file$)
100 count=0
120 count=count+1
130 INPUT#that_one,friend$(count), numb$(count)
140 UNTIL EOF#that_one
150 CLOSE #that_one
170 DEFPROCfind_number
180 CLS
190 INPUT "Whose number do you want?", name$
200 search=0
220 search=search+1
230 IF name$=friend$(search) THEN PRINTname$; "has the number";numb$(search)
240 UNTIL search=count OR name$=friend$(search)
250 IF name$<>friend$(search) THEN PRINT "I can't find this name"

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.


That said, if we now return to our marks program, there is a much quicker technique that can be used if the marks are stored as individual bytes on file. For this we need the BBC BASIC keyword BPUT#. Let us assume a tutorial group of five students. Their names will have to be listed in order, as permanent data, after the DATA statement in the program itself, to be retrieved by the READ statement later. We can use BPUT# to store separately the list of marks for each individual subject, and later retrieve them to be printed alongside the permanent list of names. Examination marks are usually recorded percentages, but if you use BPUT# to store other figures, you need to note that these can only be whole numbers in the range 0 to 255, or, in the case of strings, a single character. Note that we have used a loop, because BPUT# stores each single byte separately. Let us assume at this point a group of five students:

10 DIM M(5)
20 FOR J%=1 TO 5
30 INPUT M(J%)
60 FOR J%=1 TO 5
70 BPUT#X,M(J%)

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:

20 FOR J%=1 TO 5
30 G=GET

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:

20 FOR J%=1 TO EXT#Y
30 G=GET

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:

30 G=GET

PTR# - the File Pointer: origins and update

The above examples are concerned with the storage and retrieval of a list of data. Suppose, however, we want to retrieve single items, such as one student's mark in a test, or an individual telephone number in an address book program? For this we need the keyword PTR#, which can set the data pointer to the location in the file that we want. The easiest way of illustrating the function of the file pointer is to think of the action of the mouse in a Windows application: as you move the mouse, the pointer on the screen moves from one item of data to the next. This is may look like a poetic "image" of the pointer, but it is a real life illustration, since the electrical impulses generated by the turning ball of the mouse on the mousepad are converted within the computer into digital signals, which are pre-programmed to locate data items in RAM or on disk.


How do we pinpoint the location in a file in which an item of data is recorded? The short answer is by counting the bytes, or units of memory. Each byte holds a limited amount of information, such as a number from 0 to 255, and the pointer counts the number of bytes required to store each data item on disk. In the version of BBC BASIC on the BBC Micro, as stated above, each numeric variable was stored in a total of 6 bytes, each integer variable (e.g. J%) in a total of 5 bytes, and each string variable in a variable total of bytes, formed by adding 2 to the length of the string. This has to be borne in mind if you study programs in classic manuals or textbooks which illustrate the use of PTR#.

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%
80 FOR J%=0 TO 19
100 NEXT

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:

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
110 PRINT' "Press any key to continue "
120 G=GET

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.

Appendix on filing for Z88 Owners

Z88 owners might like to consider a highly practical use of BPUT# as a set of printer-driver functions within a program, to control such printer attributes as italics, underlining and bold print in the output from a BBC BASIC(Z80) program to the printer. You will recall from Unit 3 that output is directed to the printer as if it were a file. Herewith a small selection of BPUT# statements for printer attributes:


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

(B) Mathematical Keywords

It is perhaps with mathematical keywords that the computer as a labour-saving device properly comes into its own. Programs can be set up for carrying out routine operations with greater speed and accuracy than was possible before the invention of the computer. For users of BBC BASIC 86, the mathematical keywords below will be of great benefit to students of GCSE Maths and beyond.

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 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:

30 D=ABS(X-Y)
50 D=ABS(Y-X)

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)

Trignonometric functions:

SIN calculates the sine of a given angle, provided the angle is expressed in radians rather than degrees. If the latter is given, the formula is: X=SIN(Y) where Y is the angle in radians. If the angle is in degrees, you can convert it using the keyword RAD, given below.

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:

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:

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:

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:

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:

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:

Logarithmic functions

If you are working with logarithms, there are a number of very useful labour-saving functions set out below:

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:

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:

EXP is a function which works out the value of e (see under LN above) raised to any power you want:

(C) Graphics Keywords

As was indicated in Units 2 and 7 above, the use of graphics can be especially helpful both for adding the professional touch of colour and style to dialogue boxes, and for making geometrical shapes stand out from each other. However, with regard to the ratio of the time and effort required by study to the practical advantage of the results, we are clearly in a very different position now from that of the novelty of computer graphics, which the BBC Micro made accessible to a wide market of users, when it first appeared. Whilst the topic will certainly merit intensive study on the part of the enthusiasts who will be celebrating the twentieth anniversary of the launch of the BBC Micro in 2002, nobody now, in the view of the present author, could really claim that the chapter heading of "high resolution graphics", in the old manuals, still holds good, since the Beeb, with its 32K of memory, could not support the fine detail of modern graphics. The keywords involved will therefore be briefly defined, and the enthusiast for classic graphics can pursue the topic further by consulting a specialist manual (e.g. Steve Money: BBC Micro Graphics and Sound, ISBN 0-246-12156-4).

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."

A few odds and ends

Whilst we are revising, there are two further related keywords to mention:

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.


Although it was originally intended to produce this elementary tutor in BBC BASIC 86 for the PC in twelve monthly units, ten units have, in the event, turned out to be sufficient to complete it. The author hopes it has contributed to bridging the gap between the manuals and tutors produced for the now obsolete BBC Micro, BBC Master and Z88 on the one hand, and the online manual for BBC BASIC 86 on the other. This gap proved to be more problematic than expected, and the author apologises for the consequent delay in the publication of the later units.

Left UNIT 9


Best viewed with Any Browser Valid HTML 3.2!
© Edmund Burke 2000