Hacking in BASIC: Renumbering Including GOTO and GOSUB

Authors

Publication

Pub Details

Date

Pages

BASIC Terms

See all articles from CATS v3 n1

There have been several examples of renumbering routines that have run in the CATS newsletter over the years. They all share a common failing – they could not alter the GOTOs and GOSUBs. This is a problem in BASIC, because all program jumps (GOTO and GOSUB) are absolute jobs. The program jumps to a specific line number, not a specific number of lines forward. When a program is renumbered, a given number may refer to a very different line in the program.

In thinking about the problem, it seemed that the only way to change the GOTOs was via machine code. Programs such as this are available, called toolkits. But even the best of the toolkit programs can’t handle renumbering computed GOSUBs, such as GOSUB 500*X+1000.

Then, a couple weeks ago, I realized that there is a way to change ALL GOTOs and GOSUBs from absolute jumps to relative jumps. And it’s all in BASIC, on either the 1000 or 2068! With relative jumps, a block of instructions can be changed from lines 100-300 to 400-600, while a GOTO that jumps ahead 3 lines (or 30) continues to work.

For the 1000

Start your program with:

1 LET F$="PEEK 16391+256*PEEK 16392"

After running this line, PRINT VAL F$ will return the current line number! This takes advantage of the system variable PPC, that holds the line number being executed. An absolute jump two lines ahead might be written:

20 GOTO 40

(assuming you are using intervals of ten for each line). To change this to a relative jump to two lines ahead, substitute:

20 GOTO VAL F$+20

As written, it will GOTO line 40. If it is renumbered to line 80, it will GOTO line 100.

Computed GOTOs are possible, as in GOSUB VAL F$*500+1000. The only time this will fail, is if you change the number interval, or add a line of code within the numbers skipped by the GOTO. Obviously, this is more likely for large jumps than small – but it’s the small jumps that often can’t be avoided.

For the 2068

The 2068 has a real DEF FN command, and this is a perfect place to use it.

  1 DEF FN f()PEEK 23621+256*PEEK 23622
20 GO TO FN f()+20

Try this out yourself – a renumbering program is hidden in lines 9000 to 9070 of the AMORTIZER program in this issue.

Products

 

Media

 

Image Gallery

Source Code

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

Scroll to Top