8-Bit Software

The BBC and Master Computer Public Domain Library

Back to 8BS Or Back to Disassembly


Page Last Altered:

9384 RENUMBER

Submitted by Steve Fewell

Description:

Call routine &934D to read the Starting Line Number and Line increment parameters (if any were specified) as follows:
* Set the IWA to 10 (the default program starting Line Number)
* Call routine &9B1E to detokenise and extract the line number at BASIC text pointer A location and store it
    in the IWA. If no line number was found at BASIC text pointer A then the IWA will retain
    the default value of 10.
* Push the IWA value (the starting line number) to the BASIC stack
* Set the IWA to 10 (the default line number increment)
* If the next non-space character is not comma ',', then no line increment parameter has been specified, so call &9BB0
    to check that the statement is terminated correctly (with a ':', '<cr>' or 'ELSE-token'
    terminator character), [if not then a 'Syntax error' will be issued] and exit routine &934D.
* Otherwise, call routine &9B1E to detokenise and extract the Line number value from the BASIC Text pointer A
    location and store it in the IWA. The IWA now contains the specified Line increment value.
* If byte &2B of the IWA contains a non-zero value, then the increment value is more than 255, so issue error
    'Silly', as the line increment cannot be more than 255.
* If byte &2A (the LSB) of the IWA contains a zero value, then the increment value is zero, so issue error
    'Silly', as the line increment cannot be zero.
* Call &9BA6 to skip any space characters and check that the statement is terminated correctly (with a ':',
    '<cr>' or 'ELSE-token' terminator character), [if not then a 'Syntax error' will be
    issued] and exit routine &934D.
On return from routine &934D, the Integer on the BASIC stack is the Start Line Number and the IWA contains the step
size.

Retrieve the Start Line number from the BASIC stack and store it in locations &39-&3A (bytes &3B-&3C are
unused as the Integer should only be 2-bytes wide). This is the renumbered line number counter.
Call routine &BDE5 to check that the program can be read correctly ('Bad program' message is displayed if the program
cannot be correctly read).

Call routine &9373 to set locations &3B-&3C to point to TOP (the end of the program) and to set &37-&38 to
point to PAGE+1 (the start of the program ignoring the first '<cr>' character) and to set Y to 1.

Renumber - Phase 1: Store all current line numbers in the program in the free storage space after TOP
Use the free space after TOP (pointed to by &3B-&3C) to store a copy of all of of the Line numbers present in the
program (and to issue an error if there is not enough space to store this required informaton) as follows.
[&9392] Read the next character (offset 0) from the &37-&38 pointer.
If the character is negative (end of program reached) then jump to &93C4 (all line numbers are stored, so continue to
the main RENUMBER routine).

Store the character (the first byte of the Line Number) at the location pointed to by the &3B-&3C pointer, i.e. the
free space after the TOP of the program. Read the next character (offset 1) from the &37-&38 pointer and store this
character in the next free space pointed to by &3B-&3C (offset 1). This value is the second byte of the Line Number
value.
Add 2 to the &3B-&3C pointer (to point to the next free location after the program end).
Compare the &3B-&3C pointer address with HIMEM (stored in locations &06-&07). If &3B-&3C exceeds
(or is equal to) HIMEM then issue 'RENUMBER space' error (and terminate the RENUMBER operation).
Otherwise, call routine &947A to update &37-&38 to point to the start of the next program line.
Jump back to &9392 to copy the next program line number.

Renumber - Phase 2: Replace all line numbers in the program with the renumbered line numbers
[&93C4] Call routine &937B to set &37-&38 to point to PAGE + 1 and to set Y to 1.
[&93C7] Load the first character of the Line's Line number. If the character is negative (end of program reached) then
jump to &93E7 to start the next phase of the renumbering.
Replace the Line number on the current line with the next renumbered Line Number (from locations &39-&3A (storing
&3A first)).

Add the Step Size (&2A) to the renumbered Line Number counter (&39-&3A (&3A is the MSB)).
Correct the &39-&3A Line Number if it has exceeded 32767 (&7FFF) by resetting bit 7 to 0 (removing the &8000
value from the value). So &39-&3A now contains the next renumbered Line Number.
Call routine &947A to update &37-&38 to point to the start of the next program line.
Jump back to &93C7 to replace the next program line number.

Renumber - Phase 3: Replace all line number references in the program with the correct renumbered version
[&93E7] Set BASIC Text pointer A (&0B-&0C) to PAGE (&18, the start of the program code).
[&93ED] Load the first character (the Line Number) of the program line pointed to by &0B-&0C.
If the character is negative then we have reached the end of the program, so jump to &8F83 to initialise the BASIC
variable space (as we have just destroyed any previously defined variables by placing the old line number values after TOP)
and prompt for the user's next command line input - as the RENUMBER operation is complete.

Set Y (the line offset value) to 4 (so that the Line Number and line length byte values are ignored).
Zero location &2C (the quote toggle flag).

[&93F7] Read the next byte of the program line pointed to by the address in locations &0B-&0C (offset Y).
If &2C is zero (then we are not inside a quote) so:
* If the character is '#&8D' (Line number token) then jump to &941B to renumber the line number (this routine
    is described below). This token usually follows a GOSUB, GOTO, THEN or ELSE keyword and is
    followed by the literal line number
* If the character is '#&F4' [REM-token] then jump to &9412 to skip the rest of the current program line
[&9405] Increment Y (to point to the next character on the line)
If the current character (that was read by &93F7) is a quote (") then EOR the character with &2C (to
toggle the flag value).
If the character is not '<cr>' (carriage return) then jump back to &93F7 to check the next character of the
program line.
[&9412] Otherwise, skip the line end and move on to the next line by Adding the value at offset 3 of the current line
(which is the line length value) to the &0B-&0C pointer [and set the &0A offset to 1] and jump back to &93ED
to check the next program line for line numbers to renumber.

Renumber the Line Number specified after the line number token ('#&8D')
[&941B] Now a line number token has been encountered within a program line. This routine will now renumber the line
number so that it addresses the correct renumbered line.

Call &9B2A to set the IWA to the Line Number (after the Line number token value -'&8D').
Call routine &9373 to set &3B-&3C to TOP and &37-&38 to PAGE+1.
[&9421] Load the character at the &37-&38 location.
If the character is negative then the end of the program has been reached without finding a matching program Line, so jump
to &945C to:
* Print the text "Failed at "
* Store A in &2B (the Line number MSB)
* Store the character pointed to by &0B-&0C (the line number LSB) to location &2A
* Call routine &A081 to display the line number (in the IWA) on the screen - this outputs the line number that
    contained the failed reference
* Call routine &BA92 to output a new line
* Jump to &9446 to Set Y to &0A (the current position of the program line in the phase 3 search) and jump to
    &93F7 to continue checking the next character of the program line (for further line number
    references.
Compare the IWA line number with the next Line Number at the &3B-&3C pointed (i.e. the next line number that was
stored after the end of the program).
If the line numbers are not equal then the matching renumbered line has not yet been found so:
* [&944A] Clear the carry flag (so that we don't add an extra 1 to the &37-&38 pointer)
* Call routine &947A to update the &37-&38 pointer to point to the next program line (by adding the line
    length byte (offset 3 from the current &37-&38 start of line pointer) to the &37-&38
    pointer and reset Y to 1.
* Add 2 to the &3B-&3C pointer (so that the pointer points to the next unrenumbered line number stored after
    the end of the program.
* Jump back to &9421 to check the next line of the program for a match
Otherwise (we have found a match), so store the new renumbered line number (from the &37-&38 pointer location)
in the current program position - to replace the reference with the renumbered reference - as follows:
* Store the Line number MSB ((&37,&38)+1) in location &3D
* Store the Line Number LSB (pointed to by (&37, &38)) in X register
* Set Y = the BASIC Text Pointer A offset - 1
* Set &39-&3A to the current &0B-&0C pointer location (i.e. the line containing the reference)
* Call routine &8D62 to tokenise the Program Line Number (in X and &3D) and store it at the ((&39-&3A)+1)
    location
* Set Y to the BASIC Text pointer A offset (&0A)
* Jump to &93F7 to continue with the next character of the program line (pointed to by &0B-&0C)


Disassembly for the RENUMBER routine

934D   169 010 A9 0A LDA#&0A
934F   032 024 174 20 18 AE JSR &AE18 Set IWA to the 8-bit value in A
9352   032 030 155 20 1E 9B JSR &9B1E Detokenise the Line Number at PTR A & Set IWA to the Line Number value
9355 & 032 038 188 20 26 BC JSR &BC26 Push IWA value to the BASIC Stack [pushi]
9358   169 010 A9 0A LDA#&0A
935A   032 024 174 20 18 AE JSR &AE18 Set IWA to the 8-bit value in A
935D   032 229 140 20 E5 8C JSR &8CE5 Compare next non-space [PTR A] character with ','
9360   208 014 D0 0E BNE 14 --> &9370
9362   032 030 155 20 1E 9B JSR &9B1E Detokenise the Line Number at PTR A & Set IWA to the Line Number value
9365 + 165 043 A5 2B LDA &2B
9367 S 208 083 D0 53 BNE 83 --> &93BC 'Silly' error
9369 * 165 042 A5 2A LDA &2A
936B O 240 079 F0 4F BEQ 79 --> &93BC 'Silly' error
936D L 076 166 155 4C A6 9B JMP &9BA6 Skip spaces and check end of Statement
9370 L 076 176 155 4C B0 9B JMP &9BB0 Check for end of Statement
9373   165 018 A5 12 LDA &12
9375 ; 133 059 85 3B STA &3B
9377   165 019 A5 13 LDA &13
9379 < 133 060 85 3C STA &3C
937B   165 024 A5 18 LDA &18
937D 8 133 056 85 38 STA &38
937F   160 001 A0 01 LDY#&01
9381 7 132 055 84 37 STY &37
9383 ` 096 60 RTS
9384 M 032 077 147 20 4D 93 JSR &934D Read Start line and line increment parameters
9387 9 162 057 A2 39 LDX#&39
9389   032 008 189 20 08 BD JSR &BD08 Pop Integer from Stack to Zero page address
938C   032 229 189 20 E5 BD JSR &BDE5 Check program can be read correctly ('Bad program' message if not)
938F s 032 115 147 20 73 93 JSR &9373 Set &3B-&3C to TOP and &37-&38 to PAGE+1
9392 7 178 055 B2 37 LDA (&37)
9394 0. 048 046 30 2E BMI 46 --> &93C4
9396 ; 146 059 92 3B STA (&3B)
9398 7 177 055 B1 37 LDA (&37),Y
939A ; 145 059 91 3B STA (&3B),Y
939C 8 056 38 SEC
939D   152 98 TYA
939E e; 101 059 65 3B ADC &3B
93A0 ; 133 059 85 3B STA &3B
93A2   144 002 90 02 BCC 2 --> &93A6
93A4 < 230 060 E6 3C INC &3C
93A6   197 006 C5 06 CMP &06
93A8 < 165 060 A5 3C LDA &3C
93AA   229 007 E5 07 SBC &07
93AC   176 005 B0 05 BCS 5 --> &93B3 'RENUMBER space' error
93AE z 032 122 148 20 7A 94 JSR &947A Advance &37-&38 to point to the next Program Line
93B1   128 223 80 DF BRA -33 --> &9392
93B3   ... 'Silly' error...
93BC   ... 'RENUMBER space' error...
93C4 { 032 123 147 20 7B 93 JSR &937B Set &37-&38 to PAGE+1
93C7 7 178 055 B2 37 LDA (&37)
93C9 0 048 028 30 1C BMI 28 --> &93E7
93CB : 165 058 A5 3A LDA &3A
93CD 7 146 055 92 37 STA (&37)
93CF 9 165 057 A5 39 LDA &39
93D1 7 145 055 91 37 STA (&37),Y
93D3   024 18 CLC
93D4 9 165 057 A5 39 LDA &39
93D6 e* 101 042 65 2A ADC &2A
93D8 9 133 057 85 39 STA &39
93DA   169 000 A9 00 LDA#&00
93DC e: 101 058 65 3A ADC &3A
93DE ) 041 127 29 7F AND#&7F
93E0 : 133 058 85 3A STA &3A
93E2 z 032 122 148 20 7A 94 JSR &947A Advance &37-&38 to point to the next Program Line
93E5   128 224 80 E0 BRA -32 --> &93C7
93E7   165 024 A5 18 LDA &18
93E9   133 012 85 0C STA &0C
93EB d 100 011 64 0B STZ &0B
93ED   160 001 A0 01 LDY#&01
93EF   177 011 B1 0B LDA (&0B),Y
93F1 0g 048 103 30 67 BMI 103 --> &945A
93F3   160 004 A0 04 LDY#&04
93F5 d, 100 044 64 2C STZ &2C
93F7   177 011 B1 0B LDA (&0B),Y
93F9 , 166 044 A6 2C LDX &2C
93FB   208 008 D0 08 BNE 8 --> &9405
93FD   201 141 C9 8D CMP#&8D
93FF   240 026 F0 1A BEQ 26 --> &941B
9401   201 244 C9 F4 CMP#&F4
9403   240 013 F0 0D BEQ 13 --> &9412
9405   200 C8 INY
9406 " 201 034 C9 22 CMP#&22
9408   208 004 D0 04 BNE 4 --> &940E
940A E, 069 044 45 2C EOR &2C
940C , 133 044 85 2C STA &2C
940E   201 013 C9 0D CMP#&0D
9410   208 229 D0 E5 BNE -27 --> &93F7
9412   160 003 A0 03 LDY#&03
9414   177 011 B1 0B LDA (&0B),Y
9416   032 244 155 20 F4 9B JSR &9BF4 Add A to PTR A pointer (&0B,&0C) & set PTR A (&0A) offset to 1
9419   128 210 80 D2 BRA -46 --> &93ED
941B * 032 042 155 20 2A 9B JSR &9B2A Detokenise Line Number at PTR A (without skip spaces or skip #&8D char) & Set IWA to the Line Number value
941E s 032 115 147 20 73 93 JSR &9373 Set &3B-&3C to TOP and &37-&38 to PAGE+1
9421 7 178 055 B2 37 LDA (&37)
9423 07 048 055 30 37 BMI 55 --> &945C
9425 ; 178 059 B2 3B LDA (&3B)
9427 + 197 043 C5 2B CMP &2B
9429   208 031 D0 1F BNE 31 --> &944A
942B ; 177 059 B1 3B LDA (&3B),Y
942D * 197 042 C5 2A CMP &2A
942F   208 025 D0 19 BNE 25 --> &944A
9431 7 177 055 B1 37 LDA (&37),Y
9433 = 133 061 85 3D STA &3D
9435 7 178 055 B2 37 LDA (&37)
9437   170 AA TAX
9438   164 010 A4 0A LDY &0A
943A   136 88 DEY
943B   165 011 A5 0B LDA &0B
943D 9 133 057 85 39 STA &39
943F   165 012 A5 0C LDA &0C
9441 : 133 058 85 3A STA &3A
9443 b 032 098 141 20 62 8D JSR &8D62 Tokenise program line & store at location specified by &39-&3A (which is &0B-&0C)
9446   164 010 A4 0A LDY &0A
9448   128 173 80 AD BRA -83 --> &93F7
944A   024 18 CLC
944B z 032 122 148 20 7A 94 JSR &947A Advance &37-&38 to point to the next Program Line
944E ; 165 059 A5 3B LDA &3B
9450 i 105 002 69 02 ADC#&02
9452 ; 133 059 85 3B STA &3B
9454   144 203 90 CB BCC -53 --> &9421
9456 < 230 060 E6 3C INC &3C
9458   128 199 80 C7 BRA -57 --> &9421
945A Z 128 090 80 5A BRA 90 --> &94B6 [JMP &8F83 Initialise & prompt for next command line]
945C   032 207 190 20 CF BE JSR &BECF Print following text as a warning message & jump to address specified
945F Failed at  070 097 105 108 101 100 032 097 116 032 46 61 69 6C 65 64 20 61 74 20 EQUS "Failed at "
9469   177 B1 EQUB &B1
946A   011 0B EQUB &0B
946B + 133 043 85 2B STA &2B
946D   200 C8 INY
946E   177 011 B1 0B LDA (&0B),Y
9470 * 133 042 85 2A STA &2A
9472   032 129 160 20 81 A0 JSR &A081 Print Line Number on screen (width 0)
9475   032 146 186 20 92 BA JSR &BA92 Start new output line
9478   128 204 80 CC BRA -52 --> &9446
947A   200 C8 INY
947B 7 177 055 B1 37 LDA (&37),Y
947D   160 001 A0 01 LDY#&01
947F e7 101 055 65 37 ADC &37
9481 7 133 055 85 37 STA &37
9483   144 003 90 03 BCC 3 --> &9488
9485 8 230 056 E6 38 INC &38
9487   024 18 CLC
9488 ` 096 60 RTS

 


 Back to 8BS
Or