Classic Computer Magazine Archive COMPUTE! ISSUE 19 / DECEMBER 1981 / PAGE 166

Looney Line Numbers

Jim Butterfield
Toronto, Canada

It should never happen. You have a program that you've been working on for hours (days? weeks?) and then suddenly a line number goes wrong. In between lines 6340 and 6360 the line number that should be 6350 has suddenly changed to 2254. Not only is that wrong—the GOTO's won't work right—but you can't get rid of it! The line seems stuck in your program forever. How does it happen? More to the point, how do you get rid of it without completely reentering the program?

How It Happens

It won't happen under normal circumstances. BASIC guards carefully against this kind of error.

An unwise POKE instruction or a SYS to a machine language program that's not completely debugged can get you into all sorts of trouble. If you're lucky, all you'll get is a looney line number.

Sometimes a bad LOAD will do the trick. In theory, the computer should guard against load errors; but it doesn't always tell you the whole story. If you're loading tape on a CBM/PET, always ask for the Status value (type PRINT ST): if the value is zero, the load is reliable: otherwise, you're taking your chances.

Bad RAM (Random Access Memory) can plague you with faults. It's not always obvious. Memory can sometimes fail erratically: perhaps the power supply voltage drops for a moment, and a bit disappears; or the malfunction only starts after the computer's innards get hot. It you're plagued with this type of problem, have your machine checked out.

All of the above may cause goofy line numbers; but they also may randomly cause other errors. Some are fatal, and some cause your program to look weird. Try to pin down the cause; it's worth the effort.

Fixing Numbers That Are Too High

There are two cases: high line numbers (out of proper order) and very high line numbers.

If an out-of-sequence line number is too high, but less than 64000, the trick is easy: delete the bad line and reenter it with die proper line number.

If the line number is 64000 or more, we must go to the next section and run the program there. You're not allowed to enter a line number of 64000 or more, even to delete the line concerned. Try typing 64000 followed by RETURN; you'll get a ?SYNTAX ERROR.

Fixing Low And Super-high Numbers

Type in the following lines at the front of your program. If your program happens to have lines numbered in the range from 0 to 8, take them out and put them back later.

1 A = 1025: V = 256: X = -1
2 B = A : A = PEEK(B) + PEEK(B + 1) * V
4 Z = PEEK (B + 2): Y = Z + PEEK (B + 3) * V
5 PRINT CHR$ (145); Y; : IF Y > X AND Z<250 GOTO 8
6 Y = X + 1 : Y % = Y/V: PRINT "TO" ;Y
7 POKE B + 2, Y-Y% *V : POKE B + 3, Y%
8 X = Y: GOTO 2

The above coding is for the PET/CBM; you can adapt it to other computers by changing the value 1025 in line 1 to the Start-of-BASIC address in your machine. The CHR$(145) can be changed to match your machine's Cursor-Up character.

Meaning of the variables: B is the address of the current line of BASIC being examined; A is the address of the next line. X is the previous line number and Y is the new line number. Z is the "high byte" of the new line number; it's used to test for a super-high number. V is a constant of 256.

The program goes through each line of BASIC including itself and checks that each line number is higher than the previous one and not over 63999. If the line number fails the test, it is set to one higher than the previous line number.

Note the logic: can you see why the program must not be used on a normal "too-high" looney line number? It would "pass" the bad line number, and then bump up the numbers on all following lines.

What do you do if you have both too-low and too-high? Fix the too-high first before you run this program. If you do have multiple faults, chances are your program is in really bad shape anyway: get your computer fixed and redo the whole program.

Looney line numbers should never happen. Look for the cause if it happens to you.

You can fix them, however. And the mechanics of fixing bad line numbers has a tiny bonus: look at the coding and see if you can gain an insight into bow BASIC is put together.

Super-coders can go after the same problems by attacking the program directly as it lies on disk, copying the program over and correcting it on the way. Users with BASIC enhancement packages (Toolkit, Command-O, Power, etc.) can fix everything in a trice with program renumber.

There are many ways of fixing it...once you know how.