Line Renumbering

See all articles from CATS v1 n10
Author(s): Mark Fisher
Publication: CATS Newsletter
Pub Details: CATS v1 n10 (Volume 1, Issue 10)
Date: January 1984
Pages: 6

This month, I’ll apply the knowledge of a program’s structure [Nov. newsletter] to a task that is often desired when writing your own programs.

One of the hallmarks of BASIC (as opposed to Pascal or COBOL) is its use of line numbers. The computer uses them to find the correct spot in the program to jump to. Program lines are maintained in line number order by the computer. When you finish writing a line, the computer adds the binary equivalent of each constant, determines the correct position in the program file, makes the right amount of extra room, and moves it from the workspace to its new home. Line numbers need not be consecutive (1,2,3,4); but they must be in order (1,3,20,9000). Usually line numbers are increased by 10 for each line. This allows easy insertion of extra lines, simply by assigning the new line a number between the two existing lines.

What if you numbered your program consecutively, as Don Mayes did in Stargazing With Your T/S? [Dec. newsletter] The program will run fine, but what if you wanted to add a line between 32 and 33? No, the Timex won’t accept 32.5. You would have to start at line 93; EDIT it, change its number to something larger, re-enter it, and repeat the process all the way back to line 33! All GOTOs and GOSUBs would also have to be changed. This same situation can occur during development of a new program, even if you originally started with lines on even tens – it’s easy to have to add more than nine lines between any two, as you are working out the bugs.

CLARIFYING THE TASK

There is a solution – a RENUMBER utility. You can’t just POKE new numbers in at random, though. You need to take advantage of the regularities in the program file to find the right places to POKE the new numbers. What do we know about line structure?

1) The program file always starts at 16509.

2) The first two bytes of each line hold the line number – that’s where we’ll have to do our POKEing.

3) The next two bytes hold the line length (not including the first 4 bytes) – we’ll use these to get to the next line.

ON TO PSEUDO-CODE

Here’s the plan. X is set at 16509. The contents of address X and X+1 are combined to check the current line number – with a STOP if the first line of the RENUMBER routine is reached (you can’t use this routine to renumber itself!). The new number (N), is POKED into X and X+1, then incremented as desired. X is moved to the next line – line length +4 (for the header). And back around. You’ll still have to edit the lines that contain GOTOs and GOSUBs, but there aren’t as many of them.

ON TO BASIC

9990 LET X=16509
9991 LET N=10
9992 IF PEEK X*256+PEEK (X+1)>=9000 THEN STOP
9993 POKE X,INT (N/256)
9994 POKE X+1,N-256*INT (N/256)
9995 LET N=N+10
9996 LET X=X+4+PEEK (X+2)+256*PEE (X+3)
9997 GOTO 9992

AND BEYOND

It’s possible to include the GOTOs and GOSUBs as well, but it requires a lot of programming. Here’s what has to be done. 1) The program file must be scanned, and a table built up, of the locations of GOTOs and GOSUBs, along with the line numbers they refer to.

2) A second pass through the program is made, the old numbers are checked against the numbers in the table: if they match, the new number is entered in its place: and the line numbers are changed.

3) The program file is scanned a third time, and each GOTO and GOSUB is given its new number reference.

THE PERILS OF PROGRAM DEVELOPMENT

I have finished the first section of this expanded program. In that process, I thought of some potential bugs. To wit; computed GOTOs (GOTO 10*X) would not be correctly modified. Computed GOTOs are not used very often, and I wasn’t worried about leaving that out. The binary representation would also hve to be changed; again, not a real problem, just another subroutine. A bigger problem is that the size of the program varies with the number of digits in the line number. If the original read GOTO 20, and the revised version had to read GOTO 300, the program file would have to be lengthened by one byte. I can’t think of any way around that one in BASIC; and that’s the end of the project.

Mark Fisher


As Creative Computing says, “the reason we don’t have an errata column is because we don’t make mistakes.” Boggler, in the Dec. issue, contains some graphics DATA statements that were very hard to interperet. They weren’t wrong, but they could have been clearer.

To get line 920, TYpe: (underline = GRAPHICS SHIFT,b=space)

9020 LET Q$="bbbbbbbbbbbbbbbbbbbbbbb,bbbbbbbbBOGGLERbbbbbbb,b,bbbbbbbbbbbbbbbbbbbbbbb,bbbbb66bbb66bbb66bbbbb,b,bbb8bTYb8bTYb8bTYb5bbb,bbb8bYTb8bYTb8bYTb5bbb,"
As printed (typewriter, 32 characters per row; ^ marks the characters printed underlined = GRAPHICS SHIFT; b = space):
9020 LET Q$="bbbbbbbbbbbbbbbbbbb
             ^^^^^^^^^^^^^^^^^^^
bbbb,bbbbbbbbBOGGLERbbbbbbb,b,bb
^^^^ ^^^^^^^^       ^^^^^^^ ^ ^^
bbbbbbbbbbbbbbbbbbbbb,bbbbb66bbb
   ^^   ^^   ^^   ^^  ^^   ^^
66bbb66bbbbb,b,bbb8bTYb8bTYb8bTY
^^   ^^   ^^ ^ ^^^^^^^^^^^^^^^^^
b5bbb,bbb8bYTb8bYTb8bYTb5bbb,"
^^^^^ ^^^^^^^^^^^^^^^^^^^^^^

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