8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


BASIC IV ROM Routines
Page Last Altered:


B278 BASIC error handler

Submitted by Steve Fewell

Description:

This code is executed everytime a BRK instruction (i.e. a BASIC, or OS, error message is issued), as the address in the
BRK vector (&202-&203) is set to point to this code when the BASIC language is initiated.
Therefore, this routine will be executed when an error occurs within BASIC (i.e. 'Type mismatch', 'Mistake', 'Escape' and
'Missing #') or when an error occurs in the OS (or another ROM), i.e. 'Bad String', 'Bad command'.

On entry locations &FD-&FE point to the byte after the BRK instruction (i.e. the error number) and this number will
be followed by the actual error message.

Set the OPT flag (location &28) to #&FF - ?
Reset the stack pointer to #&FF to discard any stack storage / routine return addresses that are no longer required
(there aren't required as the Error handler will pass control to the ON ERROR code - and ignore the current position in
the program).

Set X and Y to zero (as required by the OSBYTE calls below).
Call the Operating System OSBYTE call &DA to abandon any expected items that were still to be read by the VDU queue.
Call the Operating System OSBYTE call &7E to acknowledge the detection of an escape condition (this flushes the buffers
and closes any open EXEC files).

Call routine &B237 to obtain the ERL value as follows:
* If BASIC Text pointer A offset (location &0A) is not zero then decrement it (so it points to the last
character read)
* Call routine &9BBC to add the BASIC Text pointer A offset value (&0A) to the pointer address (&0B-&0C)
    and then reset the offset (&0A) to 1.
* Zero the ERL value (&08-&09) so that the default ERL value for the error condition is zero.
* Set &37-&38 to point to PAGE (the start of the program).
* If the BASIC Text Pointer MSB (&0C) value is #&07 then we are not executing a program (only the command line
    input) so exit the &B237 routine
* Set X to the BASIC Text pointer A LSB and Y to the BASIC Text pointer A MSB
* Next, obtain the Program ERL number as follows:
* [&B251] Read the next byte of the program at the location pointed to by &37-&38 & increment the pointer
* If the byte that was read was a return character ('<cr>') then:
         * Compare the &37-&38 pointer address with X and Y (the BASIC text pointer A address)
         * If the &37-&38 pointer has exceeded the X & Y position then exit the &B237 routine
         * Read the next character at the &37-&38 pointer and increment the pointer
         * ORA the character with #&00 (to set the flags) and if the negative flag is set then the end of
            program has been reached, so exit the &B237 routine (as we don't have an ERL number).
         * Store the character in location &09 - this is the MSB of the Error Line number (ERL)
         * Read the next character at the &37-&38 pointer and increment the pointer
         * Store the character in location &08 - this is the LSB of the Error Line number (ERL)
         * Read the next character at the &37-&38 pointer and increment the pointer
* If the BASIC Text pointer A address (in registers X and Y) is greater than the &37-&38 pointer then jump
    back to &B251 to keep looking for the ERL value.
Store zero at location &20 (to turn off TRACE).
If the byte at the location pointed to by &FD-&FE (i.e. the error message number) is zero then the error is
not trapable (i.e. it is 'No room' or 'STOP'), so call &B2A6 to reset the 'ON ERROR' pointer to the BASIC default
(which displays the error message and ERL number and ENDs the program).

[&B296] Set BASIC text pointer A (&0B-&0C) to the BASIC code pointed to at the address of the 'ON ERROR' pointer
(&16-&17) and zero the BASIC text pointer A offset (&0A).

Call routine &BBCF to initialise the BASIC environment - this sets the program start address, the Stack pointer,
the *EDIT mode and the REPEAT/FOR/GOSUB control information.
Jump to &900B to execute the BASIC statement beginning at the BASIC text pointer A location.


Disassembly for the BASIC error handler routine

B237   164 010 A4 0A LDY &0A
B239   240 001 F0 01 BEQ 1 --> &B23C
B23B   136 88 DEY
B23C   032 188 155 20 BC 9B JSR &9BBC Update BASIC Text pointer A (Add offset value & then reset offset to 1)
B23F d 100 008 64 08 STZ &08
B241 d 100 009 64 09 STZ &09
B243   166 024 A6 18 LDX &18
B245 8 134 056 86 38 STX &38
B247 d7 100 055 64 37 STZ &37
B249   164 012 A4 0C LDY &0C
B24B   192 007 C0 07 CPY#&07
B24D ( 240 040 F0 28 BEQ 40 --> &B277
B24F   166 011 A6 0B LDX &0B
B251   032 160 141 20 A0 8D JSR &8DA0 Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B254   201 013 C9 0D CMP#&0D
B256   208 024 D0 18 BNE 24 --> &B270
B258 7 228 055 E4 37 CPX &37
B25A   152 98 TYA
B25B 8 229 056 E5 38 SBC &38
B25D   144 024 90 18 BCC 24 --> &B277
B25F   032 160 141 20 A0 8D JSR &8DA0 Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B262   009 000 09 00 ORA#&00
B264 0 048 017 30 11 BMI 17 --> &B277
B266   133 009 85 09 STA &09
B268   032 160 141 20 A0 8D JSR &8DA0 Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B26B   133 008 85 08 STA &08
B26D   032 160 141 20 A0 8D JSR &8DA0 Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B270 7 228 055 E4 37 CPX &37
B272   152 98 TYA
B273 8 229 056 E5 38 SBC &38
B275   176 218 B0 DA BCS -38 --> &B251
B277 ` 096 60 RTS
B278   162 255 A2 FF LDX#&FF
B27A ( 134 040 86 28 STX &28
B27C   154 9A TXS
B27D   232 E8 INX
B27E   160 000 A0 00 LDY#&00
B280   169 218 A9 DA LDA#&DA
B282   032 244 255 20 F4 FF JSR &FFF4   OSBYTE
B285 ~ 169 126 A9 7E LDA#&7E
B287   032 244 255 20 F4 FF JSR &FFF4   OSBYTE
B28A 7 032 055 178 20 37 B2 JSR &B237 Obtain the ERL value
B28D d 100 032 64 20 STZ &20
B28F   178 253 B2 FD LDA (&FD)
B291   208 003 D0 03 BNE 3 --> &B296
B293   032 166 178 20 A6 B2 JSR &B2A6 Reset 'ON ERROR' pointer to BASIC's default error handling routine
B296   165 022 A5 16 LDA &16
B298   133 011 85 0B STA &0B
B29A   165 023 A5 17 LDA &17
B29C   133 012 85 0C STA &0C
B29E d 100 010 64 0A STZ &0A
B2A0   032 207 187 20 CF BB JSR &BBCF Initialise Program start address, Stack pointer, *EDIT mode & REPEAT/FOR/GOSUB levels
B2A3 L 076 011 144 4C 0B 90 JMP &900B Process next BASIC program statement

 


 Back to 8BS
Or