The Computer Tutor: Line Renumbering

The Computer Tutor heading art with LINE RENUMBERING panel, graduate tutor pointing at a computer
The Computer Tutor heading art with LINE RENUMBERING panel, graduate tutor pointing at a computer

Good morning class, I’m the substitute teacher and I’ll be helping to fill in for the Old Professor this session. He has taken ill and couldn’t make it.

No, it isn’t serious – just a bug of some kind. I’m sure he’ll be back soon. We are, however, fortunate in that one of the more gifted students will be teaching the lesson today. I spoke to him before class and the impression he gave me is that the Professor thinks quite highly of him and consults him frequently on various programming techniques. Funny, though, I talk with the Professor quite a bit and he usually informs me of his best pupils. No matter, probably an oversight on his part.

Anyway, if the gentleman in the back of the class would care to come up here now, I’m sure we’re all waiting to hear what he has to say. I’d like to introduce at this time, Mr. Ron Lamon!

Thank you for the kind words, Sir. Most of you already know me anyway. The subject of today’s lesson will be on line-renumbering.

Have any of you ever written a program in which you had to stick in some additional lines and had no place to put them? A program such as the following demonstrates this:

20 PRINT "WHAT IS YOUR NAME?"
21 PRINT "HELLO, ";A$;"."

We have to put in an INPUT statement between lines 20 and 21 in order to get this program to work. We can, of course, edit line 20 and change the line number to 19 or we can edit line 21 and change the line number to 22. In either case we will then have 2 lines with the same information. For our example, let’s edit line 21 to 22. Enter LIST 21, press SHIFT and the <1> key and the line will be ready for editing.

Delete the <1> in <21> and replace it with a <2> and press enter. Your program should now look like this:

20 PRINT "WHAT IS YOUR NAME?"
21 PRINT "HELLO, ";A$;"."
22 PRINT "HELLO, ";A$;"."

Now enter the line:

21 INPUT A$

The program listing should now look like this:

20 PRINT "WHAT IS YOUR NAME?"
21 INPUT A$
22 PRINT "HELLO, ";A$;"."

This is the technique most of us are familiar with. However, when you have a long program that you have packed a lot of extra lines into, problems can arise. The following versions will allow you to renumber the first 4 lines of the program:

VERSION #A

1 GOTO 2
2 GOTO 3
3 GOTO 4
4 GOTO 9979
9979 PRINT "START? ";
9980 INPUT A
9981 PRINT A
9982 PRINT "STEP? ";
9983 INPUT B
9984 PRINT B
9985 FAST
9986 LET N=16508
9987 IF PEEK (N+1)=38 AND PEEK
(N+2)=251 THEN GOTO 9997
9988 LET Y=INT (A/256)
9989 LET Z=A-(Y*256)
9990 POKE (N+1),Y
9991 POKE (N+2),Z
9992 LET N=N+1
9993 IF PEEK N=118 THEN GOTO 9995
9994 GOTO 9992
9995 LET A=A+B
9996 GOTO 9987
9997 CLS
9998 SLOW
9999 LIST

This routine (in this version) takes 330 bytes of memory space, not counting the first 4 lines which are the program we want to be renumbered.

Enter and RUN this program. START? is asking you for the line number you want the program to start at. STEP? is asking you for the increment number between lines. You will notice that the first 4 lines are incremented by 1 and start with line number 1. Enter 10 for the first prompt and 10 for the second prompt.

In a second or two, you will see the new listing of the program with the first line being 10 and the first 4 lines being incremented by 10.

But what has happened to the GOTO commands in the first 4 lines? The line numbers that follow them are the same as they were. What this routine does is to search the computer’s memory locations for line numbers and replace them with the numbers we want and ignore the actual routine which starts at 9979. The user will have to edit any lines that use commands to branch to other line numbers with the new line numbers. The commands that branch are GOTO, GOSUB, LIST and RUN.

To renumber the first 4 lines again, you can’t just type in RUN. The first line 10, will tell the program to GOTO 1, which then looks for line 1. It doesn’t find it so it looks for the next line number, which is 10 and the cycle is repeated. This is a continuous loop. To execute the routine, it is better to enter GOTO 9979 or RUN 9979.

The reason that the routine doesn’t renumber itself is because line 9987 allows the routine to be ignored. The mathematical formula involved in that line, calculates 38 * 256 + 251 which equals 9979. This is the first line of the renumbering routine. When the renumbering routine encounters the line 9979, the program is instructed to break out to line 9997, which ends the program.

Here is another routine that trims off some of the “fat” to save key-entry time and memory space:

VERSION B – 263 bytes

1 REM A
2 REM B
3 REM C
4 REM D
9986 INPUT A
9987 INPUT B
9988 FAST
9989 LET N=16508
9990 IF PEEK (N+1)=39 AND PEEK
(N+2)=2 THEN STOP
9991 LET Y=INT (A/256)
9992 LET Z=A-(Y*256)
9993 POKE (N+1),Y
9994 POKE (N+2),Z
9995 LET N=N+1
9996 IF PEEK N=118 THEN GOTO 9998
9997 GOTO 9995
9998 LET A=A+B
9999 GOTO 9990

The first prompt is the starting number and the second is the increment number.

Still another:

VERSION C – 248 bytes

1 REM A
2 REM B
3 REM C
4 REM D
9986 INPUT A
9987 INPUT B
9988 FAST
9989 LET N=16508
9990 IF PEEK (N+VAL "1")=32 AND
PEEK (N+VAL "2")=2 THEN STOP
9991 LET Y=INT (A/256)
9992 LET Z=A-(Y*256)
9993 POKE (N+VAL "1"),Y
9994 POKE (N+VAL "2"),Z
9995 LET N=N+1
9996 IF PEEK N=118 THEN GOTO 9998
9997 GOTO 9995
9998 LET A=A+B
9999 GOTO 9990

This version may take a few more keystrokes because of the VAL expression but it saves a few bytes of memory because number values require more memory space.

The Old Professor entrusted with me this final version:

VERSION D

1 REM A
2 REM B
3 REM C
4 REM D
9986 STOP
9987 PRINT "WHAT IS THE STARTING
LINE NUMBERYOU WANT?"
9988 INPUT S
9989 PRINT ,,"WHAT IS THE INCREM
ENT NUMBER?"
9990 INPUT I
9991 LET A=16509
9992 LET B=S
9993 POKE A,INT (B/256)
9994 POKE A+1,B-256*INT (B/256)
9995 LET B=B+I
9996 LET A=A+1
9997 IF 256*PEEK A+PEEK (A+1)=99
86 THEN STOP
9998 IF PEEK (A-1)=118 THEN GOTO
9993
9999 GOTO 9996

This version, of course, isn’t as good as mine but it does the job. It is executed by entering GOTO 9987.

I would suggest that anyone writing a program might want to save one of these versions on tape and recall it when needed and write the program with the routine as a part. Make sure that if the program ends with the last program line where it would continue into the renumbering routine, that a STOP line is inserted properly.

If you are in the miidle of writing a program and have no more space to insert lines, then you can just type in one of these routines and execute it as outlined.

Well, since there is another half hour to the class, I suggest all of you practice what I’ve taught while I go back to my seat and sleep.

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