DOSPLUS 3.5 for the Model I - RENUM/CMD Disassembled

Page Customization

Introduction/Summary

DOSPLUS 3.5 RENUM/CMD Disassembly - CMD"RENUM", the Renumberer of a BASIC Program (Model I)

RENUM/CMD is the renumberer of Extended Disk BASIC, CMD"RENUM"; its heading reads RENUM - BASIC program renumberer - 1.7. It is a load module of 5 blocks, 1174 bytes, at 5200H-5695H, with the transfer address 5200H, after the two comment records (the name RENUM and the copyright notice). The Model I and Model III files are identical byte for byte, and so are the two pages apart from their links.

It runs only from Extended Disk BASIC: BASIC/CMD's CMD (5C48H) passes RENUM to CMNDI@ as a DOS command, the DOS loads RENUM/CMD at 5200H and calls it, and RENUM takes BASIC's text pointer off the stack (5212H). It ends at the ROM's READY, after the ROM has cleared the variables, or in one of the ROM's errors; either resets the stack. From the DOS command level it only shows its heading. TBASIC cannot run it, since its CMD runs no DOS commands.

The Statement

CMD"RENUM",nln,inc,sln,eln: the new number of the first line renumbered (10 as loaded), the increment (10), and the first and last lines of the range (0 and 65529); any of them may be left out, keeping its comma when a later one is given (5213H). CMD"RENUM",! checks the line references without renumbering (5220H). Before anything is changed RENUM shows Illegal function call for an increment of 0, a last line below the first, a new first number that is not above every line before the range (5266H), or new numbers that would reach the first line after the range (52A6H); it shows Overflow for a new number above 65529 (52AFH).

The Parts of RENUM/CMD

AddressContents
5200H-5282HThe start and the parameters (5200H).
5283H-52DCHThe new numbers, stored in the links (5283H).
52DDH-530BHThe index of 128 line addresses (52DDH).
530CH-535EHA pass over the program, line by line (530CH).
535FH-53EFHThe statements and the tokens that take line numbers (535FH).
53F0H-5492HA line reference: found, checked, counted or written (53F0H).
5493H-5505HThe end of a pass: the move for the second pass and the new line numbers (5493H).
5506H-5567HThe end test, the next line, the links, the bytes read and written (5506H, 5517H, 5523H, 5542H, 555CH).
5568H-55AAHThe messages and the errors (5568H, 5599H).
55ABH-5610HText output, a number read, a number written (55ABH, 55B4H, 55EBH).
5611H-56AAHThe texts and the variables (5611H).

How RENUM Works

A first walk over the program (5283H) works out every line's new number and stores it in the line's link, while the line number itself keeps the old number; the walk counts the lines and checks that each link points to the line after it (else Internal error). The end of the program is then marked FFFFH instead of 0000H, since a link may now hold the number 0, and the end test is changed to match (52D6H, 5506H). An index of 128 line addresses, every Nth line, is built in BASIC's input buffer (52DDH), so that a line can be found by a binary search and a short walk (53FFH).

Then come two passes over the text. Each statement is read byte by byte (535FH), spaces and line feeds passed over; a line number is looked for after GOTO, GOSUB and EDIT (a list separated by commas), after RUN, THEN, ELSE and RESUME (one number, then a list as for GOTO), after ERL and the AND, OR or relation that follows it, and after DELETE, LIST and LLIST (with . for the current line and - between two lines). ON ERROR GOTO 0 and RESUME 0 are not references; a label after GOTO is not a number, so labels are left alone, as the manual says. The first pass writes nothing: it shows Syntax error in and the line for a number too large, and Undefined line in and the line for a reference to a line that does not exist, and it adds up how much longer the program gets (5476H). If a message was shown, or for CMD"RENUM",!, the work ends there (5493H) and the links are rebuilt, so the program is as before. Otherwise the program is moved up by its greatest growth plus 256 bytes (Out of memory unless that leaves 86 bytes below the string space, 54A6H) and the second pass writes it back down from TXTTAB with each reference replaced by its new number (5467H). Last, each line's new number is moved from the link into the line number (54ECH), the links are rebuilt (5523H), the ROM clears the variables (550EH) and READY is shown.

Findings

Strings in quotes, REM and DATA are not passed over: only bytes with the values of the tokens count, and BASIC does not tokenize text typed there, but a graphics character with the value of one of the tokens (for example 8DH, GOTO) followed by digits is taken as a reference. The number after AUTO is not a reference. A reference with too many digits shows Syntax error, and the digits from the one that does not fit are then read as a new number (53F3H). The line number in a message is written straight into the display memory at the cursor address 4020H, not through the ROM's output (5586H). The last new number (5698H) is not set before a line after the range is compared with it, so with no line in the range that test uses whatever 5698H held (52A6H). An Illegal function call or Overflow in the first walk, after a line whose new number is 0 (a line 0 before the range, or a new first number 0), rebuilds the links only up to that line: the end test still looks for 0000H there and takes that link as the end of the program, so the lines from there on are lost (5523H).

Differences from the Manual

The manual (Disk BASIC, RENUM, pages 5-46 and 5-47) gives the parameters, their defaults and the ! check as the bytes take them. What it does not say: an increment of 0, a range the wrong way round and a new first number not above the lines before the range are Illegal function call errors, as is the overlap with the lines after the range that the manual mentions; a number above 65529 is Overflow; the messages are Syntax error and Undefined line, each with in and the line, and all of them are shown before RENUM stops without renumbering; the ! check still makes the range tests; the variables are cleared after a renumbering; RENUM needs free memory of the program's growth plus 256 bytes, and 86 more, even when the program does not grow.

Variables

RENUM/CMD Variables

Address
Size
Contents
5689H-568AH
2 bytes
New number
nln, 10 as loaded; then the next new number (5689H).
568BH-568CH
2 bytes
Increment
inc, 10 as loaded (568BH).
568DH-568EH
2 bytes
First line
sln, 0 as loaded (568DH).
568FH-5690H
2 bytes
Last line
eln, 65529 as loaded (568FH).
5691H
1 byte
Check flag
21H after ! (5691H).
5692H
1 byte
Pass
00H the first, 01H the second (5692H).
5693H
1 byte
Error flag
01H once a message was shown (5693H).
5694H-5695H
2 bytes
Greatest growth
Of the program in the first pass (5694H).
5696H
1 byte
ON flag
An ON in the statement (5696H).
5697H
1 byte
Zero flag
A 0 is not a reference (5697H).
5698H-5699H
2 bytes
Last new number
The number given last (5698H).
569AH-569BH
2 bytes
Growth
The change in length so far (569AH).
569CH-569DH
2 bytes
Index entry
The next entry to match in the second pass (569CH).
569EH-56A1H
4 bytes
Line places
Where the current line is written to and read from (569EH, 56A0H).
56A2H-56A3H
2 bytes
Old line number
The line being read, for the messages (56A2H).
56A4H-56A5H
2 bytes
Digit place
Where a reference's digits are written (56A4H).
56A6H-56AAH
5 bytes
Work area
The digits made by the ROM's 132FH (56A6H).

Memory Outside RENUM/CMD

Address
Size
Contents
4020H-4021H
2 bytes
Cursor address
The line number of a message is written there (5586H).
40A0H-40A1H
2 bytes
String space
RENUM keeps 86 bytes below it free (54C2H).
40A4H-40A5H
2 bytes
TXTTAB
The start of the program (5286H).
40A7H-40A8H
2 bytes
Input buffer
The place of the index, 256 bytes (52EDH).
40F9H-40FAH
2 bytes
VARTAB
The end of the program, set again at the end (553EH).
4121H-4122H
2 bytes
Accumulator
The integer converted to digits (55ECH).

Outside Routines

AddressContents
4467HSYS0/SYS: DSPLY@, the heading (520CH).
402DHSYS0/SYS: EXIT@ (520FH).
1E5AHROM: a line number as digits into Register Pair DE (522BH and others).
1B2CHROM: find a line (5271H, 5279H).
132FHROM: an integer as decimal digits (55F7H).
032AHROM: output a character (55AEH, 5593H).
1997HROM: Syntax error (5263H).
1E4AHROM: Illegal function call (5243H, 526EH, 5280H).
19A2HROM: the error routine (55A8H).
1B5DHROM: clear the variables (5511H).
1A19HROM: READY (5514H).

Disassembly

5200H - CMD"RENUM"

RENUM/CMD's transfer address. RENUM is the renumberer of BASIC programs, run from Extended Disk BASIC as CMD"RENUM",nln,inc,sln,eln or CMD"RENUM",!: BASIC/CMD's CMD passes RENUM to CMNDI@ as a DOS command, and the DOS loads RENUM/CMD at 5200H and calls it. From the DOS command level (return address 402DH) it only shows its heading. From BASIC it takes the new first line number (5689H, 10 as loaded), the increment (568BH, 10), the first and last line of the range (568DH 0 and 568FH 65529) or the ! of the check alone (5691H), checks them against the program, and renumbers in two passes over the program. It ends at the ROM's READY.

5200
POP HL E1
Restore Register Pair HL with the return address on the top of the stack: 402DH (EXIT@) from the DOS command level, else the return into the caller of CMNDI@.
5201
LD DE,402DH 11 2D 40
Load Register Pair DE with 402DH, EXIT@, the return address that the DOS command level gives a program.
5204
OR A B7
OR Register A against itself, to clear the C FLAG for the SBC below.
5205
SBC HL,DE ED 52
SUBtract Register Pair DE (402DH) from Register Pair HL with the C FLAG: the Z FLAG when RENUM was run from the DOS command level.
5207
If the NZ FLAG is set (run from a program, BASIC's CMD), JUMP to 5212H.
5209
LD HL,5611H 21 11 56
Point Register Pair HL to 5611H, RENUM's heading and copyright line, ended by 0DH.
520C
GOSUB to 4467H, DSPLY@, to show the text at Register Pair HL.
520F
JUMP to 402DH, EXIT@, back to the DOS: RENUM works only from BASIC.
5212
POP HL E1
Restore Register Pair HL from the top of the stack: BASIC's text pointer after the string "RENUM". The ROM's return into its statement loop stays on the stack; RENUM never uses it, since it ends at READY or in an error.
5213
DEC HL 2B
DECrement Register Pair HL by 1, so that the RST 10H below reads the character at the text pointer.
5214
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the next character of the text that is not a space.
5215
CP 2CH FE 2C
Compare Register A against 2CH, a comma: the parameters follow it.
5217
If the NZ FLAG is set (no comma: CMD"RENUM" alone, with every value as loaded), JUMP to 5261H to check the end of the statement.
5219
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the comma, with the C FLAG for a digit.
521A
If the C FLAG is set (a digit: the new first line number), JUMP to 522BH.
521C
CP 21H FE 21
Compare Register A against 21H, the exclamation point of the check alone.
521E
If the NZ FLAG is set (neither a digit nor !: no new first line number), JUMP to 5232H for the comma before the increment.
5220
LD (5691H),A 32 91 56
Store Register A (21H) to 5691H, the check flag: the program is checked but not renumbered.
5223
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the !.
5224
CP 2CH FE 2C
Compare Register A against 2CH, a comma, which must follow the ! before any more parameters.
5226
If the NZ FLAG is set (no comma after the !), JUMP to 5261H to check the end of the statement.
5228
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the comma, with the C FLAG for a digit.
5229
If the NC FLAG is set (not a digit), JUMP to 5232H for the comma before the increment.
522B
GOSUB to 1E5AH, the ROM's scan of a line number at Register Pair HL into Register Pair DE (a number above 65529 is a Syntax error there); Register A returns the character after it.
522E
LD (5689H),DE ED 53 89 56
Store Register Pair DE into 5689H-568AH, the new first line number.
5232
CP 2CH FE 2C
Compare Register A against 2CH, a comma: the increment follows it.
5234
If the NZ FLAG is set (no comma), JUMP to 5261H to check the end of the statement.
5236
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the comma, with the C FLAG for a digit.
5237
If the NC FLAG is set (not a digit: the increment is left as it is), JUMP to 5247H for the next comma.
5239
GOSUB to 1E5AH, the ROM's scan of a line number at Register Pair HL into Register Pair DE; Register A returns the character after it.
523C
LD (568BH),DE ED 53 8B 56
Store Register Pair DE into 568BH-568CH, the increment.
5240
PUSH AF F5
Save Register Pair AF (the character after the number and its flags) to the top of the stack.
5241
LD A,D 7A
Load Register A with Register D, the high byte of the increment.
5242
OR E B3
OR Register A with Register E: the Z FLAG for an increment of 0.
5243
If the Z FLAG is set (an increment of 0), JUMP to 1E4AH, the ROM's Illegal function call error.
5246
POP AF F1
Restore Register Pair AF, the character after the increment, from the top of the stack.
5247
CP 2CH FE 2C
Compare Register A against 2CH, a comma: the first line of the range follows it.
5249
If the NZ FLAG is set (no comma), JUMP to 5261H to check the end of the statement.
524B
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the comma, with the C FLAG for a digit.
524C
If the NC FLAG is set (not a digit: the first line of the range is left at 0), JUMP to 5255H for the next comma.
524E
GOSUB to 1E5AH, the ROM's scan of a line number at Register Pair HL into Register Pair DE; Register A returns the character after it.
5251
LD (568DH),DE ED 53 8D 56
Store Register Pair DE into 568DH-568EH, the first line of the range.
5255
CP 2CH FE 2C
Compare Register A against 2CH, a comma: the last line of the range follows it.
5257
If the NZ FLAG is set (no comma), JUMP to 5261H to check the end of the statement.
5259
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character after the comma.
525A
GOSUB to 1E5AH, the ROM's scan of a line number at Register Pair HL into Register Pair DE (there is no test for a digit here); Register A returns the character after it.
525D
LD (568FH),DE ED 53 8F 56
Store Register Pair DE into 568FH-5690H, the last line of the range.
5261
DEC HL 2B
DECrement Register Pair HL by 1, so that the RST 10H below reads the current character again.
5262
RST 10H D7
RST 10H: increment Register Pair HL and load Register A with the character at the text pointer: the Z FLAG for the end of the statement (00H or a colon).
5263
If the NZ FLAG is set (more text after the parameters), JUMP to 1997H, the ROM's Syntax error.
5266
LD DE,(568DH) ED 5B 8D 56
Load Register Pair DE with the first line of the range from 568DH-568EH.
526A
LD HL,(568FH) 2A 8F 56
Load Register Pair HL with the last line of the range from 568FH-5690H.
526D
RST 18H DF
RST 18H: compare Register Pair HL (the last line) with Register Pair DE (the first line): the C FLAG when the last is below the first.
526E
If the C FLAG is set (the range is the wrong way round), JUMP to 1E4AH, the ROM's Illegal function call error.
5271
GOSUB to 1B2CH, the ROM's search for line Register Pair DE: Register Pair BC returns the address of the first line of the range (the line with that number or the next one).
5274
PUSH BC C5
Save Register Pair BC, the address of the first line of the range, to the top of the stack.
5275
LD DE,(5689H) ED 5B 89 56
Load Register Pair DE with the new first line number from 5689H-568AH.
5279
GOSUB to 1B2CH, the ROM's search for line Register Pair DE: Register Pair BC returns the address of the first line whose number is not below the new first line number.
527C
POP DE D1
Restore Register Pair DE, the address of the first line of the range, from the top of the stack.
527D
LD H,B 60
Load Register H with Register B, the high byte of that line's address.
527E
LD L,C 69
Load Register L with Register C: Register Pair HL the address of the first line not below the new first line number.
527F
RST 18H DF
RST 18H: compare Register Pair HL with Register Pair DE: the C FLAG when that line comes before the range.
5280
If the C FLAG is set (the new numbers would not be above the lines before the range), JUMP to 1E4AH, the ROM's Illegal function call error.

5283H - Number the Lines

The first walk over the program, from TXTTAB. Each line's new number is worked out and stored in its link (the line number itself is kept until the end): a line before the range keeps its number, a line in the range gets the next new number (Overflow above 65529), and a line after the range keeps its number, which must be above the last new number (else Illegal function call: RENUM does not move lines). The walk also counts the lines in Register Pair BC and checks that each link points to the line after it (else Internal error).

5283
LD BC,0000H 01 00 00
Load Register Pair BC with 0000H, the count of the lines.
5286
LD HL,(40A4H) 2A A4 40
Load Register Pair HL with TXTTAB from 40A4H-40A5H, the first line of the program.
5289
LD E,(HL) 5E
Load Register E with the low byte of the line's link at Register Pair HL.
528A
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the link.
528B
LD D,(HL) 56
Load Register D with the high byte of the link at Register Pair HL: Register Pair DE the address of the next line.
528C
LD A,D 7A
Load Register A with Register D, the high byte of the address of the next line.
528D
OR E B3
OR Register A with Register E: the Z FLAG for the link 0000H at the end of the program.
528E
If the Z FLAG is set (the end of the program), JUMP to 52D6H.
5290
PUSH DE D5
Save Register Pair DE, the address of the next line, to the top of the stack.
5291
PUSH HL E5
Save Register Pair HL, the address of the link's high byte, to the top of the stack.
5292
INC BC 03
INCrement Register Pair BC by 1, the count of the lines.
5293
INC HL 23
INCrement Register Pair HL by 1, to the line number.
5294
LD A,(HL) 7E
Load Register A with the low byte of the line number at Register Pair HL.
5295
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the line number.
5296
LD H,(HL) 66
Load Register H with the high byte of the line number at Register Pair HL.
5297
LD L,A 6F
Load Register L with Register A: Register Pair HL the line number.
5298
LD DE,(568DH) ED 5B 8D 56
Load Register Pair DE with the first line of the range from 568DH-568EH.
529C
RST 18H DF
RST 18H: compare Register Pair HL (the line number) with Register Pair DE: the C FLAG for a line before the range.
529D
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair DE the line number (the flags stay).
529E
If the C FLAG is set (a line before the range), JUMP to 52C8H: the line keeps its number.
52A0
LD HL,(568FH) 2A 8F 56
Load Register Pair HL with the last line of the range from 568FH-5690H.
52A3
RST 18H DF
RST 18H: compare Register Pair HL (the last line) with Register Pair DE (the line number): the NC FLAG for a line in the range.
52A4
If the NC FLAG is set (a line in the range), JUMP to 52AFH for its new number.
52A6
LD HL,(5698H) 2A 98 56
Load Register Pair HL with the last new number given, from 5698H-5699H.
52A9
RST 18H DF
RST 18H: compare Register Pair HL (the last new number) with Register Pair DE (the line after the range): the NC FLAG when the line is not above it.
52AA
If the NC FLAG is set (the renumbered lines would reach this line), JUMP to 559CH, Illegal function call.
52AD
JUMP to 52C8H: the line after the range keeps its number.
52AF
LD DE,(5689H) ED 5B 89 56
Load Register Pair DE with the new number for this line from 5689H-568AH.
52B3
LD (5698H),DE ED 53 98 56
Store Register Pair DE into 5698H-5699H, the last new number given.
52B7
LD HL,(568BH) 2A 8B 56
Load Register Pair HL with the increment from 568BH-568CH.
52BA
ADD HL,DE 19
ADD Register Pair DE to Register Pair HL: the new number of the next line in the range.
52BB
LD (5689H),HL 22 89 56
Store Register Pair HL into 5689H-568AH, the next new number.
52BE
If the C FLAG is set (the next number would pass 65535), JUMP to 559FH, Overflow.
52C1
LD HL,FFF9H 21 F9 FF
Load Register Pair HL with FFF9H, 65529, the highest line number.
52C4
RST 18H DF
RST 18H: compare Register Pair HL (65529) with Register Pair DE (the new number): the C FLAG when the new number is above 65529.
52C5
If the C FLAG is set (too high), JUMP to 559FH, Overflow.
52C8
POP HL E1
Restore Register Pair HL, the address of the link's high byte, from the top of the stack.
52C9
LD (HL),D 72
Store Register D, the high byte of the line's new number, into the high byte of its link at Register Pair HL.
52CA
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte of the link.
52CB
LD (HL),E 73
Store Register E, the low byte of the new number, into the low byte of the link at Register Pair HL: the link now holds the new number.
52CC
GOSUB to 5517H to move Register Pair HL past the link, the line number and the text to the next line.
52CF
POP DE D1
Restore Register Pair DE, the address of the next line from the old link, from the top of the stack.
52D0
RST 18H DF
RST 18H: compare Register Pair HL (the line found after the text) with Register Pair DE (the old link): the Z FLAG when they agree.
52D1
If the Z FLAG is set (the link was right), LOOP BACK to 5289H for the next line.
52D3
JUMP to 55A2H, Internal error: the line's link does not point to the line after it.
52D6
DEC A 3D
DECrement Register A (00H, from the test of the link) by 1: FFH, the new end mark.
52D7
LD (HL),A 77
Store Register A (FFH) into the high byte of the end of the program's link at Register Pair HL.
52D8
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte of that link.
52D9
LD (HL),A 77
Store Register A (FFH) into the low byte at Register Pair HL: the end of the program is marked FFFFH, since a link may now hold the line number 0.
52DA
LD (5507H),A 32 07 55
Store Register A (FFH) to 5507H, the operand of the LD A,00H at 5506H: from now on 5506H tests for the end mark FFFFH.

52DDH - The Index

Builds a table of 128 line addresses in BASIC's input buffer (the address at 40A7H): with N lines in the program and a step S of N/128 rounded up (Register C), entry k is the address of line k times S, or the end of the program once that is passed. The table lets 53FFH find a line by a binary search before it walks the last few lines.

52DD
PUSH BC C5
Save Register Pair BC, the count of the lines, to the top of the stack.
52DE
POP HL E1
Restore the count of the lines into Register Pair HL from the top of the stack.
52DF
LD C,00H 0E 00
Load Register C with 00H, the step between the lines of two entries.
52E1
LD DE,0080H 11 80 00
Load Register Pair DE with 0080H, 128, the number of entries.
52E4
INC C 0C
INCrement Register C by 1, the step between the lines of two entries.
52E5
SBC HL,DE ED 52
SUBtract Register Pair DE (128) from Register Pair HL with the C FLAG.
52E7
If the Z FLAG is set (no lines left over), JUMP to 52EBH.
52E9
If the NC FLAG is set (lines left over), LOOP BACK to 52E4H: Register C becomes the count divided by 128, rounded up (at least 1).
52EB
LD B,80H 06 80
Load Register B with 80H, the count of the entries for the DJNZ.
52ED
LD DE,(40A7H) ED 5B A7 40
Load Register Pair DE with the address of the input buffer from 40A7H-40A8H, the place of the table.
52F1
LD HL,(40A4H) 2A A4 40
Load Register Pair HL with TXTTAB from 40A4H-40A5H, the first line.
52F4
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair HL the entry, Register Pair DE the line.
52F5
LD (HL),E 73
Store Register E, the low byte of the line's address, into the entry at Register Pair HL.
52F6
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
52F7
LD (HL),D 72
Store Register D, the high byte of the line's address, into the entry at Register Pair HL.
52F8
INC HL 23
INCrement Register Pair HL by 1, to the next entry.
52F9
PUSH HL E5
Save Register Pair HL, the next entry, to the top of the stack.
52FA
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair HL the line.
52FB
LD E,C 59
Load Register E with Register C, the step: the lines to pass over to the next entry's line.
52FC
JUMP to 5304H to test for the end of the program first.
52FE
GOSUB to 5517H to move Register Pair HL to the next line.
5301
DEC E 1D
DECrement Register E by 1, the lines still to pass over.
5302
If the Z FLAG is set (the step is done), JUMP to 5309H.
5304
GOSUB to 5506H: the Z FLAG when the link at Register Pair HL is FFFFH, the end of the program.
5307
If the NZ FLAG is set (a line), LOOP BACK to 52FEH; at the end the entry stays at the end of the program.
5309
POP DE D1
Restore Register Pair DE, the next entry, from the top of the stack.
530A
DECrement Register B and LOOP BACK to 52F4H until all 128 entries are stored.

530CH - A Pass over the Program

Each pass reads every line from Register Pair HL. In the first pass (5692H = 00H) nothing is written: the line references are found and checked, the change in the length of the program is added up (569AH) and the greatest growth is kept (5694H). In the second pass (5692H = 01H) the program has been moved up (54A6H) and every byte read is written again from TXTTAB on at Register Pair BC (5545H), with each line reference replaced by its new number. Register Pair DE is set to 0000H for the length count at the start of each pass.

530C
LD HL,(40A4H) 2A A4 40
Load Register Pair HL with TXTTAB from 40A4H-40A5H, the first line to read (the first pass).
530F
LD DE,0000H 11 00 00
Load Register Pair DE with 0000H, the change in the length of the program so far.
5312
LD BC,(40A4H) ED 4B A4 40
Load Register Pair BC with TXTTAB from 40A4H-40A5H, the place to write to (used in the second pass).
5316
LD (569AH),DE ED 53 9A 56
Store Register Pair DE (0000H) into 569AH-569BH, the change in length.
531A
LD (569EH),BC ED 43 9E 56
Store Register Pair BC into 569EH-569FH, the place the line is written to.
531E
LD (56A0H),HL 22 A0 56
Store Register Pair HL into 56A0H-56A1H, the place the line is read from.
5321
GOSUB to 5506H: the Z FLAG when the link at Register Pair HL is FFFFH, the end of the program.
5324
If the Z FLAG is set (the end), JUMP to 5493H, the end of the pass.
5327
LD A,(5692H) 3A 92 56
Load Register A with the pass from 5692H: 00H the first, 01H the second.
532A
OR A B7
OR Register A against itself: the Z FLAG for the first pass.
532B
If the Z FLAG is set (the first pass), JUMP to 534DH.
532D
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair DE the place the line is read from.
532E
LD HL,(569CH) 2A 9C 56
Load Register Pair HL with the next entry of the index to match, from 569CH-569DH.
5331
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
5332
LD A,(HL) 7E
Load Register A with the high byte of the entry's line address at Register Pair HL.
5333
CP D BA
Compare Register A against Register D, the high byte of the line being read.
5334
If the NZ FLAG is set (not this line), JUMP to 534CH.
5336
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte of the entry.
5337
LD A,(HL) 7E
Load Register A with the low byte of the entry's line address at Register Pair HL.
5338
CP E BB
Compare Register A against Register E, the low byte of the line being read.
5339
If the NZ FLAG is set (not this line), JUMP to 534CH.
533B
LD (HL),C 71
Store Register C, the low byte of the place the line is written to, into the entry at Register Pair HL.
533C
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
533D
LD (HL),B 70
Store Register B, the high byte, at Register Pair HL: the entry now holds the line's new address.
533E
INC HL 23
INCrement Register Pair HL by 1, to the next entry.
533F
PUSH DE D5
Save Register Pair DE, the place the line is read from, to the top of the stack.
5340
LD DE,(40A7H) ED 5B A7 40
Load Register Pair DE with the address of the input buffer from 40A7H-40A8H.
5344
INC D 14
INCrement Register D by 1: Register Pair DE the end of the 256 byte index.
5345
RST 18H DF
RST 18H: compare Register Pair HL (the next entry) with Register Pair DE: the Z FLAG at the end of the index.
5346
POP DE D1
Restore Register Pair DE, the place the line is read from, from the top of the stack.
5347
If the Z FLAG is set (the last entry was matched), JUMP to 534CH without moving on.
5349
LD (569CH),HL 22 9C 56
Store Register Pair HL into 569CH-569DH, the next entry of the index to match.
534C
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair HL the place the line is read from again.
534D
GOSUB to 5545H to take the low byte of the link (the new number), and write it in the second pass.
5350
GOSUB to 5545H to take the high byte of the link.
5353
GOSUB to 5545H to take the low byte of the line number (the old number).
5356
LD E,A 5F
Load Register E with Register A, the low byte of the old line number.
5357
GOSUB to 5545H to take the high byte of the line number.
535A
LD D,A 57
Load Register D with Register A: Register Pair DE the old line number.
535B
LD (56A2H),DE ED 53 A2 56
Store Register Pair DE into 56A2H-56A3H, the number of the line, for the error messages.

535FH - The Statements

Reads the text of a line byte by byte (copied in the second pass). Each statement starts with the flags 5696H (an ON seen) and 5697H (a 0 is not a line reference) cleared. The tokens that take line numbers are GOTO, GOSUB and EDIT (a list of numbers separated by commas), RUN, THEN, ELSE and RESUME (a number, then a list as for GOTO), ERL (numbers after AND, OR and the relations), DELETE, LIST and LLIST (a range with . and -), and ON and ERROR set the flags for ON ERROR GOTO. Strings in quotes, REM and DATA are not passed over.

535F
XOR A AF
Load Register A with 00H, to clear the two flags of the statement.
5360
LD (5697H),A 32 97 56
Store Register A to 5697H, the flag that a 0 is not a line reference (ON ERROR GOTO 0, RESUME 0).
5363
LD (5696H),A 32 96 56
Store Register A to 5696H, the flag that an ON was seen in the statement.
5366
GOSUB to 5542H to pass over (and copy) spaces and line feeds and take the next byte of the text into Register A.
5369
OR A B7
OR Register A against itself: the Z FLAG for the 00H at the end of the line.
536A
If the Z FLAG is set (the end of the line), JUMP back to 531AH for the next line.
536C
CP 3AH FE 3A
Compare Register A against 3AH, a colon: the end of the statement.
536E
If the Z FLAG is set (a colon), JUMP back to 535FH to clear the flags for the next statement.
5370
CP 8DH FE 8D
Compare Register A against 8DH, the token of GOTO.
5372
If the Z FLAG is set (GOTO), JUMP to 53BFH for a list of line numbers.
5374
CP 91H FE 91
Compare Register A against 91H, the token of GOSUB.
5376
If the Z FLAG is set (GOSUB), JUMP to 53BFH for a list of line numbers.
5378
CP 9DH FE 9D
Compare Register A against 9DH, the token of EDIT.
537A
If the Z FLAG is set (EDIT), JUMP to 53BFH for a list of line numbers.
537C
CP 8EH FE 8E
Compare Register A against 8EH, the token of RUN.
537E
If the Z FLAG is set (RUN), JUMP to 53B7H for a line number if a digit follows.
5380
CP CAH FE CA
Compare Register A against CAH, the token of THEN.
5382
If the Z FLAG is set (THEN), JUMP to 53B7H for a line number if a digit follows.
5384
CP 95H FE 95
Compare Register A against 95H, the token of ELSE.
5386
If the Z FLAG is set (ELSE), JUMP to 53B7H for a line number if a digit follows.
5388
CP 9EH FE 9E
Compare Register A against 9EH, the token of ERROR.
538A
If the Z FLAG is set (ERROR), JUMP to 53A8H to see whether an ON came before it.
538C
CP 9FH FE 9F
Compare Register A against 9FH, the token of RESUME.
538E
If the Z FLAG is set (RESUME), JUMP to 53B2H.
5390
CP C2H FE C2
Compare Register A against C2H, the token of ERL.
5392
If the Z FLAG is set (ERL), JUMP to 53CDH for the numbers it is compared with.
5394
CP B6H FE B6
Compare Register A against B6H, the token of DELETE.
5396
If the Z FLAG is set (DELETE), JUMP to 53DFH for a range of lines.
5398
CP B4H FE B4
Compare Register A against B4H, the token of LIST.
539A
If the Z FLAG is set (LIST), JUMP to 53DFH for a range of lines.
539C
CP B5H FE B5
Compare Register A against B5H, the token of LLIST.
539E
If the Z FLAG is set (LLIST), JUMP to 53DFH for a range of lines.
53A0
CP A1H FE A1
Compare Register A against A1H, the token of ON.
53A2
If the NZ FLAG is set (any other byte), LOOP BACK to 5366H for the next byte.
53A4
LD A,01H 3E 01
Load Register A with 01H, the value of a flag that is set.
53A6
JUMP to 5363H to set 5696H, an ON in this statement, and go on with the next byte.
53A8
LD A,(5696H) 3A 96 56
Load Register A with the ON flag of the statement from 5696H.
53AB
OR A B7
OR Register A against itself: the Z FLAG when no ON came before the ERROR.
53AC
If the Z FLAG is set (the ERROR statement), JUMP back to 5366H: no line number follows.
53AE
LD A,01H 3E 01
Load Register A with 01H, the value of a flag that is set.
53B0
JUMP to 5360H to set 5697H and 5696H: ON ERROR GOTO 0 is not a reference to line 0.
53B2
LD A,01H 3E 01
Load Register A with 01H, the value of a flag that is set.
53B4
LD (5697H),A 32 97 56
Store Register A (01H) to 5697H: RESUME 0 is not a reference to line 0.
53B7
GOSUB to 555CH for the next byte: the C FLAG for a digit (not yet taken), else the byte taken into Register A.
53BA
If the NC FLAG is set (no number), JUMP back to 5369H to handle the byte taken.
53BC
GOSUB to 53F0H to take the line number at Register Pair HL and look it up.
53BF
GOSUB to 555CH for the next byte: the C FLAG for a digit (not yet taken), else the byte taken into Register A.
53C2
If the C FLAG is set (a digit), JUMP to 53BCH for the line number.
53C4
CP 2CH FE 2C
Compare Register A against 2CH, a comma: another number of the list may follow.
53C6
If the Z FLAG is set (a comma), LOOP BACK to 53BFH.
53C8
JUMP back to 5369H to handle the byte taken, which ends the list of numbers.
53CA
GOSUB to 53F0H to take the line number at Register Pair HL and look it up.
53CD
GOSUB to 555CH for the next byte: the C FLAG for a digit (not yet taken), else the byte taken into Register A.
53D0
If the C FLAG is set (a digit), JUMP to 53CAH for the line number.
53D2
CP D2H FE D2
Compare Register A against D2H, the token of AND.
53D4
If the C FLAG is set (below the tokens AND, OR, >, = and <), JUMP back to 5369H to handle the byte taken.
53D6
CP D7H FE D7
Compare Register A against D7H, the token after <.
53D8
If the C FLAG is set (AND, OR, >, = or <), LOOP BACK to 53CDH for a number after it.
53DA
JUMP back to 5369H to handle the byte taken, which ends the numbers after ERL.
53DC
GOSUB to 53F0H to take the line number at Register Pair HL and look it up.
53DF
GOSUB to 555CH for the next byte: the C FLAG for a digit (not yet taken), else the byte taken into Register A.
53E2
If the C FLAG is set (a digit), JUMP to 53DCH for the line number.
53E4
CP 2EH FE 2E
Compare Register A against 2EH, the period (the current line).
53E6
If the Z FLAG is set (a period), GOSUB to 5542H to pass over spaces and take the byte after the period.
53E9
CP CEH FE CE
Compare Register A against CEH, the token of the minus sign, between the two lines of a range.
53EB
If the Z FLAG is set (a minus sign), JUMP to 53B7H for the last line of the range.
53ED
JUMP back to 5369H to handle the byte taken, which ends the range of lines.

53F0H - A Line Reference

Takes the number at Register Pair HL (55B4H) and looks up the line. A number above 6552 with another digit after it gives the message Syntax error; a 0 after ON ERROR GOTO or RESUME is left alone. The line is found through the index (a binary search) and the lines after the entry found. A line that does not exist gives the message Undefined line. In the first pass the length of the new number is counted; in the second the new number is written over the old one.

53F0
GOSUB to 55B4H to take the decimal number at Register Pair HL into Register Pair DE (copied in the second pass); the C FLAG when it is too large.
53F3
If the C FLAG is set (too large), JUMP to 5568H to show Syntax error and return; the digits not yet taken are then read as a new number.
53F6
LD A,D 7A
Load Register A with Register D, the high byte of the number taken.
53F7
OR E B3
OR Register A with Register E: the Z FLAG for the number 0.
53F8
If the NZ FLAG is set (not 0), JUMP to 53FFH to look the line up.
53FA
LD A,(5697H) 3A 97 56
Load Register A with the flag from 5697H, set by ON ERROR and by RESUME.
53FD
OR A B7
OR Register A against itself: the NZ FLAG after ON ERROR GOTO or RESUME.
53FE
RET NZ C0
If the NZ FLAG is set (the 0 is not a reference), RETurn.
53FF
PUSH HL E5
Save Register Pair HL, the text pointer, to the top of the stack.
5400
PUSH BC C5
Save Register Pair BC, the place to write to, to the top of the stack.
5401
LD BC,0080H 01 80 00
Load Register Pair BC with 0080H, the first step of the search: 64 entries of 2 bytes.
5404
LD HL,(40A7H) 2A A7 40
Load Register Pair HL with the address of the input buffer from 40A7H-40A8H, the first entry of the index.
5407
ADD HL,BC 09
ADD Register Pair BC (the step) to Register Pair HL, the entry to try.
5408
PUSH HL E5
Save Register Pair HL, the entry, to the top of the stack.
5409
LD A,(HL) 7E
Load Register A with the low byte of the line address in the entry at Register Pair HL.
540A
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
540B
LD H,(HL) 66
Load Register H with the high byte of the line address in the entry at Register Pair HL.
540C
LD L,A 6F
Load Register L with Register A: Register Pair HL the line of the entry.
540D
GOSUB to 5506H: the Z FLAG when the link at Register Pair HL is FFFFH, the end of the program.
5410
SCF 37
Set the C FLAG: the end counts as above the number wanted.
5411
If the Z FLAG is set (the end), JUMP to 541DH with the C FLAG.
5413
INC HL 23
INCrement Register Pair HL by 1, past the low byte of the link.
5414
INC HL 23
INCrement Register Pair HL by 1, to the low byte of the line number.
5415
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the line number.
5416
LD A,D 7A
Load Register A with Register D, the high byte of the number wanted.
5417
CP (HL) BE
Compare Register A against the high byte of the line number at Register Pair HL: the C FLAG when the number wanted is lower.
5418
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte (the flags stay).
5419
If the NZ FLAG is set (the high bytes differ), JUMP to 541DH.
541B
LD A,E 7B
Load Register A with Register E, the low byte of the number wanted.
541C
CP (HL) BE
Compare Register A against the low byte of the line number at Register Pair HL: the C FLAG when the number wanted is lower.
541D
POP HL E1
Restore Register Pair HL, the entry, from the top of the stack.
541E
If the NC FLAG is set (the line of the entry is not above the number wanted), JUMP to 5423H to keep the entry.
5420
OR A B7
OR Register A against itself, to clear the C FLAG for the SBC below.
5421
SBC HL,BC ED 42
SUBtract Register Pair BC (the step) from Register Pair HL with the C FLAG: back to the entry before the try.
5423
SRL C CB 39
Shift Register C right one bit: half the step.
5425
LD A,C 79
Load Register A with Register C, the step of the search in bytes.
5426
CP 02H FE 02
Compare Register A against 02H, a step of one entry: the C FLAG below it.
5428
If the NC FLAG is set (a step of at least one entry), LOOP BACK to 5407H; Register Pair HL ends at the last entry whose line is not above the number wanted (or the first entry).
542A
LD A,(HL) 7E
Load Register A with the low byte of the line address in the entry at Register Pair HL.
542B
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
542C
LD H,(HL) 66
Load Register H with the high byte of the line address in the entry at Register Pair HL.
542D
LD L,A 6F
Load Register L with Register A: Register Pair HL the line to start the walk from.
542E
LD A,(5692H) 3A 92 56
Load Register A with the pass from 5692H: 00H the first, 01H the second.
5431
OR A B7
OR Register A against itself: the Z FLAG for the first pass.
5432
If the Z FLAG is set (the first pass), JUMP to 5443H.
5434
LD BC,(569EH) ED 4B 9E 56
Load Register Pair BC with the place the current line is written to, from 569EH-569FH.
5438
LD A,H 7C
Load Register A with Register H, the high byte of the line's address.
5439
CP B B8
Compare Register A against Register B, the high byte of the current line's new address.
543A
If the NZ FLAG is set (not the current line), JUMP to 5443H.
543C
LD A,L 7D
Load Register A with Register L, the low byte of the line's address.
543D
CP C B9
Compare Register A against Register C, the low byte of the current line's new address.
543E
If the NZ FLAG is set (not the current line), JUMP to 5443H.
5440
LD HL,(56A0H) 2A A0 56
Load Register Pair HL with the place the current line is read from, from 56A0H-56A1H: its new copy is not yet complete.
5443
GOSUB to 5506H: the Z FLAG when the link at Register Pair HL is FFFFH, the end of the program.
5446
If the Z FLAG is set (the end: no such line), JUMP to 545BH.
5448
INC HL 23
INCrement Register Pair HL by 1, past the low byte of the link.
5449
INC HL 23
INCrement Register Pair HL by 1, to the low byte of the line number.
544A
LD C,(HL) 4E
Load Register C with the low byte of the line number at Register Pair HL.
544B
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the line number.
544C
LD A,(HL) 7E
Load Register A with the high byte of the line number at Register Pair HL.
544D
CP D BA
Compare Register A against Register D, the high byte of the number wanted.
544E
If the NZ FLAG is set (the high bytes differ), JUMP to 5452H.
5450
LD A,C 79
Load Register A with Register C, the low byte of the line number.
5451
CP E BB
Compare Register A against Register E, the low byte of the number wanted.
5452
If the Z FLAG is set (the line wanted), JUMP to 5461H.
5454
If the NC FLAG is set (a line above the number wanted: no such line), JUMP to 545BH.
5456
GOSUB to 551AH to move Register Pair HL past the text to the next line.
5459
LOOP BACK to 542EH to try the next line, which may be the current line.
545B
GOSUB to 556BH to show Undefined line in, and the number of the line being read.
545E
POP BC C1
Restore Register Pair BC, the place to write to, from the top of the stack.
545F
POP HL E1
Restore Register Pair HL, the text pointer, from the top of the stack.
5460
RET C9
RETurn to the caller: the reference is left as it is in the text.
5461
POP BC C1
Restore Register Pair BC, the place to write to, from the top of the stack.
5462
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte of the line number.
5463
DEC HL 2B
DECrement Register Pair HL by 1, to the high byte of the line's link.
5464
LD D,(HL) 56
Load Register D with the high byte of the link at Register Pair HL, which holds the line's new number.
5465
DEC HL 2B
DECrement Register Pair HL by 1, to the low byte of the link.
5466
LD E,(HL) 5E
Load Register E with the low byte of the link at Register Pair HL: Register Pair DE the new number.
5467
LD A,(5692H) 3A 92 56
Load Register A with the pass from 5692H: 00H the first, 01H the second.
546A
OR A B7
OR Register A against itself: the Z FLAG for the first pass.
546B
If the Z FLAG is set (the first pass), JUMP to 5476H to count the length.
546D
LD BC,(56A4H) ED 4B A4 56
Load Register Pair BC with the place where the reference's digits were written, from 56A4H-56A5H.
5471
GOSUB to 55EBH to write the new number's digits at Register Pair BC, over the old ones; Register Pair BC returns past them.
5474
POP HL E1
Restore Register Pair HL, the text pointer, from the top of the stack.
5475
RET C9
RETurn to the caller with the new number written into the program.
5476
LD BC,56A6H 01 A6 56
Point Register Pair BC to 56A6H, the work area, for the digits.
5479
GOSUB to 55EBH to make the new number's digits; Register Pair DE returns the count of the digits.
547C
LD HL,(569AH) 2A 9A 56
Load Register Pair HL with the change in length from 569AH-569BH (the old digits already taken off).
547F
ADD HL,DE 19
ADD Register Pair DE (the new digits) to Register Pair HL.
5480
LD (569AH),HL 22 9A 56
Store Register Pair HL into 569AH-569BH, the change in length so far.
5483
BIT 7,H CB 7C
Test bit 7 of Register H: the NZ FLAG when the program is shorter so far.
5485
If the NZ FLAG is set (no growth), JUMP to 5491H.
5487
LD DE,(5694H) ED 5B 94 56
Load Register Pair DE with the greatest growth so far from 5694H-5695H.
548B
RST 18H DF
RST 18H: compare Register Pair HL with Register Pair DE: the C FLAG when the growth is less than the greatest.
548C
If the C FLAG is set (less than the greatest), JUMP to 5491H.
548E
LD (5694H),HL 22 94 56
Store Register Pair HL into 5694H-5695H, the greatest growth of the program so far.
5491
POP HL E1
Restore Register Pair HL, the text pointer, from the top of the stack.
5492
RET C9
RETurn to the caller with the length of the new number counted.

5493H - The End of a Pass

After the first pass the work ends if only a check was wanted (!) or a reference was wrong; the line numbers are then as they were. Otherwise the program is moved up by the greatest growth plus 256 bytes (Out of memory if it would come within 86 bytes of the string space at 40A0H), the index entries are moved with it, and the second pass writes it back down from TXTTAB. After the second pass each line's new number is moved from its link into its line number (54ECH). Then the links are rebuilt (5523H), the variables cleared (the ROM's 1B5DH) and READY shown.

5493
LD A,(5691H) 3A 91 56
Load Register A with the check flag from 5691H.
5496
OR A B7
OR Register A against itself: the NZ FLAG for CMD"RENUM",!.
5497
If the NZ FLAG is set (a check only), JUMP to 550EH to rebuild the links and end.
5499
LD A,(5693H) 3A 93 56
Load Register A with the error flag from 5693H.
549C
OR A B7
OR Register A against itself: the NZ FLAG when a message was shown.
549D
If the NZ FLAG is set (a wrong reference), JUMP to 550EH: the program is not renumbered.
549F
LD A,(5692H) 3A 92 56
Load Register A with the pass from 5692H: 00H the first, 01H the second.
54A2
XOR 01H EE 01
XOR Register A against 01H: the Z FLAG when the second pass has just ended.
54A4
If the Z FLAG is set (the second pass is done), JUMP to 54ECH.
54A6
LD (5692H),A 32 92 56
Store Register A (01H) to 5692H: the second pass.
54A9
LD DE,(40A4H) ED 5B A4 40
Load Register Pair DE with TXTTAB from 40A4H-40A5H.
54AD
LD HL,(40F9H) 2A F9 40
Load Register Pair HL with VARTAB from 40F9H-40FAH, the end of the program.
54B0
SBC HL,DE ED 52
SUBtract Register Pair DE from Register Pair HL with the C FLAG (clear after the XOR): the length of the program.
54B2
PUSH HL E5
Save Register Pair HL, the length, to the top of the stack.
54B3
POP BC C1
Restore the length of the program into Register Pair BC from the top of the stack.
54B4
LD HL,(5694H) 2A 94 56
Load Register Pair HL with the greatest growth from 5694H-5695H.
54B7
INC H 24
INCrement Register H by 1: 256 bytes more, the distance the program is moved.
54B8
PUSH HL E5
Save Register Pair HL, the distance, to the top of the stack.
54B9
ADD HL,DE 19
ADD Register Pair DE (TXTTAB) to Register Pair HL.
54BA
ADD HL,BC 09
ADD Register Pair BC (the length) to Register Pair HL: the end of the moved program.
54BB
If the C FLAG is set (past FFFFH), JUMP to 5599H, Out of memory.
54BE
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair DE the end of the moved program, Register Pair HL TXTTAB.
54BF
ADD HL,BC 09
ADD Register Pair BC (the length) to Register Pair HL: the end of the program now.
54C0
PUSH BC C5
Save Register Pair BC, the length, to the top of the stack.
54C1
PUSH HL E5
Save Register Pair HL, the end of the program, to the top of the stack.
54C2
LD HL,(40A0H) 2A A0 40
Load Register Pair HL with the start of the string space from 40A0H-40A1H.
54C5
LD BC,FFAAH 01 AA FF
Load Register Pair BC with FFAAH, -86, the room to keep below the string space.
54C8
ADD HL,BC 09
ADD Register Pair BC to Register Pair HL: 86 bytes below the string space.
54C9
RST 18H DF
RST 18H: compare Register Pair HL with Register Pair DE (the end of the moved program): the C FLAG when there is no room.
54CA
If the C FLAG is set (no room), JUMP to 5599H, Out of memory.
54CD
POP HL E1
Restore Register Pair HL, the end of the program, from the top of the stack.
54CE
POP BC C1
Restore Register Pair BC, the length, from the top of the stack.
54CF
INC BC 03
INCrement Register Pair BC by 1, the bytes to move.
54D0
LDDR ED B8
Move Register Pair BC bytes from Register Pair HL down to Register Pair DE (LDDR): the program goes up by the distance, from its end down.
54D2
INC DE 13
INCrement Register Pair DE by 1: the start of the moved program.
54D3
POP BC C1
Restore Register Pair BC, the distance, from the top of the stack.
54D4
PUSH DE D5
Save Register Pair DE, the start of the moved program, to the top of the stack.
54D5
LD HL,(40A7H) 2A A7 40
Load Register Pair HL with the address of the input buffer from 40A7H-40A8H, the first entry of the index.
54D8
LD (569CH),HL 22 9C 56
Store Register Pair HL into 569CH-569DH, the next entry of the index to match in the second pass.
54DB
LD E,80H 1E 80
Load Register E with 80H, the count of the 128 entries of the index.
54DD
LD A,(HL) 7E
Load Register A with the low byte of the entry at Register Pair HL.
54DE
ADD A,C 81
ADD Register C, the low byte of the distance, to Register A.
54DF
LD (HL),A 77
Store Register A back into the low byte of the entry at Register Pair HL.
54E0
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the entry.
54E1
LD A,(HL) 7E
Load Register A with the high byte of the entry at Register Pair HL.
54E2
ADC A,B 88
ADD Register B, the high byte of the distance, and the C FLAG to Register A.
54E3
LD (HL),A 77
Store Register A back at Register Pair HL: the entry points to its line in the moved program.
54E4
INC HL 23
INCrement Register Pair HL by 1, to the next entry.
54E5
DEC E 1D
DECrement Register E by 1, the count of the entries left to move.
54E6
If the NZ FLAG is set (more entries), LOOP BACK to 54DDH.
54E8
POP HL E1
Restore Register Pair HL, the start of the moved program, from the top of the stack: the place to read from.
54E9
JUMP to 530FH for the second pass, which writes from TXTTAB.
54EC
GOSUB to 5545H to copy the low byte of the end mark FFFFH.
54EF
GOSUB to 5545H to copy the high byte of the end mark FFFFH.
54F2
LD HL,(40A4H) 2A A4 40
Load Register Pair HL with TXTTAB from 40A4H-40A5H, the first line.
54F5
GOSUB to 5506H: the Z FLAG when the link at Register Pair HL is FFFFH, the end of the program.
54F8
If the Z FLAG is set (the end), JUMP to 550EH.
54FA
LD E,(HL) 5E
Load Register E with the low byte of the link at Register Pair HL, the new number.
54FB
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the link.
54FC
LD D,(HL) 56
Load Register D with the high byte of the link at Register Pair HL.
54FD
INC HL 23
INCrement Register Pair HL by 1, to the line number.
54FE
LD (HL),E 73
Store Register E into the low byte of the line number at Register Pair HL.
54FF
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the line number.
5500
LD (HL),D 72
Store Register D into the high byte at Register Pair HL: the line has its new number.
5501
GOSUB to 551AH to move Register Pair HL past the text to the next line.
5504
LOOP BACK to 54F5H for the next line of the program.

5506H - The End Test

The Z FLAG when the word at Register Pair HL is the end mark: 0000H as loaded, FFFFH after 52DAH has stored FFH into the operand at 5507H.

5506
LD A,00H 3E 00
Load Register A with the end mark's byte: 00H as loaded, FFH once 52DAH has changed this operand (5507H).
5508
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the link.
5509
CP (HL) BE
Compare Register A against the high byte of the link at Register Pair HL.
550A
DEC HL 2B
DECrement Register Pair HL by 1, back to the link (the flags stay).
550B
RET NZ C0
If the NZ FLAG is set (not the end mark), RETurn with the NZ FLAG.
550C
CP (HL) BE
Compare Register A against the low byte of the link at Register Pair HL: the Z FLAG for the end mark.
550D
RET C9
RETurn to the caller with the Z FLAG at the end mark.
550E
GOSUB to 5523H to rebuild the links of the program and set VARTAB.
5511
GOSUB to 1B5DH, the ROM's clearing of the variables (as for RUN), since the program may have moved.
5514
JUMP to 1A19H, the ROM's READY: RENUM is done.

5517H - The Next Line

5517H moves Register Pair HL from a line's link past the link, the line number and the text to the next line; 551AH does the same from the line number's high byte. The text is passed over with CPIR up to its 00H; Register Pair BC is kept.

5517
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the link.
5518
INC HL 23
INCrement Register Pair HL by 1, to the low byte of the line number.
5519
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the line number.
551A
INC HL 23
INCrement Register Pair HL by 1, to the text.
551B
XOR A AF
Load Register A with 00H, the end of the text.
551C
PUSH BC C5
Save Register Pair BC to the top of the stack.
551D
LD B,A 47
Load Register B with Register A (00H), the high byte of the count.
551E
LD C,A 4F
Load Register C with Register A (00H): Register Pair BC 0000H, so that CPIR may run up to 65536 bytes.
551F
CPIR ED B1
Search from Register Pair HL on for the byte in Register A, the 00H, counting Register Pair BC down (CPIR): Register Pair HL returns the byte after it, the next line.
5521
POP BC C1
Restore Register Pair BC from the top of the stack.
5522
RET C9
RETurn to the caller with Register Pair HL at the next line.

5523H - Rebuild the Links

Walks the program from TXTTAB by its text (5517H), not by its links, and stores in each link the address of the line after it; the end mark becomes 0000H again and VARTAB is set after it. The end is found by 5506H, so before 52DAH a link that already holds the new number 0 is taken as the end.

5523
LD DE,(40A4H) ED 5B A4 40
Load Register Pair DE with TXTTAB from 40A4H-40A5H.
5527
LD H,D 62
Load Register H with Register D, the high byte of the line's address.
5528
LD L,E 6B
Load Register L with Register E: Register Pair HL the line.
5529
GOSUB to 5506H: the Z FLAG at the end mark of the program.
552C
If the Z FLAG is set (the end), JUMP to 5538H.
552E
GOSUB to 5517H to move Register Pair HL to the next line.
5531
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair DE the next line, Register Pair HL this line.
5532
LD (HL),E 73
Store Register E, the low byte of the next line's address, into the link at Register Pair HL.
5533
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the link.
5534
LD (HL),D 72
Store Register D, the high byte, at Register Pair HL: the link points to the next line.
5535
JUMP back to 5527H with Register Pair DE the next line.
5538
LD (HL),00H 36 00
Store 00H into the low byte of the end mark at Register Pair HL.
553A
INC HL 23
INCrement Register Pair HL by 1, to the high byte of the end mark.
553B
LD (HL),00H 36 00
Store 00H into the high byte at Register Pair HL: the end of the program is 0000H again.
553D
INC HL 23
INCrement Register Pair HL by 1, past the end of the program.
553E
LD (40F9H),HL 22 F9 40
Store Register Pair HL into 40F9H-40FAH, VARTAB, the start of the variables.
5541
RET C9
RETurn to the caller with VARTAB set after the program.

5542H - Take a Byte

5542H passes over spaces and line feeds (5552H), then takes the next byte. 5545H takes the byte at Register Pair HL into Register A and moves Register Pair HL on; in the second pass it also writes the byte at Register Pair BC and moves Register Pair BC on. It returns the NC FLAG (and the Z FLAG in the first pass, the NZ FLAG in the second).

5542
GOSUB to 5552H to pass over the spaces and line feeds.
5545
LD A,(5692H) 3A 92 56
Load Register A with the pass from 5692H: 00H the first, 01H the second.
5548
OR A B7
OR Register A against itself: the Z FLAG for the first pass, and the NC FLAG.
5549
LD A,(HL) 7E
Load Register A with the byte at Register Pair HL.
554A
INC HL 23
INCrement Register Pair HL by 1 (the flags stay).
554B
RET Z C8
If the Z FLAG is set (the first pass), RETurn: nothing is written.
554C
LD (BC),A 02
Store Register A, the byte, at Register Pair BC, the program being written.
554D
INC BC 03
INCrement Register Pair BC by 1, the next place to write to.
554E
RET C9
RETurn to the caller with the byte in Register A.
554F
GOSUB to 5545H to take (and copy) the space or line feed.
5552
LD A,(HL) 7E
Load Register A with the byte at Register Pair HL, not yet taken.
5553
CP 20H FE 20
Compare Register A against 20H, a space, which is passed over.
5555
If the Z FLAG is set (a space), JUMP to 554FH to take it.
5557
CP 0AH FE 0A
Compare Register A against 0AH, a line feed, which is passed over.
5559
If the Z FLAG is set (a line feed), JUMP to 554FH to take it.
555B
RET C9
RETurn with Register A the next byte, not yet taken.

555CH - A Digit?

Passes over spaces and line feeds, then looks at the next byte: a digit is not taken and returns the C FLAG; any other byte is taken (5545H) and returned in Register A with the NC FLAG.

555C
GOSUB to 5552H to pass over the spaces and line feeds; Register A the next byte.
555F
CP 30H FE 30
Compare Register A against 30H, the digit 0: the C FLAG below the digits.
5561
If the C FLAG is set (below the digits), JUMP to 5545H to take the byte and return with the NC FLAG.
5563
CP 3AH FE 3A
Compare Register A against 3AH, the byte after the digit 9.
5565
RET C D8
If the C FLAG is set (a digit), RETurn with the C FLAG; the digit is not taken.
5566
JUMP to 5545H to take the byte and return with the NC FLAG.

5568H - The Messages

5568H shows Syntax error, 556BH Undefined line (entered inside the LD DE,023EH at its bytes 3EH 02H, which read as LD A,02H), each followed by in and the number of the line being read (56A2H), and sets the error flag 5693H so that the program is not renumbered. The number is written straight into the display memory at the cursor address (4020H) and not through the ROM's output, so the cursor does not move over it, and a P for the printer does not apply; a carriage return ends the message.

5568
LD A,01H 3E 01
Load Register A with 01H, the message Syntax error, and fall into the LD DE below.
556A
LD DE,023EH 11 3E 02
556BH enters inside this LD DE,023EH: its bytes 3EH 02H read as LD A,02H, the message Undefined line; from above it only loads Register Pair DE.
556D
PUSH BC C5
Save Register Pair BC to the top of the stack.
556E
PUSH HL E5
Save Register Pair HL, the text pointer, to the top of the stack.
556F
LD HL,5693H 21 93 56
Point Register Pair HL to 5693H, the error flag.
5572
LD (HL),01H 36 01
Store 01H at Register Pair HL, the error flag 5693H: a message was shown.
5574
LD HL,5668H 21 68 56
Point Register Pair HL to 5668H, the text Syntax error.
5577
DEC A 3D
DECrement Register A by 1: the Z FLAG for the message 01H.
5578
If the Z FLAG is set (Syntax error), JUMP to 557DH.
557A
LD HL,5675H 21 75 56
Point Register Pair HL to 5675H, the text Undefined line.
557D
GOSUB to 55ABH to show the text at Register Pair HL up to its 00H.
5580
LD HL,5684H 21 84 56
Point Register Pair HL to 5684H, the text " in ".
5583
GOSUB to 55ABH to show the text at Register Pair HL up to its 00H.
5586
LD BC,(4020H) ED 4B 20 40
Load Register Pair BC with the cursor address of the display from 4020H-4021H.
558A
LD DE,(56A2H) ED 5B A2 56
Load Register Pair DE with the number of the line being read from 56A2H-56A3H.
558E
GOSUB to 55EBH to write the number's digits at Register Pair BC, into the display memory at the cursor.
5591
LD A,0DH 3E 0D
Load Register A with 0DH, a carriage return, the end of the message.
5593
GOSUB to 032AH, the ROM's output of the character in Register A, to end the message.
5596
POP HL E1
Restore Register Pair HL, the text pointer, from the top of the stack.
5597
POP BC C1
Restore Register Pair BC from the top of the stack.
5598
RET C9
RETurn to the caller after the message has been shown.

5599H - The Errors

Four entries, each loading a BASIC error offset into Register B: 5599H Out of memory (0CH), 559CH Illegal function call (08H), 559FH Overflow (0AH), 55A2H Internal error (66H, a disk BASIC error). The entries after the first are each entered inside a LD DE,xx06H instruction, at its 06H xxH bytes, which read as LD B,xxH; from above they only load Register Pair DE. The links are rebuilt (5523H) before the ROM's error routine (19A2H).

5599
LD B,0CH 06 0C
Load Register B with 0CH, the offset of the error Out of memory, and fall into the LD DE below.
559B
LD DE,0806H 11 06 08
559CH enters inside this LD DE,0806H: its bytes 06H 08H read as LD B,08H, the offset Illegal function call; from above it only loads Register Pair DE.
559E
LD DE,0A06H 11 06 0A
559FH enters inside this LD DE,0A06H: its bytes 06H 0AH read as LD B,0AH, the offset Overflow; from above it only loads Register Pair DE.
55A1
LD DE,6606H 11 06 66
55A2H enters inside this LD DE,6606H: its bytes 06H 66H read as LD B,66H, the offset Internal error; from above it only loads Register Pair DE.
55A4
GOSUB to 5523H to rebuild the links of the program and set VARTAB.
55A7
LD E,B 58
Load Register E with Register B, the error offset.
55A8
JUMP to 19A2H, the ROM's error routine, with the offset in Register E.

55ABH - Show a Text

Shows the text at Register Pair HL up to its 00H through the ROM's 032AH.

55AB
LD A,(HL) 7E
Load Register A with the character at Register Pair HL.
55AC
OR A B7
OR Register A against itself: the Z FLAG for the 00H at the end.
55AD
RET Z C8
If the Z FLAG is set (the end of the text), RETurn.
55AE
GOSUB to 032AH, the ROM's output of the character in Register A.
55B1
INC HL 23
INCrement Register Pair HL by 1, to the next character.
55B2
LOOP BACK to 55ABH for the next character of the text.

55B4H - Take a Number

Takes the decimal digits at Register Pair HL (after spaces and line feeds) into Register Pair DE, copying them in the second pass. 56A4H keeps the place where the digits are written. Each digit is taken off the change in length (569AH). Returns the NC FLAG at the first byte that is not a digit, with Register Pair HL and Register Pair BC set back to that byte; returns the C FLAG, with that digit not taken, when the number is above 6552 and another digit follows.

55B4
LD DE,0000H 11 00 00
Load Register Pair DE with 0000H, the number.
55B7
LD (56A4H),BC ED 43 A4 56
Store Register Pair BC into 56A4H-56A5H, the place where the digits are written.
55BB
PUSH BC C5
Save Register Pair BC to the top of the stack.
55BC
PUSH HL E5
Save Register Pair HL to the top of the stack.
55BD
GOSUB to 555CH for the next byte: the C FLAG for a digit (not yet taken), else the byte taken.
55C0
If the NC FLAG is set (not a digit), JUMP to 55E8H to set Register Pairs HL and BC back to the byte and return.
55C2
POP AF F1
Restore Register Pair AF from the top of the stack, to drop the saved Register Pair HL.
55C3
POP AF F1
Restore Register Pair AF again, to drop the saved Register Pair BC.
55C4
LD A,19H 3E 19
Load Register A with 19H, the high byte of 1998H (6552).
55C6
CP D BA
Compare Register A against Register D: the C FLAG when Register D is above 19H.
55C7
If the NZ FLAG is set (the high bytes differ), JUMP to 55CCH.
55C9
LD A,98H 3E 98
Load Register A with 98H, the low byte of 1998H.
55CB
CP E BB
Compare Register A against Register E: the C FLAG when Register E is above 98H.
55CC
RET C D8
If the C FLAG is set (above 6552: one more digit would pass 65535), RETurn with the C FLAG.
55CD
GOSUB to 5545H to take (and copy) the digit into Register A.
55D0
PUSH HL E5
Save Register Pair HL to the top of the stack.
55D1
LD HL,(569AH) 2A 9A 56
Load Register Pair HL with the change in length from 569AH-569BH.
55D4
DEC HL 2B
DECrement Register Pair HL by 1: one old digit less.
55D5
LD (569AH),HL 22 9A 56
Store Register Pair HL into 569AH-569BH, the change in length so far.
55D8
LD H,D 62
Load Register H with Register D, the high byte of the number so far.
55D9
LD L,E 6B
Load Register L with Register E: Register Pair HL the number so far.
55DA
ADD HL,HL 29
ADD Register Pair HL to itself: twice the number.
55DB
ADD HL,HL 29
ADD Register Pair HL to itself: four times the number so far.
55DC
ADD HL,DE 19
ADD Register Pair DE to Register Pair HL: five times.
55DD
ADD HL,HL 29
ADD Register Pair HL to itself: ten times the number.
55DE
SUB 30H D6 30
SUBtract 30H from Register A: the digit's value.
55E0
LD E,A 5F
Load Register E with Register A, the value of the digit.
55E1
LD D,00H 16 00
Load Register D with 00H: Register Pair DE the value of the digit.
55E3
ADD HL,DE 19
ADD Register Pair DE (the digit) to Register Pair HL.
55E4
EX DE,HL EB
EXchange Register Pair DE with Register Pair HL: Register Pair DE the number.
55E5
POP HL E1
Restore Register Pair HL, the text pointer, from the top of the stack.
55E6
LOOP BACK to 55BBH for the next digit of the number.
55E8
POP HL E1
Restore Register Pair HL from the top of the stack: back at the byte after the digits.
55E9
POP BC C1
Restore Register Pair BC from the top of the stack: the byte will be written again.
55EA
RET C9
RETurn to the caller with the NC FLAG and the number in Register Pair DE.

55EBH - Write a Number

Writes the decimal digits of Register Pair DE at Register Pair BC, without leading zeros (at least one digit), and returns Register E the count of the digits, Register D 00H, Register Pair BC past the digits. The digits are made by the ROM's 132FH in the work area 56A6H.

55EB
PUSH BC C5
Save Register Pair BC, the place for the digits, to the top of the stack.
55EC
LD (4121H),DE ED 53 21 41
Store Register Pair DE, the number, into 4121H-4122H, the ROM's accumulator.
55F0
LD HL,56A6H 21 A6 56
Point Register Pair HL to 56A6H, the work area.
55F3
PUSH HL E5
Save Register Pair HL (56A6H) to the top of the stack.
55F4
LD BC,0000H 01 00 00
Load Register Pair BC with 0000H, the format for the ROM's 132FH.
55F7
GOSUB to 132FH, the ROM's conversion of the integer in the accumulator to five decimal digits at Register Pair HL.
55FA
POP HL E1
Restore Register Pair HL (56A6H) from the top of the stack.
55FB
POP BC C1
Restore Register Pair BC, the place for the digits, from the top of the stack.
55FC
LD D,04H 16 04
Load Register D with 04H: up to four leading zeros are passed over.
55FE
LD A,(HL) 7E
Load Register A with the digit at Register Pair HL.
55FF
CP 30H FE 30
Compare Register A against 30H, the digit 0: the Z FLAG for a leading zero.
5601
If the NZ FLAG is set (not a zero), JUMP to 5607H.
5603
INC HL 23
INCrement Register Pair HL by 1, past the leading zero.
5604
DEC D 15
DECrement Register D by 1, the leading zeros that may still be passed over.
5605
If the NZ FLAG is set (fewer than four zeros passed), LOOP BACK to 55FEH.
5607
INC D 14
INCrement Register D by 1: the count of the digits to write.
5608
LD E,D 5A
Load Register E with Register D, the count to return.
5609
LD A,(HL) 7E
Load Register A with the digit at Register Pair HL.
560A
LD (BC),A 02
Store Register A, the digit, at Register Pair BC, the place for the digits.
560B
INC BC 03
INCrement Register Pair BC by 1, to the place of the next digit.
560C
INC HL 23
INCrement Register Pair HL by 1, to the next digit of the work area.
560D
DEC D 15
DECrement Register D by 1, the count of the digits still to write.
560E
If the NZ FLAG is set (more digits), LOOP BACK to 5609H.
5610
RET C9
RETurn to the caller with Register E the count of the digits written.

5611H - The Texts and the Variables

Data. The values 5689H-5695H are in the file; 5696H on is not loaded.

5611-5636
DEFM "RENUM - BASIC program renumberer - 1.7" 52 45 4E 55 4D 20 2D 20 42 41 53 49 43 20 70 72 6F 67 72 61 6D 20 72 65 6E 75 6D 62 65 72 65 72 20 2D 20 31 2E 37
The heading that RENUM shows when it is run from the DOS command level (5209H).
5637
DEFB 0AH 0A
A line feed, which starts the second line of the heading for DSPLY@.
5638-5666
DEFM "(c) Copyright 1983, Micro-Systems Software Inc." 28 63 29 20 43 6F 70 79 72 69 67 68 74 20 31 39 38 33 2C 20 4D 69 63 72 6F 2D 53 79 73 74 65 6D 73 20 53 6F 66 74 77 61 72 65 20 49 6E 63 2E
The copyright line of the heading, the second line that DSPLY@ shows.
5667
DEFB 0DH 0D
The carriage return that ends the heading for DSPLY@.
5668-5673
DEFM "Syntax error" 53 79 6E 74 61 78 20 65 72 72 6F 72
The message of a number too large, after GOTO and the other statements (5574H).
5674
DEFB 00H 00
The 00H that ends the message Syntax error for 55ABH.
5675-5682
DEFM "Undefined line" 55 6E 64 65 66 69 6E 65 64 20 6C 69 6E 65
The message of a reference to a line that does not exist (557AH).
5683
DEFB 00H 00
The 00H that ends the message Undefined line for 55ABH.
5684-5687
DEFM " in " 20 69 6E 20
The text between a message and the line number (5580H).
5688
DEFB 00H 00
The 00H that ends the text " in ", before the line number, for 55ABH.
5689-568A
DEFW 000AH 0A 00
The new number of the first line renumbered: 10 as loaded, the nln of the statement (522EH); then the next new number (52BBH).
568B-568C
DEFW 000AH 0A 00
The increment: 10 as loaded, the inc of the statement (523CH).
568D-568E
DEFW 0000H 00 00
The first line of the range: 0 as loaded, the sln of the statement (5251H).
568F-5690
DEFW 0FFF9H F9 FF
The last line of the range: FFF9H (65529) as loaded, the eln of the statement (525DH).
5691
DEFB 00H 00
The check flag: 00H, or 21H after CMD"RENUM",! (5220H).
5692
DEFB 00H 00
The pass: 00H the first, 01H the second (54A6H).
5693
DEFB 00H 00
The error flag: 01H once a message was shown (5572H).
5694-5695
DEFW 0000H 00 00
The greatest growth of the program in the first pass, in bytes (548EH).
5696
DEFS 1
The ON flag of the statement: an ON was seen before the ERROR (5363H).
5697
DEFS 1
The flag that a 0 is not a line reference, after ON ERROR GOTO or RESUME (5360H, 53B4H).
5698-5699
DEFS 2
The last new number given (52B3H); not set before a line after the range is compared with it (52A6H).
569A-569B
DEFS 2
The change in the length of the program so far in the first pass (5316H, 55D5H, 5480H).
569C-569D
DEFS 2
The next entry of the index to match in the second pass (54D8H, 5349H).
569E-569F
DEFS 2
The place the current line is written to (531AH).
56A0-56A1
DEFS 2
The place the current line is read from (531EH).
56A2-56A3
DEFS 2
The old number of the line being read, for the messages (535BH).
56A4-56A5
DEFS 2
The place where the digits of a reference are written (55B7H).
56A6-56AA
DEFS 5
The work area of the five digits made by the ROM's 132FH (55F0H).