Utility programs for renumbering the lines of BASIC programs facilitate merging with other programs, spreading out line numbers to make room for additional lines, and neatening up finished programs. Changing line numbers is easy, but a useful renumbering utility must also fix the destination line numbers in GOTOs and GOSUBs, a more difficult task. This program does the job, although slowly since it is written almost entirely in BASIC.
If you have a program merging utility, RENUMBER can be appended to, and used to renumber, any BASIC program of less than about 12,000 bytes (provided its line numbers initially extend no farther than 9722). Even without a merge utility, RENUMBER can still be useful. LOAD it first, then key in your main program as you develop it.
Note the partial Syntactic Sums given at several points in the listing. When typing in RENUMBER, get a Syntactic Sum at each of these checkpoints and correct any mistakes before proceeding. This will reduce the number of lines you need to search to find your error. Since this is a self-starting program, the final Syntactic Sum reflects the program after it has been SAVEd and then LOADed from cassette.
After typing in RENUMBER, SAVE it to cassette with the command GOTO 9976. Whenever LOADed it will self-start and execute Lines 9978 to 9994, storing two machine language subroutines into high memory. You must set RAMTOP to address 32512 or lower, and NEW to reinitialize the machine stack prior to loading RENUMBER. The machine code loads at addresses 32512 to 32542. It is relocatable, but this requires appropriate changes in Lines 9890, 9892, 9904, 9982, 9990, and 9996.
RENUMBER ignores any GOTO or GOSUB whose destination line number does not begin with a digit. With this exception, RENUMBER correctly handles the renumbering of all (or any part of) a BASIC program.
You must specify what part of the target program is to be renumbered, the new starting line number, and the desired increment between line numbers. If only a portion of the target program is renumbered the user must be very careful in selecting the new starting number and the increment. The line numbers that result from renumbering must be consistent with (i.e., in correct numerical order) the rest of the program, otherwise the resulting program may crash. Note that entering 0 in response to the first prompt renumbers the entire program.
RENUMBER starts by searching the entire program, line by line, (main search routine starts at line 9826) looking for IF, GOTO, or GOSUB statements. When an IF statement is found, the rest of that statement is scanned to see whether it contains a GOTO or GOSUB. If so, the program jumps to the GOTO/GOSUB processing routine at Line 9848. Otherwise it jumps back to the main search routine. Line 9842 avoids misinterpreting bytes within floating point numbers as GOTO or GOSUB tokens, ignoring 5 bytes following code 126.
The core of the program begins at line 9848, the GOTO/GOSUB processing routine. After the token for GOTO or GOSUB Sinclair BASIC puts the codes for the decimal digits of the destination line number, then a “number” token (code 126), then five bytes holding the number in floating-point binary form, then a newline character (code 118). Lines 9848 to 9860 put the characters following GOTO or GOSUB into D$ if it is a number, but if the first destination character is non-numeric it jumps back to the main search routine.
Lines 9862 to 9874 determine whether or not the number in D$ is a bona-fide line number within the part of the program being renumbered. If not, RENUMBER jumps back to the main search routine, without making any changes. Otherwise, it determines what position that line occupies in the section of the program being renumbered and puts that position number into variable DC. The new line number will be the position number times the step size plus the new starting line number.
Both the old line number and the new line number may have anywhere from one to four digits (1 to 9722). If the new number is shorter than the old, lines 9886 and 9888 put enough leading zeroes in front of the new number to make it the same length as the old. But when the new number is longer than the old, RENUMBER makes room for it in the program line by moving the rest of the target program (including all of RENUMBER itself) upward in memory by one, two, or three bytes. There is a suitable “make room” routine already available in the system ROM, at address 2459. It moves the memory block starting at the address pointed to by register pair HL and ending at the end of the program, one byte up, adjusting the system variables accordingly.
Lines 9890 and 9892 POKE into memory locations 32512 and 32513 the address to load into HL. The FOR…NEXT loop beginning at line 9894 makes one or more USR calls to the machine code routine at address 32514, which in turn loads the stored address into register pair HL and calls the ROM routine to “make room”. This loop also adjusts the length bytes of the newly expanded program line.
The “make room” routine moves the bytes of the BASIC line that called it, resulting in the return address being off by one byte and, normally, a crash. BASIC can easily be tricked into returning to the correct address: Line 9902 gets the address of Line 9904 from system variable NXTLIN and POKEs the length of line 9904 so it is one byte too high (15 instead of 14). After the “make room” routine executes, it returns to the location 15 bytes after line 9904. Line 9904 is 14 bytes long, plus the one byte it has been moved by the “make room” routine, so the USR routine returns to the correct address. Immediately after each USR call the phony length of 15 is corrected to the proper figure of 14 by Line 9906. This line gets the address of line 9908 from system variable NXTLIN and looks backward 77 bytes (the total length of Line 9906 plus part of Line 9904) to the address of the low length byte of line 9904.
Lines 9910 to 9914 POKE the codes for the decimal digits of the new line number into the program line. Then Line 9918 gets the address of program variable ND from system variable VARS. Variable ND, originally defined in line 9738, contains the value of the new line number in floating-point form, so Lines 9920 to 9924 simply copy those five bytes into the program line, over-writing the original number. This process repeats for each line in the program containing GOTO or GOSUB. Finally, lines 9940 to 9950 renumber the lines within the range initially specified.
You can erase RENUMBER completely with these two commands:
GOTO 9996 ENTER
9724 ENTER
Line 9996 invokes the machine code routine at address 32521, which calculates the total length of RENUMBER and POKEs that number minus 4 into the length bytes of line 9724. Thus when you delete line 9724 the entire RENUMBER program goes with it, leaving your finished main program intact.
9724 STOP
9725 REM ****RENUMBER****
9726 REM BY C. C. STALDER
9728 REM >>NOTE<< THIS ENTIRE PROGRAM LINES 9724 TO 9999 CAN BE DELETED BY TWO COMMANDS: GOTO 9996 ENTER
9724 ENTER
9730 REM
9732 REM RESERVE SPACE AT "VARS"FOR CONVERTING EACH DESTINATION LINE NUMBER TO FLOATING POINT
9734 CLS
9736 CLEAR
9738 LET ND=0
9740 PRINT "LOWEST LINE TO RENUMBER? "
9742 PRINT "TO RENUM ENTIRE PROGRAM, ENTER 0"
9744 INPUT LL
9746 IF LL<0 OR LL<>INT LL OR LL>9722 THEN GOTO 9952
9748 IF LL<>0 THEN PRINT AT 0,26;LL;AT 3,0;
9750 IF LL<>0 THEN GOTO 9762
9752 PRINT AT 0,26;"ALL";AT 3,0;
9754 LET LL=256*PEEK 16509+PEEK 16510
9756 LET HL=9722
9758 GOTO 9770
9762 PRINT "HIGHEST LINE TO RENUMBER? ";
9764 INPUT HL
9766 IF HL<=LL OR HL<>INT HL OR HL>9722 THEN GOTO 9952
9768 PRINT HL
9770 PRINT ,,"NEW NUMBER FOR LOW LINE? ";
9772 INPUT NL
9774 IF NL<1 OR NL<>INT NL OR NL>9722 THEN GOTO 9952
9776 PRINT NL
9778 PRINT ,,"STEP SIZE DESIRED? ";
9780 INPUT SZ
9782 IF SZ<1 OR SZ<>INT SZ THEN GOTO 9952
9784 PRINT SZ
9786 PRINT ,,"CHOOSE:..(R)UN..(M)ODIFY..(Q)UIT"
9788 PAUSE 4E4
9790 LET K$=INKEY$
9792 IF K$="R" THEN GOTO 9812
9794 IF K$="M" THEN GOTO 9734
9796 IF K$="Q" THEN GOTO 9992
9798 GOTO 9790 SYNTACTIC SUM SO FAR: 60304
9812 LET SA=16509
9814 IF 256*PEEK SA+PEEK (SA+1)=LL THEN GOTO 9826
9816 IF 256*PEEK SA+PEEK (SA+1)>LL THEN GOTO 9952
9818 LET SA=SA+4+PEEK (SA+2)+256*PEEK (SA+3)
9820 GOTO 9814
9826 LET OP=16509
9828 IF 256*PEEK OP+PEEK (OP+1)>
9722 THEN GOTO 9940
9830 LET LA=OP+4
9832 IF PEEK LA=250 THEN GOTO 9840
9834 IF PEEK LA=236 OR PEEK LA=2
37 THEN GOTO 9848
9836 LET OP=OP+4+PEEK (OP+2)+256*PEEK (OP+3)
9838 GOTO 9828
9840 LET LA=LA+1
9842 IF PEEK LA=126 THEN LET LA=LA+6
9844 IF PEEK LA=118 THEN GOTO 9836
9846 IF PEEK LA<>236 AND PEEK LA<>237 THEN GOTO 9840 SYNTACTIC SUM SO FAR: 31362
9848 LET LA=LA+1
9850 LET D$=""
9852 FOR N=LA TO LA+4
9854 IF PEEK N=126 THEN GOTO 9862
9856 IF PEEK N<28 OR PEEK N>37 THEN GOTO 9836
9858 LET D$=D$+CHR$ PEEK N
9860 NEXT N
9862 IF VAL D$<LL OR VAL D$>HL THEN GOTO 9836
9864 LET DC=0
9866 LET DP=SA
9868 IF VAL D$<=256*PEEK DP+PEEK (DP+1) THEN GOTO 9880
9870 LET DP=DP+4+PEEK (DP+2)+256*PEEK (DP+3)
9872 LET DC=DC+1
9874 GOTO 9868
9880 LET ND=DC*SZ+NL
9882 LET X$=STR$ ND
9884 IF LEN X$<LEN D$ THEN LET X$="0"+X$
9886 IF LEN X$<LEN D$ THEN GOTO 9884
9888 IF LEN X$=LEN D$ THEN GOTO 9910
9890 POKE 32513,INT (LA/256)
9892 POKE 32512,LA-256*PEEK 32513 SYNTACTIC SUM SO FAR: 921
9894 FOR N=1 TO LEN X$-LEN D$
9896 LET TL=PEEK (OP+2)+256*PEEK (OP+3)+1
9898 POKE (OP+3),INT (TL/256)
9900 POKE (OP+2),TL-256*PEEK (OP+3)
9902 POKE (PEEK 16425+256*PEEK 16426+2),15
9904 RAND USR 32514
9906 POKE (PEEK 16425+256*PEEK 16426-77),14
9908 NEXT N
9910 FOR N=0 TO LEN X$-1
9912 POKE (LA+N),CODE X$(N+1)
9914 NEXT N
9916 LET LA=LA+LEN X$+1
9918 LET VP=PEEK 16400+256*PEEK 16401+2
9920 FOR N=0 TO 4
9922 POKE (LA+N),PEEK (VP+N)
9924 NEXT N
9926 GOTO 9836
9940 IF 256*PEEK SA+PEEK (SA+1)>HL THEN GOTO 9992
9942 POKE SA,INT (NL/256)
9944 POKE (SA+1),NL-256*PEEK SA
9946 LET NL=NL+SZ
9948 LET SA=SA+4+PEEK (SA+2)+256*PEEK (SA+3)
9950 GOTO 9940 SYNTACTIC SUM SO FAR: 42169
9952 CLS
9954 PRINT "INVALID NUMBER. PRESS ENTER AND START OVER."
9956 PAUSE 4E4
9958 GOTO 9734
9976 SAVE "RENUMBER"
9978 FAST
9980 LET Z$="042000127205155009201033252037205216009035035035235042012064167237082235027114043115201"
9982 LET PA=32514
9984 FOR N=1 TO 87 STEP 3
9986 POKE PA,VAL Z$(N TO N+2)
9988 LET PA=PA+1
9990 NEXT N
9992 CLS
9993 LIST
9994 STOP
9996 RAND USR 32521
9999 GOTO 9992 SYNTACTIC SUM BEFORE SAVE: 62187
ADDR 7F00 00005 00006 DFILE 400C 00003 00016 ERASE 7F09 00010 MAKER 7F02 00006
00001 ;RENUMBER MACHINE CODE
00002
400C 00003 DFILE EQU 16396
7F00 00004 ORG 32512
7F00 0000 00005 ADDR DEFW 0 ;ADDRESS POKED IN BASIC
7F02 2A007F 00006 MAKER LD HL,(ADDR) ;GET ADDRESS IN HL
7F05 CD9B09 00007 CALL 2459 ;CALL "MAKE ROOM"
7F08 C9 00008 RET ;BACK TO BASIC
00009
7F09 21FC25 00010 ERASE LD HL,9724 ;FIRST LINE # IN RENUM
7F0C CDD809 00011 CALL 2520 ;LOOK UP ADDRESS
7F0F 23 00012 INC HL ;POINT HL TO HIGH
7F10 23 00013 INC HL ;TEXT LENGTH BYTE
7F11 23 00014 INC HL ;OF LINE 9724
7F12 EB 00015 EX DE,HL ;SAVE HL IN DE
7F13 2A0C40 00016 LD HL,(DFILE) ;HL=ADDRESS OF DFILE
7F16 A7 00017 AND A ;CLEAR CARRY FLAG
7F17 ED52 00018 SBC HL,DE ;HL=# PROG BYTES MINUS 3
7F19 EB 00019 EX DE,HL ;RECOVER LEN PTR IN HL
7F1A 1B 00020 DEC DE ;DE=# PROG BYTES MINUS 4
7F1B 72 00021 LD (HL),D ;CHANGE HI BYTE LEN 9724
7F1C 2B 00022 DEC HL
7F1D 73 00023 LD (HL),E ;CHANGE LOW BYTE LEN 9724
7F1E C9 00024 RET ;BACK TO BASIC
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.