RENUMBERING INCLUDING GOTO and GOSUB
There have been several examples of renumbering routines that have run in the CATS newsletter over the years. They have all shared a common failing – they couldn’t alter the GOTOs and GOSUBs. This is a problem in BASIC, because all program jumps (GOTO & GOSUB) are absolute jumps. The program jumps to a specific line number, not a specific number of lines foreward. 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 of 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 the 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 to 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 also possible, as in GOSUB VAL F$*500+1000. The only time this will fail, is if you change the numbering 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 just 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
followed by
20 GOTO FN f()+20
Try this out for yourself – a renumbering program is hidden in lines 9000 to 9070 of the AMORTIZER program in this issue.
Mark Fisher
For parts for all those hardware projects, try: Revacto or Heathkit in Rockville, Electronics Plus in College Park, or Mark [no relation] Electronics in Beltsville.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.