×   Main Menu ALL The 8BS News Manuals (New menu) Links Worth a Look Tools Disc and Basic Webring Site Map 8BS Guestbook Old Guest Book Me The Barnsley Rovers   
8-Bit Software

The BBC and Master Computer Public Domain Library



B2B2 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 &B271 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 &B271 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 &B271 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 &B271 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 &BC51 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 &B2E0 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

B271   164 010 A4 0A LDY &0A
B273   240 001 F0 01 BEQ 1 --> &B276
B275   136 88 DEY
B276   032 128 156 20 80 9C JSR &9C80 Update BASIC Text pointer A (Add offset value & then reset offset to 1)
B279 d 100 008 64 08 STZ &08
B27B d 100 009 64 09 STZ &09
B27D   166 024 A6 18 LDX &18
B27F 8 134 056 86 38 STX &38
B281 d7 100 055 64 37 STZ &37
B283   164 012 A4 0C LDY &0C
B285   192 007 C0 07 CPY#&07
B287 ( 240 040 F0 28 BEQ 40 --> &B2B1
B289   166 011 A6 0B LDX &0B
B28B   032 093 142 20 5D 8E JSR &8E5D Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B28E   201 013 C9 0D CMP#&0D
B290   208 024 D0 18 BNE 24 --> &B2AA
B292 7 228 055 E4 37 CPX &37
B294   152 98 TYA
B295 8 229 056 E5 38 SBC &38
B297   144 024 90 18 BCC 24 --> &B2B1
B299   032 093 142 20 5D 8E JSR &8E5D Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B29C   009 000 09 00 ORA#&00
B29E 0 048 017 30 11 BMI 17 --> &B2B1
B2A0   133 009 85 09 STA &09
B2A2   032 093 142 20 5D 8E JSR &8E5D Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B2A5   133 008 85 08 STA &08
B2A7   032 093 142 20 5D 8E JSR &8E5D Read character pointed to by &37-&38 and then increment the &37-&38 pointer
B2AA 7 228 055 E4 37 CPX &37
B2AC   152 98 TYA
B2AD 8 229 056 E5 38 SBC &38
B2AF   176 218 B0 DA BCS -38 --> &B28B
B2B1 ` 096 60 RTS
B2B2   162 255 A2 FF LDX#&FF
B2B4 ( 134 040 86 28 STX &28
B2B6   154 9A TXS
B2B7   232 E8 INX
B2B8   160 000 A0 00 LDY#&00
B2BA   169 218 A9 DA LDA#&DA
B2BC   032 244 255 20 F4 FF JSR &FFF4   OSBYTE
B2BF ~ 169 126 A9 7E LDA#&7E
B2C1   032 244 255 20 F4 FF JSR &FFF4   OSBYTE
B2C4 7 032 055 178 20 37 B2 JSR &B271 Obtain the ERL value
B2C7 d 100 032 64 20 STZ &20
B2C9   178 253 B2 FD LDA (&FD)
B2CB   208 003 D0 03 BNE 3 --> &B2D0
B2CD   032 166 178 20 A6 B2 JSR &B2E0 Reset 'ON ERROR' pointer to BASIC's default error handling routine
B2D0   165 022 A5 16 LDA &16
B2D2   133 011 85 0B STA &0B
B2D4   165 023 A5 17 LDA &17
B2D6   133 012 85 0C STA &0C
B2D8 d 100 010 64 0A STZ &0A
B2DA   032 207 187 20 CF BB JSR &BBFF Initialise Program start address, Stack pointer, *EDIT mode & REPEAT/FOR/GOSUB levels
B2A3 L 076 011 144 4C 0B 90 JMP &90D0 Process next BASIC program statement

 


 Back to 8BS
Or