This program renumbers a specified range of lines in a BASIC program stored in memory, reading the line number and length bytes directly via PEEK and POKE operations. The user supplies the first and last line numbers to renumber, a new starting number, and a step value, all subject to minimum-value guards. It locates the BASIC program area using system variables at addresses 23635–23638 (PROG and VARS pointers), then walks each line’s header in memory, rewriting the two-byte line-number field in place. The program is drawn from Roger Valentine’s book “The Timex Sinclair 2068 Explored” and was entered by G.F. Chambers.
Program Analysis
Program Structure
The program is organized into two phases: user input collection (lines 1–5) and in-memory line traversal with renumbering (lines 6–10). Lines 9998–9999 handle saving and verifying the program to tape and are not part of the renumbering logic itself.
- Lines 1–5: Collect and validate four parameters — first line (
f), last line (e), new starting number (r), and step (s). Each has a guard that loops back on invalid input. - Lines 6–10: Walk the BASIC program in memory, renaming lines whose original numbers fall within [
f,e].
System Variable Usage
The program uses two pairs of system variable addresses to bracket the BASIC program area:
| Addresses | System Variable | Purpose |
|---|---|---|
| 23635–23636 | PROG | Start address of the BASIC program |
| 23637–23638 | VARS | Start of the variables area (marks end of BASIC) |
Line 6 reads these two 16-bit little-endian values into p (current line pointer) and v (end sentinel). The condition p=v on line 8 detects that the entire program has been scanned.
BASIC Line Header Format
Each BASIC line in memory has a 4-byte header: two bytes for the line number (stored big-endian, high byte first) and two bytes for the line length (stored little-endian). Line 7 reconstructs the line number as 256*PEEK p + PEEK (p+1) into n, and the line length as PEEK (p+2) + 256*PEEK (p+3) into l. Advancing the pointer by 4+l on line 10 skips over the header plus the line body to reach the next line.
Renumbering Logic
Line 7 checks whether the current line number n is below the target range; if so, line 9 is skipped via GO TO 9 and the pointer simply advances. When n is within range, line 9 overwrites the two header bytes in place using POKE: the high byte is INT(r/256) and the low byte is r - (p1*256), after which r is incremented by the step s. Line 8 terminates traversal when the line number exceeds e or the end of the BASIC area is reached, then calls LIST to display the result before stopping.
Notable Techniques
- The renumbering operates entirely in-place with no copy of the program, relying solely on PEEK and POKE — no machine code or USR calls are needed.
- Lines skipped because they fall before
fstill advance the counterrvia theGO TO 9path — however, this is actually a subtle issue: on line 9,ris incremented unconditionally whether or not the line was in range. Because theGO TO 9on line 7 always falls through to line 9, lines beforefwill also consume a renumber slot, meaning the effective new numbers assigned may not start exactly at the user-suppliedr. - The use of
LIST : STOPon line 8 provides immediate visual confirmation of the renumbered program. - Input validation uses backward
GO TOjumps rather than structured loops, a common BASIC idiom to enforce minimum values. - The line-length field being little-endian while the line-number field is big-endian accurately mirrors the Spectrum/TS2068 memory map, and the code correctly handles both byte orders.
Bugs and Anomalies
The most significant issue is that line 9 is always executed — both for lines within the renumber range and for lines before f (which jump directly to line 9 from the IF n<f THEN GO TO 9 branch). This means out-of-range lines before f cause r to be incremented, so the first line actually inside the range may not receive the exact starting number the user specified. Additionally, the program does not update any GO TO or GO SUB targets within line bodies, so any branching statements pointing into the renumbered range will be left with stale line numbers.
Content
Source Code
1 REM RENUMBER \''\''\''\''\''\''\''\'' from the book entitled "THE TIMEX SINCLAIR 2068" by ROGER VALINTINE entered by G.F. Chambers
2 INPUT "First line to renumber ";f: IF f<10 THEN GO TO 1
3 INPUT "Last line to renumber ";e
4 INPUT ("New number for ";f;" ");r: IF r<10 THEN GO TO 3
5 INPUT "Step ";s: IF s<1 THEN GO TO 4
6 LET p=PEEK 23635+256*PEEK 23636: LET v=PEEK 23637+256*PEEK 23638
7 LET n=256*PEEK p+PEEK (p+1): LET l=PEEK (p+2)+256*PEEK (p+3): IF n<f THEN GO TO 9
8 IF n>e OR p=v THEN LIST : STOP
9 LET p1=INT (r/256): POKE p,p1: POKE p+1,r-(p1*256): LET r=r+s
10 LET p=4+p+l: GO TO 6
9998 SAVE "RENUMBER" LINE 1
9999 VERIFY ""
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.
