Volume 2, Number 4 – September 1982

Page 3 of 36

Labels for Xtal BASIC

by Stephen Hope

This article describes how labels can be added to Xtal BASIC. These labels can be used instead of line numbers in such commands as GOSUB and GOTO. This helps in producing easy to read programs, and allows subroutines to be referenced by meaningful names. Subroutines and other sections of a program can be moved around without the chore of searching the complete program to find any subroutine calls, or other references. It is surprising how such a relatively simple extension to BASIC improves the readability and ease of modification of the code.

The basic (no pun intended) idea is to first change the BASIC interpreter so that it will ignore a label on a line, and then alter the routines for GOSUB, GOTO etc. so that they will work with line numbers or labels. The first thing to do is to find a suitable format for the labels. I finally settled on a string of alphanumeric characters which starts and ends with a single quote mark. The quote marks make it relatively simple to distinguish between a label and a line number or anything else, in, for example, an IF..THEN statement.​This will minimise the increase in execution time for the interpreter.

Next, a suitable point to patch the program. There is a section of code in Xtal which is used every time a statement finishes. This code checks for a keyword, a variable or a line terminator and executes the appropriate routine. Since all keywords are greater than 80H, any character less than this at the beginning of a line is assumed to be part of a variable name. Any illegal names for variables are detected later in the LET routine. Here I diverted the code to check for a single quote. If one is found, then my routine skips aver any characters between this and the next. Note that this particular patch allows a label anywhere on a line at the end of statement. Although labels will only be used if they occur at the beginning of a line, this allows comments to be embedded in a line, as long as they are enclosed in single quotes. A missing second quote gives a syntax error. Otherwise, the routine simply treats anything after the end of the label as the start of a new statement.

Finally, altering the GOTO and GOSUB statements. Here I again patched the original code in the interpreter. GOTO, GOSUB and RUN all use the same code to jump to a line number. The subroutine call to get the line number is changed so that it checks the first character after the keyword. If it is a quote, then it is assumed to be the start of a label. If not, something which is dealt with by the original routine. (This allows labels to be used with more complex types of statement, such as an IF statement, where almost anything is legal after the THEN

Page 3 of 36