UNIT 9: Keyword Roundup (I)

(A) General Keywords

At this stage in our study of BBC BASIC we have covered most of the keywords available for non-specialist, practical programming. A substantial proportion of the remaining keywords are for use in mathematics - the purpose, after all, for which the computer was originally devised. However, since not all practical computer users will be interested in the more technical aspects of maths, the keywords concerned will follow in the next unit, whilst most of the general keywords used for program management will be rounded up in this unit. A number of these general keywords, whilst appearing to be new, are in fact supplementary devices for speeding up familiar routines. A few of them are for the more technically advanced user, who understands the hardware of the computer sufficiently to be able to identify, and manage, programming details such as memory locations. These will be defined here, for the sake of completeness, but left to the specialist to pursue.

ASC

ASC is a reference tool: it enables you to access the ASCII code for any given character directly, instead of looking it up in a table. So if you want to know the ASCII code for A, you type : PRINT ASC ("A"). This will return the code number 65. The ASCII code will be necessary if you use the keyword CHR$ in, say, Mode 3. As a demonstration, try the following program sketch:

10 MODE 3
20 FOR J%= 60 TO 100
30 PRINT CHR$ J%;
40 NEXT

AUTO, NEW, OLD, CLEAR, TRACE and RENUMBER

These are editorial keywords for beginning or editing a program or recovering a previous program after starting afresh.
  1. NEW clears the decks for a new program by cancelling the programming "legacy" of the previous program.
  2. AUTO relieves you of the task of numbering lines manually by automatically numbering them 10,20,30, etc., from the beginning, unless you intervene and start, say, at line 7000 (e.g. for a procedure), in which case you type AUTO 7000, and the computer will then number the following lines automatically as 7000,7010,7020,etc.
  3. CLEAR disengages all the existing variable names, assigned to numeric or string variables, from the variables themselves, thus freeing the variable names for reuse.
  4. OLD allows you to recover a program which you happen to have "disconnected" when you typed NEW.
  5. TRACE is another editorial keyword which brings a piece of "backstage" machinery on to the stage itself when you are running a program: it "traces" the line number of whatever operation is current in the running of a program and prints it. Suppose you have a point where your program branches between two optional procedures, and you want to add a third. If you type TRACE ON at the beginning of the program, and then RUN the program, the line numbers in operation at each stage of the program will appear on the screen above your dialogue box. You could then print out your program, using VDU2, and TRACE will have highlighted the program lines you need to work on. TRACE OFF will then disable this editorial facility.
  6. Once you have made a number of incisions in a program and added extra lines between each standard interval of ten lines, RENUMBER will restore the default sequencing of ten-line intervals.

POS, VPOS and WIDTH

Whilst we are on the subject of editing programs, these three keywords are very useful for planning the layout of lettering in a dialogue box, where spaces have to be counted. (At this point it would be useful to revise the related keywords LEN and SPC on pp. 12-13 of Unit 2.) If you place the cursor at the point in the screen where you want to centre a line of text, for example, and type X=POS: PRINT X , then the function will return the position in spaces across the screen, where the left hand side is zero. This could be useful in matters of layout. Let us assume you are sketching the layout of a dialogue box in Mode 5, where the characters are fairly large. The following program will help you to space the characters of the strings you INPUT by showing the position of the cursor at the end of each line:

10 MODE 5
20 DIM N$(3)
30 INPUT N$(1)
40 INPUT N$(2)
50 INPUT N$(3)
60 PRINT N$(1);:PRINT POS
70 PRINT N$(2);:PRINT POS
80 PRINT N$(3);:PRINT POS

VPOS, similarly, will return the vertical position of the cursor, whilst if you say WIDTH 20 (or whatever figure you choose) it will allocate the number of characters in the line. An automatic new line follows when WIDTH has been reached by the cursor.

GOSUB, RETURN, STEP and TIME

Before we move on from the subject of procedures, we might take a look at the combination of GOSUB and RETURN. For this you will need to revise the matter of Setting Conditions and Making Choices in Unit 3. This unit concentrated on the task of using procedures without additional complications. Procedures are by far the most efficient way of dealing with subroutines, but the presence in BBC BASIC of GOSUB...RETURN gives the user access to an alternative method, which was put into the BBC Micro merely to provide compatibility with earlier versions of BASIC on other machines. If you wish to experiment with GOSUB, note that as with a procedure, it is best to place a subroutine at the end of a program - so give it a high line number - and to follow it with the keyword RETURN, which will return you to the point at which you left the main program. As an example, we could insert the following subroutine into the CYBER ROASTER program in Unit 7, so as to fine-tune the cooking of beef to rare, medium or well done. The sub-routine adds 5 minutes per lb. for well-done beef, and subtracts 5 minutes per lb. to cook it rare - but you can adjust it to your requirements. It works as follows:

Immediately after line 120 we insert:
125 IF M$= "B" THEN GOSUB 4000
Then at the end of the program we insert our subroutine:
4000 CLS
4010 INPUT ' ' "Do you prefer your beef rare, medium or well done (Enter R/M/W)",R$
4020 IF R$= "R" THEN M=15 ELSE IF R$= "M" THEN M=20 ELSE IF R$= "W" THEN M=25
4030 PRINT ' "Taking the specific cooking time for beef into account,"
4040 RETURN

Incidentally, it is recognised that opinions on the timing of beef can vary widely. If, in an emergency, you decided to override the default provision of an extra five minutes per lb for well done beef, and reduce it to four minutes, and furthermore check it manually at four minute intervals yourself, when the program had already run, you could use the keyword STEP in a loop to set the intervals at four minutes, and print them to screen, as follows:

10 FOR J%=30 TO 60 STEP 4
20 PRINT J%
30 NEXT

This would print out the list of four minute intervals to check on your clock. And whilst we are on the subject of time, you can, of course, make a clock of your own by using the TIME keyword itself. The sky is the limit for the application of this keyword, but essentially it counts centiseconds from a predetermined point entered on the keyboard. We have already met TIME$, which prints out the date and time as pre-programmed in BBC BASIC. But with TIME you can devise your own clock. To illustrate how it counts centiseconds, try the following program:

10 TIME=0
20 REPEAT
30 G=GET
40 T=TIME
50 PRINT T
60 UNTIL FALSE

This is obviously just a preliminary sketch of what can be done with the keyword. To make your own clock, you will need to revise DIV and MOD so as to move from centiseconds to seconds, seconds to minutes, etc.

DELETE, ERL and ERR

DELETE, followed by a single line number, will delete the line in question. Alternatively you can delete a block of lines by specifying the first and the last lines, and separating them with a comma, e.g. DELETE 20,90 will delete the whole program block between lines 20 and 90 inclusive. This is very useful for erasing, say, an entire procedure.

ERL, meaning "error line number", is intended to help the programmer find the line number where the last error occurred, whilst ERR is there to define the error. A convenient way to use both is to type in, early in the program:
10 ON ERROR GOTO 50000

Then at the specified line, enter:

50000 PRINT ERR
50010 PRINT ERL

This will both define the nature of the error itself, and number the line in which it occurs.

REPORT, OFF and STOP

REPORT can be used to reproduce an error message as it would be displayed by BBC BASIC, using the following:

PRINT: REPORT: PRINT " at line ";ERL

OFF will get you out of trouble if there happens to be an error in your error trapping routine itself! It will return control to BBC BASIC and terminate the program, as follows:

ON ERROR OFF

STOP has a similar action, within a program, to pressing the ESCAPE key when you are writing a program. It also has a similar action to END, in that it will terminate the program, but it then goes further and will then print the line number at which it has occurred. STOP can be used to indicate an abnormal exit from a program, perhaps because of a bug. Let's assume we're doing a program sketch, for inclusion in a larger program, which involves planning a budget that cannot be allowed to go into deficit. An erroneous deficit could be trapped by a line such as

IF balance <1 THEN STOP

GET$, LINE and INSTR

GET$, like GET, waits for you to press a key - but then goes further: it stores the character of the key pressed as a string, and this can be used in your program. Suppose you set up a dialogue box which requires the program user to answer "y" or "n" to a question. You could, of course, use INPUT, as follows:

200 INPUT "Do you wich to continue to the next level Y/N",answer$
210 IF answer$= "Y" OR answer$= "y" THEN PROCNEXTLEVEL

However, the alternative, using GET$, would be:

200 PRINT "Do you with to continue to the next level Y/N?"
205 answer$ = GET$
210 IF answer$= "Y" OR answer$= "y" THEN PROCNEXTLEVEL

LINE allows you more flexibility when handling an INPUT statement. If you input a line of text after the prompt INPUT LINE N$, and you then type the statement PRINT N$, it will print the entire line: strings, numbers, spaces, punctuation marks and all, as follows:

10 INPUT LINE A$
20 CLS
30 PRINT A$

Let us assume that in response to the prompt in line 10 you write the following:

"Boy, what's the square root of minus one?" thundered Wackford Squeers. SQR(-1), replied the boy.

Line 30 will print A$ verbatim, without attempting to perform a mathematical operation on SQR(-1).

INSTR searches for a string within a string, and reports on the position where the first character of string occurs in the left-to right sequence. Let us suppose you are designing a specialist word-processing application with built-in short cuts, enabling you to press a single key to print a given syllable. You need first of all to establish the most frequently occurring syllables. Suppose, as an example, you want to find how often, and where, the syllable ten occurs in tenacity, maintenance and bitten. Here is a short program which can do the job:

10 REM L$= WORD TO BE SEARCHED
20 INPUT L$
30 S$= "ten"
40 Z=INSTR(L$,S$)
50 PRINT Z

Each time we run the program, line 20 prompts for the INPUT of the word to be searched, and so, one after the other, we INPUT tenacity, maintenance and bitten. Line 50 returns the positions 1,5 and 4 in succession.

B) Specialist keywords

There are a number of keywords provided with BBC BASIC which effectively take the user beyond the skills that can be managed without technical knowledge, as in driving a car. They are essentially for the technical expert who is going to use machine code subroutines and is familiar with the relation between the hardware and the computer's memory map. A full description of these keywords lies beyond the scope of this introductory BBC BASIC Tutor for the non-specialist user. Among them are CALL, PAGE, USR, HIMEM , LOMEM and OSCLI:
  1. CALL is used to call a routine which has been pre-programmed in machine code.
  2. PAGE is a pseudo-variable which gives the address in memory where BASIC stores your program.
  3. USR is used to call sections of machine code program designed to return one value.
  4. HIMEM is a keyword used for setting the highest memory location that BASIC uses for the program and variables.
  5. LOMEM gives the address of the start of the BASIC variables.
  6. OSCLI (Operating System Command Line Interpreter) is a statement which passes a string to the operating system. (The non-specialist who wants to acquire a minimum grounding in computer science might like to try an excellent work by Randall Raus, in two slim paperback volumes: The Essentials of Computer Science, Research & Education Association, New Jersey, 1996).

Left UNIT 8

UNIT 10Right


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