Line Renumbering

Author(s): Larry Lockwood; Mike Mullen
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v3 n6 (Volume 3, Issue 6)
Date: June 1982
Pages: 4-5

Here are two approaches to line renumbering, one in BASIC and one in machine code. Neither of them renumbers GOTOs or GOSUBs. Whenever you write a program, load your choice first, then execute it after entering your program to clean up line numbers.

BASIC

This program works on an 8K ZX80 or a ZX81 with 16K RAM. Load the renumbering program (RENO), then write your program. Execute RENO by entering RUN 9965. After renumbering, I save the program with RENO attached in case I want to modify and renumber the program later. RENO begins with a STOP statement to separate it from the program being written, and resides at the end of allowable line numbers. To try it out, enter a few statements with random line numbers and then RUN 9965 to renumber them as you wish.

Larry Lockwood, Yorba Linda, CA

9964 STOP
9965 PRINT AT 8,1;"START RENUMBERING AT?"
9966 INPUT START
9967 PRINT AT 8,21;" ";START;"."
9968 LET ADDR=16509
9969 LET LINE=256*PEEK (ADDR)+PEEK (ADDR+1)
9970 IF LINE=START THEN GOTO 9978
9971 IF LINE=9964 THEN GOTO 9975
9972 LET LENGTH=PEEK (ADDR+2)+256*PEEK (ADDR+3)
9973 LET ADDR=ADDR+4+LENGTH
9974 GOTO 9969
9975 PRINT AT 8,1;" STARTING ADDRESS NOT FOUND."
9976 PAUSE 200
9977 GOTO 9965
9978 PRINT AT 10,1;"RENUMBER BEGINNING WITH?"
9979 INPUT NEW
9980 PRINT AT 10,24;" ";NEW;"."
9981 PRINT AT 12,1;"INCREMENT LINES BY?"
9982 INPUT INC
9983 PRINT AT 12,19;" ";INC;"."
9984 PAUSE 150
9985 CLS
9986 IF PEEK (ADDR)=38 AND PEEK (ADDR+1)=236 THEN GOTO 9996
9987 IF NEW>=9964 THEN GOTO 9998
9988 LET MSB=INT (NEW/256)
9989 LET LSB=NEW-256*MSB
9990 POKE ADDR,MSB
9991 POKE ADDR+1,LSB
9992 LET LENGTH=PEEK (ADDR+2)+256*PEEK (ADDR+3)
9993 LET ADDR=ADDR+4+LENGTH
9994 LET NEW=NEW+INC
9995 GOTO 9986
9996 PRINT AT 10,5;"RENUMBERING COMPLETED"
9997 STOP
9998 PRINT AT 10,5;"CANNOT COMPLETE."
9999 PRINT AT 12,6;"LINE NO. >9964."
SYNTACTIC SUM: 60543, 8K

Machine Code

This program renumbers program lines at any time. It is stored in machine code and safely tucked away behind a new RAMTOP of 32000. It sets the first line to 10 and increments the rest by 10s. No STOP statement is needed to signal the end.

Enter the program as shown, then RUN. Enter the numbers in the table (33,125, etc.). This is the decimal listing for the machine language the Z80 CPU understands.

Now delete lines 20-70 and 150. Note the REM 1234… line is different.

Save the program by entering GOTO 200. It runs automatically when LOADed. You will see a flicker and the K cursor on the screen. To test it, type in a short program with random line numbers. To renumber, enter RAND USR 32000.

To start with a line number other than 10, POKE 16518,X (where X=line number you want). If you want to increment the lines by a number other than 10, POKE 16534,Y (where Y=number to increment by).

Mike V. Mullen, Lawrence, KS

  10 REM 123456789012345678901234567
20 FOR X=16514 TO 16540
30 SCROLL
40 INPUT I
50 POKE X,I
60 PRINT X,PEEK X
70 NEXT X
80 LET L=16514
90 FOR X=32000 TO 32026
100 POKE X,PEEK L
110 LET L=L+1
120 NEXT X
130 POKE 16388,0
140 POKE 16389,125
150 STOP
160 NEW
200 SAVE "RENUM"
210 RUN

Decimal Listing

 33  125   64   17   10    0  114
 35  115   35   78   35   70   35
  9  126  214  118  200    1   10
  0  235    9  235   24  235

Assembly Listing

Dec  Hex  OpCode      Comment
 33  21   LD HL,NN    loads 16509
125  7D                 into HL
 64  40
 17  11   LD DE,NN    loads 10 into
                      DE as 1st line
                      number
 10  0A
  0  00
114  72   LD (HL),D   insert new line
                      number
 35  23   INC HL
115  73   LD (HL),E
 35  23   INC HL
 78  4E   LD C,(HL)   loads length of
                      line text into
 35  23   INC HL      BC (B is high
 70  46   LD B,(HL)   byte)
 35  23   INC HL
  9  09   ADD HL,BC   sets HL pointer
126  7E   LD A,(HL)   to next line of
                      text
214  D6   SUB A,N     check for code
                      118 (indicates
                      end of program)
118  76
200  C8   RET Z       return to BASIC
                      if at end
  1  01   LD BC,NN    increment line
                      number by 10
 10  0A
  0  00
235  EB   EX DE,HL
  9  09   ADD HL,BC
235  EB   EX DE,HL
 24  18   JR, DIS     jump to con-
235  EB   EX DE,HL    tinue renum-
                      bering (is
                      shown in 2's
                      complement)

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.