How BBC BASIC Stores Your Programs
By Tim Surtell
When you type a program into the BBC BASIC interpreter on your Notepad or Notebook, the lines are not stored exactly as you have typed them. When you press ENTER after typing a line, the interpreter reads the line and searches for any BASIC commands. For each command it finds, it replaces the word with a single byte 'token' for that command. Each command has a unique token so that BASIC does not get confused.
A list of the tokens can be found on the BASIC Commands Index page.
Replacing each command with a token has two advantages for BBC BASIC:
Also, the first byte in each line tells the interpreter how many bytes there are in the line. This means that the maximum number of characters in a line is 255. The end of a line is always marked with the code 13, which is the CARRIAGE RETURN character. The end of the program is marked by having the total number of bytes in the line equal to 0 and the two following bytes equal to 255. The diagram below illustrates all of this:
As an example, consider the following two line program :
10 PRINT"Ten second timer"
When typed in, this is stored as follows:
Note that there are two tokens for the command TIME : one for when it is left of an operator (TIME=0) and one for when it is right of an operator (UNTIL TIME>1000). Note also that there are two unneccasary spaces in the program. These, and other which you would not be able to remove when typing in a program, can be removed using the compacter program below.
BBC BASIC compactor
This program can be run after you have finished writing a program to compact it so that is uses less memory. The program does several things to save memory :
COMPACT.TXT ... 4kb
Running the program
The program uses the PAGE variable which specifies the memory address where the first byte of your program will be found.
Because you will need to change PAGE, BACKUP ANY FILES ON YOUR MACHINE BEFORE RUNNING THE PROGRAM.
Differences between the NC and the BBC Micro
Although the NC uses the same tokens for commands as the BBC Micro does, programs written on the BBC Micro will not load on the NC because the order of the line number and number of bytes is different, as are the codes at the end of a program . Figure 3 shows the differences:
Compacter program taken from Advanced programming techniques for the BBC Micro by Jim McGregor & Alan Watt, page 342. ISBN : 0-201-14059-4