Machine code routine to renumber programs. Uses lines 0 and 9999.
MC-RENUM is a 54-byte BASIC program whose working part is 30 bytes of Z80 code hidden in the REM of line 0; the only other line is 9999, which saves it. Called with RANDOMIZE USR 26715 (the first byte after the REM token, since a TS2068 program starts at 26710), it walks through the program in memory and gives every line between line 0 and line 9999 a new number, starting at 10 and going up in tens. Only the line numbers change: GO TO, GO SUB and RESTORE targets are left as they were and have to be fixed by hand. It is the eighth program on side A of the Miscellaneous Programs tape, next to the longer BASIC Renumber.
Program Analysis
How the routine works
Disassembled from the bytes in the REM (addresses for a stock TS2068):
LD BC,10: the first new line number, held in the two bytes at 26716–26717.LD HL,26719, then a loop ofINC HL/CP (HL)/JR NZthat scans forward for the end-of-line byte (13), which finds the end of line 0.INC HLmoves to the high byte of the next line number;LD A,38/CP (HL)/RET Mreturns to BASIC once that byte is above 38, i.e. at line 9984 or higher, which in practice means the routine’s own line 9999.LD (HL),B/INC HL/LD (HL),Cwrites the new number (high byte first, as Sinclair BASIC stores it), then 10 is added toBCand the code jumps back to scan for the end of that line.
Notable Techniques
- The code never contains the byte 13, which would end the REM line early: it loads
Awith 12 and increments it to get the end-of-line marker. - Because the routine sits in line 0 and saves itself from line 9999, it can be merged with another program and stays out of the way of the lines being renumbered.
- It finds each line by scanning for the byte 13 rather than by using the line-length bytes.
Potential Issues
- Line references inside the program are not updated, so any program with
GO TOorGO SUBwill need its targets corrected after renumbering. - Scanning for the byte 13 instead of using each line’s length bytes appears to go wrong if a line holds a 13 anywhere else, for example in a number’s five-byte form, a REM full of machine code or a colour code inside a string; the routine would then write a line number into the middle of that line.
- The start number can be changed by POKEing 26716–26717, but the step of 10 is fixed in the code, and with more than about 970 lines the new numbers would pass the 9984 limit and the routine would stop early.
- The routine is position-dependent and only works while line 0 is the first line in the program, at 26710.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

