×   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

BAEB Tokenise Command Line and Insert Line into Program

Submitted by Steve Fewell

Routine:Tokenise
Name: Tokenise Command Line (and insert into program if line number found)
Starting Address: &BAEB
Entry criteria: &0700-&07EF (the command line input location) contains the program text to tokenise
Exit: C = 0 if the command is a direct command and not part of a program - this inputt is requried to be executed
immediately.
C = 1 if a program line was entered (and inserted into the program) - not required to be executed immediately.

Description:

Set &28 (OPT flag) to #&FF.
Set &3C to #&FF (this tells the &8DB2 routine to Check for a Line Number at the start of the line).
Call routine &BBCF to initialise the Program start address (&1C, &1D), Stack pointer & REPEAT/FOR/GOSUB
levels and also reset the top bit of the LISTO flag (#&1F, to cancel *EDIT mode).
Note: variables are NOT reset by this initialisation.

Set pointer (&37, &38) to point to BASIC Text Pointer A (i.e. the Command Line input text).
Zero &3B (to initialise routine &8DB2 to be in 'Start of Statement' mode).
Zero BASIC Text pointer A offset (as we will start processing from the beginning of the command line text).

Call routine &8DB2 to tokenise the command line text pointed to by (&37, &38).

Call routine &9B1E to set the IWA to the Line Number (which is now tokenised) at the location pointed to by BASIC Text
Pointer A (i.e. the start of the command line input), and set Y to point to the next character after the Line Number.
If the carry flag is not set then a Line Number was not present, so exit as we do not need to alter the current BASIC
program at all (no lines to insert, change or delete).

[&BB08] If the LISTO flag (&1F) is not 0, then skip any multiple leading spaces after the line number - meaning
that a maximum of 1 space character will be kept after the line number. This enables helps LIST command with its
formatting. Additionally, if no spaces are specified, then none will be stored.

Set location &3B to Y - that is the offset (from &0700) to the start of the program line (after the Line Number),
with any extra leading spaces ignored (if the LISTO flag is not 0).

Call routine &BA98 to remove the line number (specified in the IWA) from the program (if it is found).
Set location &3C to 7 and Y to 0.
Now pointer (&3B, &3C) points to the first character (after the Line Number) of the tokenised program line,
and Y contains the offset (from this pointer) of the current character that we are working with.

If the start of the program line (pointed to by (&3B, &3C)) contains the '<cr>' character then exit as
there is no line to insert - and we have deleted the specified line from the program (as per the user's request).

[&BB26] Search for the next '<cr>' character and set Y to the length of the program line (excluding the Line Number
and the '<cr>' character.
Reduce Y (if necessary) so that it points to the last non-space character on the line. This is done in order to skip any
trailing spaces on the command line.

Store a '<cr>' character at location (&37, &38 + offset Y+1) [i.e. at the end of the program line].
Add 4 to Y (to take into account the 4-byte tokenised Line Number [1 byte for Line Number token and 3 for the Line
Number value]).
Store Y (the line length) in location &3F.

Set &39-&3A to TOP.
Update TOP (by adding Y tm the TOP value via routine &BE04).
Set (&37, &38) to the new TOP value.

Set Y to 0 (Y is 1 on return from &BE04).
If HIMEM (&06, &07) is less than the new TOP value then there is not enough space for the program line to be stored,
so:
    * Call &BBAC to check that the program can be read correctly (and to set the correct TOP value).
      [at this stage the new/changed Line will not be present in the program at all].
    * generate a 'LINE space' error.

[&BB6B] Copy bytes from the old TOP location (&39, &3A) to the new TOP location (&37, &38), starting
with the last character in the program as follows:
* Copy character from (&39, &3A) + Y to location (&37, &38) + Y
* If Y is 0 then decrement old TOP MSB byte (&3A) and new TOP MSB byte (&38).
* [&BB76] decrement Y
* Set A (LSB byte) and X (MSB byte) to the old TOP value plus Y.
* [&BB7F] if (&3D, &3E) [which is a pointer to the program line that is greater than or equal to the line that
we are inserting, as set by routine &BA98] is less than the old TOP value + Y (stored in: A (LSB byte), X (MSB byte))
then jump back to &BB6B to copy the next byte from the old TOP location to the new TOP location.
Otherwise, it must be equal to the location of the program line after the one we want to insert (as this program
line has just been copied to the new TOP location, so now we have space for the new program line to be inserted.

Store the program line number MSB (from the IWA byte &2B) in the first byte of the reserved program space pointed to
by (&3D-&3E).
Store the program line number LSB (from the IWA byte &2A) in the second byte of the reserved program space pointed to
by (&3D-&3E).
Store the line length (from location &3F) in the third byte of the reserved program space pointed to by (&3D-&3E).

Add 4 (Y + 1) to the (&3D-&3E) pointer.
Copy the new BASIC program (from location (&3B, &3C), i.e. the command line) to the new location (&3D, &3E).
(The program line is terminated by the '<cr>' character).
Exit with Carry flag set.
Note: the lines before the inserted line do not need to be moved, only the lines between the inserted line and the old TOP
location.


Disassembly for the 'Tokenise Command Line and Insert Line into Program' routine

BAEB   162 255 A2 FF LDX#&FF
BAED ( 134 040 86 28 STX &28
BAEF < 134 060 86 3C STX &3C
BAF1   032 207 187 20 CF BB JSR &BBCF Initialise Program start address, Stack pointer, *EDIT mode & REPEAT/FOR/GOSUB levels
BAF4   165 011 A5 0B LDA &0B
BAF6 7 133 055 85 37 STA &37
BAF8   165 012 A5 0C LDA &0C
BAFA 8 133 056 85 38 STA &38
BAFC d; 100 059 64 3B STZ &3B
BAFE d 100 010 64 0A STZ &0A
BB00   032 178 141 20 B2 8D JSR &8DB2 Tokenise Command Line
BB03   032 030 155 20 1E 9B JSR &9B1E Detokenise the Line Number at PTR A & Set IWA to the Line Number value
BB06   144 226 90 E2 BCC -30 --> &BAEA [RTS]
BB08   165 031 A5 1F LDA &1F
BB0A   240 009 F0 09 BEQ 9 --> &BB15
BB0C   185 000 007 B9 00 07 LDA &0700,Y
BB0F   200 C8 INY
BB10   201 032 C9 20 CMP#&20
BB12   240 248 F0 F8 BEQ -8 --> &BB0C
BB14   136 88 DEY
BB15 ; 132 059 84 3B STY &3B
BB17   032 152 186 20 98 BA JSR &BA98 Remove Line Number (specified in IWA) from Program
BB1A   160 007 A0 07 LDY#&07
BB1C < 132 060 84 3C STY &3C
BB1E   160 000 A0 00 LDY#&00
BB20   169 013 A9 0D LDA#&0D
BB22 ; 210 059 D2 3B CMP (&3B)
BB24   240 196 F0 C4 BEQ -60 --> &BAEA [RTS]
BB26   200 C8 INY
BB27 ; 209 059 D1 3B CMP (&3B),Y
BB29   208 251 D0 FB BNE -5 --> &BB26
BB2B   169 032 A9 20 LDA#&20
BB2D   136 88 DEY
BB2E   240 004 F0 04 BEQ 4 --> &BB34
BB30 ; 209 059 D1 3B CMP (&3B),Y
BB32   240 249 F0 F9 BEQ -7 --> &BB2D
BB34   200 C8 INY
BB35   169 013 A9 0D LDA#&0D
BB37 ; 145 059 91 3B STA (&3B),Y
BB39   200 C8 INY
BB3A   200 C8 INY
BB3B   200 C8 INY
BB3C   200 C8 INY
BB3D ? 132 063 84 3F STY &3F
BB3F   165 018 A5 12 LDA &12
BB41 9 133 057 85 39 STA &39
BB43   165 019 A5 13 LDA &13
BB45 : 133 058 85 3A STA &3A
BB47   032 004 190 20 04 BE JSR &BE04 Update TOP (add Y to TOP value, set Y to 1 & exit)
BB4A 7 133 055 85 37 STA &37
BB4C   165 019 A5 13 LDA &13
BB4E 8 133 056 85 38 STA &38
BB50   136 88 DEY
BB51   165 006 A5 06 LDA &06
BB53   197 018 C5 12 CMP &12
BB55   165 007 A5 07 LDA &07
BB57   229 019 E5 13 SBC &13
BB59   176 016 B0 10 BCS 16 --> &BB6B
BB5B   032 229 189 20 E5 BD JSR &BDE5 Check program can be read correctly ('Bad program' message if not)
BB5E   032 172 187 20 AC BB JSR &BBAC Initialise Page 7 & reset Variable pointers, etc...
BB61   ... 'LINE space' error ...
BB6B 9 177 057 B1 39 LDA (&39),Y
BB6D 7 145 055 91 37 STA (&37),Y
BB6F   152 98 TYA
BB70   208 004 D0 04 BNE 4 --> &BB76
BB72 : 198 058 C6 3A DEC &3A
BB74 8 198 056 C6 38 DEC &38
BB76   136 88 DEY
BB77   152 98 TYA
BB78 e9 101 057 65 39 ADC &39
BB7A : 166 058 A6 3A LDX &3A
BB7C   144 001 90 01 BCC 1 --> &BB7F
BB7E   232 E8 INX
BB7F = 197 061 C5 3D CMP &3D
BB81   138 8A TXA
BB82 > 229 062 E5 3E SBC &3E
BB84   176 229 B0 E5 BCS -27 --> &BB6B
BB86   160 001 A0 01 LDY#&01
BB88 + 165 043 A5 2B LDA &2B
BB8A = 145 061 91 3D STA (&3D),Y
BB8C   200 C8 INY
BB8D * 165 042 A5 2A LDA &2A
BB8F = 145 061 91 3D STA (&3D),Y
BB91   200 C8 INY
BB92 ? 165 063 A5 3F LDA &3F
BB94 = 145 061 91 3D STA (&3D),Y
BB96 8 056 38 SEC
BB97   152 98 TYA
BB98 e= 101 061 65 3D ADC &3D
BB9A = 133 061 85 3D STA &3D
BB9C   144 002 90 02 BCC 2 --> &BBA0
BB9E > 230 062 E6 3E INC &3E
BBA0   160 255 A0 FF LDY#&FF
BBA2   200 C8 INY
BBA3 ; 177 059 B1 3B LDA (&3B),Y
BBA5 = 145 061 91 3D STA (&3D),Y
BBA7   201 013 C9 0D CMP#&0D
BBA9   208 247 D0 F7 BNE -9 --> &BBA2
BBAB ` 096 60 RTS

 


 Back to 8BS
Or