Getting right to the point of this article, why would anyone want to save only the variables from a program? There are two reasons. First, some programs are used to store various sets of data. For example, I use PRO/FILE to keep track of student information, music lists, customers, and other things. I could not store all this at one time in the computer. If I had to load the program with the data in each case, I would waste considerable time. A second reason would be to use a different program to operate on the same data as another program originally created. Again, using PRO/FILE as an example, you may want to load your PRO/FILE data into another program which would do a search and replace operation or other functions. It’s true that the 2068 permits you to save dimensioned data, but you may have other variables which serve as pointers that need transferring as well.
To save the variables without the program, you could DELETE 0,9999 before saving, but the SAVE and VERIFY commands would have to be entered directly. Also, you may still need to keep the program in the computer for further use. After a lot of experimenting, I may have a solution. The “may have” is because the process alters the system variables and, although it seems fine so far, I prefer to test it much more before using it with complete confidence.
The process simply involves fooling the computer about where to start saving. Since we only want the variables, we need to make the computer think that the start of the variables is the start of the entire program.
The computer stores things in certain places or in a certain order. For instance, the ROM is always found from address 0 to 16383. Next comes the display and attribute files followed by the printer buffer. Eventually we come to the start of the BASIC program which on the 2068 is normally found at address 26710, but it can be moved. Right after the BASIC ends, we find the start of the variables. The exact starting point varies according to the size of the BASIC program. Actually, when you first turn on the computer, the variables and the program start at the same place because no program exists.
The system variables help the computer keep track of these things. System variables are not like the variables we use (like LET X=10). Instead they are special storage addresses which tell the computer where to find things or to do certain things (like add a key click)
One of these variables is called PROG (short for program). This tells the computer where the program starts and you can find PROG stored in addresses 23635 and 23636. If you PEEK those addresses you will find the values 86 and 104. The actual start of the program is found by the formula 86+256*104 which equals 26710. The start of the variables is similarly found by peeking VARS (addresses 23627 and 23628).
To start saving at the start of VARS, we first POKE PROG with the values presently in VARS. As long as no GOTO or GOSUB instructions are encountered before PROG is reset, the program will continue to function. After saving and verification is completed, PROG is set back to its original values. If an error occurs before it is reset, you will not find any BASIC until you poke the original values back. This same process works on the Spectrum except the original value of PROG is different. If you have Interface 1 connected to your Spectrum, you will find that PROG also changes as soon as SAVE, LOAD, VERIFY, or MERGE is used. Once it changes, it remains the same so you can still use this process. It will not work if saving to ZX Microdrives though, only tape. If you have any hardware or software installed which moves PROG, then you must adjust the values to reset PROG to use this program. To find the correct values, just set up your hardware or load your software and PEEK into PROG to get the correct numbers.
To load the saved variables into another program, first set PROG equal to VARS again and LOAD the variables, then reset PROG. At first I just MERGED the program without changing PROG and it worked on a small test program, but when trying on PRO/FILE I always got an out of memory error. By changing PROG it worked fine. Since I don’t understand why this happens, I consider this experimental.
The accompanying listing shows a simple example for the 2068 and gives the changes necessary for the Spectrum with or without Interface 1.
5 PRINT AT 10,3;"PRESS A KEY TO START TEST": PAUSE 0: CLEAR
10 LET A=1: LET B=2
11 LET A$="A": LET B$="B"
12 DIM X(2): LET X(1)=1000: LET X(2)=2000
13 DIM Z$(2,3): LET Z$(1)="ABC": LET Z$(2)="DEF"
100 PRINT ''A,B,A$,B$,X(1),X(2),Z$(1),Z$(2)
200 PRINT '" PRESS TO CONTINUE WITH SAVING": PAUSE 0: BEEP .1,30
1000 POKE 23635,PEEK 23627: POKE 23636,PEEK 23628
1010 SAVE "VARIABLES": PRINT ''TAB 8;"REWIND TO VERIFY": VERIFY "VARIABLES"
2000 POKE 23635,86: POKE 23636,104
5000 CLEAR : PRINT "OLD VARIABLE VALUES SAVED"'"HERE ARE NEW VALUES:": LET A=-1: LET B=-2: LET A$="a": LET B$="b": DIM X(2): LET X(1)=-1000: LET X(2)=-2000: DIM Z$(2,3): LET Z$(1)="abc": LET Z$(2)="def": PRINT 'A,B,A$,B$,X(1),X(2),Z$(1),Z$(2)
5010 PRINT '" REWIND TAPE TO LOAD OLD VALUES": POKE 23635,PEEK 23627: POKE 23636,PEEK 23628: LOAD "VARIABLES": POKE 23635,86: POKE 23636,104
5020 CLS : PRINT "RESTORED VARIABLES ="'A,B,A$,B$,X(1),X(2),Z$(1),Z$(2): STOP
9000 SAVE "TEST" LINE 1: PRINT AT 10,0;"REWIND TO VERIFY PROGRAM": VERIFY "TEST"
Lines 5 through 200 first wait until you want to start, assigns different types of variables, prints them on the screen, and waits again to continue. The pauses also give you the opportunity to break in before PROG is changed if you wish. When I broke in on the second PAUSE during testing, the first POKE was sometimes activated, so I added the BEEP to prevent this.
Line 1000 starts the actual meat of the program and thus the part you would use in your own projects. This simply pokes PROG with the contents of VARS.
Line 1010 saves and verifies the program which only contains variables.
Line 2000 resets PROG to its original value. If you have a Spectrum, change the 86 and 104 to be 203 and 92. If Interface 1 is connected change the POKES to 5 and 93 respectively. Remember that Interface 1 does not change PROG until SAVE, LOAD, etc is activated, thus be sure this has been done first. This ends the actual SAVE process.
Line 5000 simply changes the values of the variables to prove we can later reload the original values. These new values are printed on screen.
Line 5010 first prints instructions. Next the actual LOAD section is activated: first PROG is poked with VARS, then the program of variables is loaded, then PROG is reset.
Line 5020 prints the restored variables which should be the same as when saved.
Line 9000 simply lets you make a copy of this test program.
I hope this works for you. If you have any questions, ideas, or discoveries, contact Robert C. Fischer; Rt 2 Arizona St; Emerson, Ga. 30137
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.