The BBC and Master Computer Public Domain Library
BASIC IV ROM Routines
Page Last Altered:
8FAE Execute next command line / program statement
Submitted by Steve Fewell
This routine has many entry points (depending on how the last statement finished and what needs to be done to tidy up
before the next statement can be executed. The entry points are listed between square brackets ().
Therefore, this routine may not necessarily be executed in the order described.
[&8FAE] Skip the rest of the line. This routine is called when a DATA keyword is found (as DATA statements are skipped
when reached and not executed (as the READ command controls how they are handled). It is also called when the DEF keyword
is found (as function and procedure definitions are not directly executed until they are called by an FN or PROC keyword).
This routine is also called when a REM keyword is found, to skip the rest of the line - as it is a comment line.
Additionally, this routine is also executed when a '*' command is found (the '*' command is first passed to the Operating
System OSCLI routine and then this routine skips the line - so that BASIC doesn't try to execute the '*' command.
The rest of the line is skipped as follows:
* Set A to #&0D (the end of line character - '<cr>').
* Set Y to the BASIC Text Pointer A offset (&0A) - 1.
* Keep incrementing Y and checking the character at BASIC Text pointer A (&0B-&0C) plus Y (offset) with the
character in A (the '<cr>' character) until the characters match.
* Call routine &9BBC to update the BASIC Text Pointer A address (to include the Y offset value) and to reset the offset
(&0A) to 1. (basically, this adds Y to the Text Pointer A address (&0B-&0C)).
[&8FBD] This routine checks whether the last character read (in A) is '<cr>', if it isn't then jump to &8FAE
to skip the rest of the line.
Now, we are at the end of the line (i.e. we have reached the '<cr>' character).
[&8FC1] If the BASIC Text pointer MSB address is #&07 then there are no more program lines to execute (as we were
executing a statement from the command line - and not from within a program), so jump to &8F86 to prompt for the next
command line input.
Set Y to #&01.
Load the next character (offset Y) from the BASIC text pointer A location.
If the character is negative (i.e. #&FF) then we have reached the end of the program, so jump to &8F86 to prompt
for the next command line input.
[&8FDB] Set the BASIC Text pointer A offset (location &0A) to #&04 (i.e. the first character of the program
If the TRACE flag (location &20) is on (i.e. it's value is not zero), then:
- * Set IWA to the Line number (MSB byte (&2B) = the first byte at BASIC Text pointer A (offset 1) and the LSB byte (&2A)
- = the second byte at BASIC text pointer A location (offset 2)).
- * Call routine &9C4B to display the TRACE line number (in the IWA) on the screen.
line - after the line number and line length).
Jump to &900D to execute the statement at the BASIC Text pointer A location.
[&8FEB] Check for special start statement characters '*', '=', 'EXT keyword' and '['
Set Y to the BASIC Text pointer A offset (location &0A) minus 1.
Load the character at the BASIC text pointer A location (plus offset - Y) (this should be the first character of the
If the character is '*' then jump to &8FA4 to execute the '*'-command.
If the character is '[' then set the OPT flag (location &28) to 3 (default setting) and jump to &8920 to begin
If the character is '=' then jump to &9060 to deal with returning from a function and setting the return variable's
If the character is 'EXT keyword' then jump to &BE93 to execute the 'EXT =' statement. Note: In BASIC 2, it was not
possible to assign a file length (EXT) value, only to read the EXT value; therefore, only 1 BASIC token exists for the 'EXT'
keyword (unlike PTR, PAGE, LOMEM and HIMEM which have 2 keyword tokens, one for the setting of the value (where the keyword
occurs before the '='), and one for the reading of the value (where the keyword occurs after the '=')).
When BASIC version 4 was written, there was not enough spare BASIC Keyword token values to have two 'EXT' keywords
(one for 'EXT =' and the other for '= EXT'), so this workaround directly tests for the 'EXT' keyword appearing at the
start of a statement (along with the other special start statement characters '*', '[' and '=') in order to distinguish between
the reading of the EXT value and the writing of the EXT value. The EXT address in the keyword execution address table points to
the EXT read (=EXT) routine - this is used when the EXT keyword is not at the start of the BASIC program statement.
[&9000-&9048] Execute the BASIC statement
[&9000] Decrement the BASIC Text pointer A offset (location &0A).
[&9002] Call routine &9BA6 to check for the end of statement ('Syntax error' if ':', '<cr>' or 'ELSE' not found).
[&9005] Load the character pointed to by BASIC Text Pointer A.
If the character is not ':' then jump back to &8FBD to skip the rest of the program line and then proceed to execute
the statement on the next line.
[&900B] Set Y to the BASIC Text Pointer A offset (location &0A).
[&900D] Keep incrementing the BASIC Text Pointer A offset (location &0A) until we have found a non-space character.
This skips any spaces at the beginning of the BASIC Statement.
If the first non-space character is more than or equal to #&CF (PTR=, PAGE=,...,OSCLI), [i.e. it is not a Command
Line-only statement (e.g. OLD, NEW, RENUMBER, EDIT) or a middle of statement keyword (e.g. MID$, ELSE, AND, LEN), but a
valid keyword that can occur at the start of a program Statement], then:
[&9019] Jump to the keyword's execution address, as follows:
Multiply the character's ASCII Code by 2 & add the result to the base address &874D to form a pointer to the execution
address (LSB first, MSB next) of the required BASIC keyword. This resulting address is jumped to.
Example 1: Character = &CF [PTR= token] = 11001111 multiply by 2 = 10011110 (which is &9E in hex).
So, &874D + &9E = &87EB (The LSB of the execution address for the PTR= keyword (&87EC is the MSB of the address)).
Example 2: Character = &FF [OSCLI token] = 11111111 multiply by 2 = 11111110 (which is &FE in hex).
So, &874D + &FE = &884B (The LSB of the execution address for the OSCLI keyword (&884C is the MSB of the address)).
BASIC Keywords between 128 and 141 are not considered. as these Keywords are used in the middle of statements (and are not
functions), so the statements/expression handler will deal with these values. These keywords are as follows: AND, DIV, EOR,
MOD, OR, ERROR, LINE, OFF, STEP, SPC, TAB(, ELSE and THEN.
[&901E] Execute the BASIC Command line statement (which can include keywords OLD, NEW, AUTO, EDIT, etc...)
If the first/next non-space character on the command line is more than or equal to #&C6 ('AUTO') then jump back to &9019
to jump to the execution address for the BASIC keyword. This includes all BASIC statement start keywords (but not middle
of statement keywords like MID$, LEN, =PTR, AND, etc...).
Otherwise (the character is less than #&C6) continue to &9025 to check for a variable name.
[&9025] Check for variable name
If the character is less than #&CF then check whether it is a variable name as follows:
* Set BASIC Text pointer B location to the BASIC Text Pointer A location. (&19=&0B, &1A=&0C, &1B=Y)
* Call routine &9909 to evaluate a variable name at the BASIC text pointer B location.
* If routine &9909 returns with a value other than zero then jump to the LET keyword routine (address &904F).
to assign a value to the variable, as the variable was found and the address of the variable's value was found
* Otherwise, (routine &9909 returned zero) the variable is either invalid or hasn't been created yet, so:
- * Check the carry flag status (as returned by routine &9909). If carry is set then jump to &8FEB to
- check whether the BASIC statement begins with a special character ('*', '[', '=' or 'EXT keyword',
- and issue 'Syntax error' [via check end of statement routine &9BA6] if none of these special
- characters match the character at the start of the statement).
- * Store the BASIC text pointer B offset (in X) back to location &1B.
- * Call routine &9B86 to check whether the next non-space character after the variable name is an '=' character.
- If it isn't then issue the 'Mistake' error, as the variable assignment is not correct and a variable
- cannot appear at the start of a statement unless it is being assigned a value.
- * Otherwise, '=' was found sucessfully.
- * Call routine &9854 to add the new variable name to the variable pointer table.
- * If the variable type is a float (location &2C contains #&05) then set X to #&06; otherwise, set X to
- #&05. This specifies the amount of space to allocate for the variable's value.
- * Call routine &9883 to allocate space for the variable (and initialise it's value to zero/null).
- * Decrement the BASIC text pointer A offset (so that BASIC Text pointer A points to the first character of the
- variable name) and continue to the LET keyword routine to evaluate the variable name (again!) and
- assign the value specified after the '=' to the variable.
Disassembly for the Execute next command line / program statement routine
||BNE -5 --> &8FB3
||032 188 155
||20 BC 9B
||JSR &9BBC Update BASIC Text pointer A (Add offset value & then reset offset to 1)
||BRA 4 --> &8FC1 Process the next program line
||BNE -19 --> &8FAE Skip the rest of the line and process the next program line
||BEQ -65 --> &8F86 Read & execute command line input
||BMI -71 --> &8F86 Read & execute command line input
||BEQ 10 --> &8FDB
||032 075 156
||20 4B 9C
||JSR &9C4B Display current line number (IWA) on screen [if TRACE is on]
||BRA 44 --> &900D
||076 032 137
||4C 20 89
||JMP &8920 '[' Begin Assembly
||076 147 190
||4C 93 BE
||JMP &BE93 EXT =
||BEQ -80 --> &8FA4 '*'-Command
||BEQ -23 --> &8FE1
||BEQ -20 --> &8FE8
||BEQ 96 --> &9060 '=' Return from Function Call (FN)
||032 166 155
||20 A6 9B
||JSR &9BA6 Check end of Statement
||BNE -78 --> &8FBD Skip the rest of the line (until '<cr>' found) & process the next program line
||BEQ -10 --> &900B
||BCC 12 --> &9025
||124 077 135
||7C 4D 87
||032 224 142
||20 E0 8E
||JSR &8EE0 Get next non-space character pointed to by Ptr A
||BCS -12 --> &9019
||032 009 153
||20 09 99
||JSR &9909 Evaluate variable/array name & return the address of the value
||BNE 27 --> &904F Create variable (LET)
||BCS -75 --> &8FEB
||032 134 155
||20 86 9B
||JSR &9B86 Check for '='
||032 084 152
||20 54 98
||JSR &9854 Create new variable name in variable pointer table
||BNE 1 --> &9045
||032 131 152
||20 83 98
||JSR &9883 Allocate space for variable
Disassembly for 9B86 Check for '=' routine
||032 213 142
||20 D5 8E
||JSR &8ED5 Get next non-space character (PTR B)
||BNE -45 --> Mistake error