Automatic VIC Appending
It's quite simple to add two programs together to make a single, larger program. This brief tutorial shows how and explains how the VIC automatically relocates programs in memory.
One of the nice features of the VIC is the autorelocation of BASIC programs during a LOAD. The VIC puts a program into the correct place in RAM memory automatically because programs can be located at three different places in VIC, depending on the memory size that it has. If you saved a program on a 3.5K VIC and later on you bought a 3K expander, it would be next to impossible to RUN that program if the locator didn't make an adjustment.
BASIC on a 3.5K machine expects the starting memory address to be 4097. All programs are saved with this memory address as their starting point. On an expanded-by-3K VIC, the starting memory address is 1025. Since the starting point of BASIC can thus vary, it's left up to the relocator to set things right.
How The Relocator Works
The relocator first checks to see where the start of BASIC is. This is an address POKEd by the computer into locations 43 and 44 when the VIC is switched on. This "start of BASIC" address is where the relocator will begin to store any program that the VIC is LOADing. Note: This does not include programs that are saved using absolute save mode, as in Jim Butterfield's "Tinymon" (COMPUTE!, January 1982, #20).
Since the relocator depends on the "start-of-BASIC" memory locations (called "pointers") to know where to start storing a program during a LOAD, it is possible to join two separate programs by using a method that I will describe later on. Note that the two programs to be joined must not have overlapping line numbers and that the program in memory at the time must have lower line numbers than the program you are "appending" onto it from tape.
Type in this example program:
50 REM PART 2 OF TEST PRG. 60 PRINT "THIS A TEST" 70 PRINT "TO SEE A VIC" 80 PRINT "APPENDINGI"
Now save this example on tape and clear the memory using NEW to make way for the next program:
SAVE "PART 2" PRESS PLAY & RECORD ON TAPE OK SAVING PART 2 READY. NEW
Now type in this example program:
10 REM PART 1 OF TEST PRG. 20 PRINT "WILL THIS WORK?" 30 PRINT "I HOPE IT DOES." 40 PRINT "I KNOW IT WILLl"
I had you type in the second part first so that part one, the program we are appending, is in memory, and part two is on tape.
Clear the screen and type the following in direct mode:
On a 3.5K machine you should get 1 and 16, respectively. Write down these printed values because you're going to need them again later on.
Now type in the following in direct mode:
POKE 43, PEEK(45)-2 : POKE 44,PEEK(46) LOAD"PART 2" PRESS PLAY ON TAPE OK SEARCHING FOR PART 2 LOADING PART 2 READY.
The above lines typed in direct mode set the start of BASIC to the end of the current program already in memory. Then you load part two as you would any other program. The key to the whole thing is that the relocator will use as its starting location the start of BASIC which is directed by locations 43 and 44.
There is one final step before the two programs are finally appended. You must reset the start of BASIC to what it was before you loaded in part two. To do this, you simply POKE the two values that you previously PEEKed into their respective memory locations. For a 3.5K machine it would look like the following:
POKE 43,1 : POKE 44,16
If you made no error in the procedure outlined above, you should be able to list the whole program with both parts together. Appending can be a very powerful programming aid, since it allows you to develop several sets of subroutines and then later on lets you patch them into a main program at will.
Why not create a library of frequently used subroutines (that: shuffle cards, round off numbers, accept INPUT in a special way, anything you use in lots of different programs)? Simply use high line numbers for the subroutines (50000 and up) and then use this technique to add them painlessly to your main programs.