Chaining Programs

See all articles from SYNC v4 n1

The Problem

From time to time we have two programs or more that we want to combine—perhaps a main program and a subroutine. Obviously, this can be done if they are both typed in from scratch.

However, what do we do when one program is on the computer and one is on tape or when both are on tape? If we attempt to LOAD a program from tape into the computer when it already has a program resident, we find that it will erase the first program. We could type in the second program, but we certainly would like to avoid that much work. Some computers have a MERGE command to accomplish this, but the 8K ROM does not include a MERGE command.

The ZX/TS Solution

On the ZX/TS computers, however, we do have an option: we can “chain” the programs by the method developed in this article so that a program on tape can be LOADed in the computer when a program is already in RAM.

The method in outline is: 1) Protect the resident program from erasure by POKEing a copy of it above RAMTOP. This area of memory, once created, is unaffected by LOAD, SAVE, and NEW. 2) Now LOAD the other program from the tape in the normal manner. 3) POKE the original program into the program area immediately above the LOADed program. Now you have access to both programs which can be RUN, SAVEd and EDITed.

Now let’s apply the method. Suppose we have two programs, A and B, which we want to chain together, with B following A. We will illustrate the process for the TS1000 with the Sample Programs A and B below.

Steps in Chaining.

The following steps will enable you to chain A and B.

Step 1: Check the line numbers.

Before chaining, you must be sure that none of the line numbers in Program B are used in Program A because a line already on the computer with a given line number is replaced by a new line entered with the same number. If you plan ahead and assign relatively high numbers in Program B, you can avoid having to renumber the lines in Program A. You may want to use a line renumber utility to save editing the line numbers.

Step 2: LOAD (or ENTER) Program B.

We must modify Program B so that whenever it is LOADed, a copy is automatically placed (POKEd) into a protected region of memory, i.e., above RAMTOP, so that it will not be cleared when Program A is entered or LOADed. This is done through steps 3-6 below, but, when it has been done once, it never has to be repeated.

Sample Program B

1000 PRINT 1
1010 PRINT 2
1020 PRINT 3
1030 LET A=4

Step 3: Determine the program size.

Since the program area begins at 16509 and ends at D-FILE-1 (see the chapter on the organization of memory in your manual), you can determine size by typing in the immediate mode (without a line number):

PRINT PEEK 16396+256*PEEK 16397-16509

This yields the number of bytes in the program. Let us call this number n. Note: All italicized letters in lines and listings below must be replaced by their appropriate values prior to execution. For Sample Program B, n = 54.

Step 4: Calculate the new RAMTOP.

Begin with the RAMTOP value for your system: 1K = 17408; 2K = 18432; 16K = 32768. Subtract n. Let’s call the answer r. If you have 64K, omit this calculation and use r = 8192. For Sample Program B, r = 18378.

Step 5: Modify Program B.

To Program B add, with the appropriate line numbers, the following:

STOP
FOR I=0 TO n-1 (line y)
POKE (r+I),PEEK (16509+I)
NEXT I
STOP

Replace n-1 in line y and r with the appropriate values. For Sample Program B, this would give:

2000 STOP
2010 FOR I=0 TO 53
2020 POKE (18378+I),PEEK (16509+I)
2030 NEXT I
2040 STOP

Step 6: SAVE Program B.

Save this program, e.g., SAVE “BB”.

Step 7: Adjust RAMTOP.

Move RAMTOP down n bytes by the following steps. (If you have 64K, omit this step.)

a) After replacing n with the appropriate value and carrying out the calculations, enter the following line in the immediate mode:

POKE 16388,256*(1+INT (n/256))-n

For Sample Program B, this gives:

POKE 16388,202

b) After replacing n and z with the appropriate values (z = 67 for 1K, 71 for 2K, 127 for 16K) and carrying out the calculations, enter this line in the immediate mode:

POKE 16389,z-INT (n/256)

For Sample Program B, this gives:

POKE 16389,71

c) Press NEW and ENTER. While this erases whatever was in memory, it also moves RAMTOP down n bytes.

Step 8: Put Program B above RAMTOP.

LOAD and RUN the routine at line y in the modified version of Program B, e.g., LOAD “BB” and RUN 2010. A copy of Program B is now above RAMTOP and cannot be erased by subsequent LOADs, SAVEs, or NEWs.

Step 9: LOAD Program A.

LOAD Program A from tape (if you are entering Program A, hit NEW and ENTER to get rid of Program B).

Sample Program A

100 PRINT "A"
200 PRINT "B"
300 PRINT "C"
400 LET B=4

Step 10: Determine the size of Program A.

Determine the number of bytes in Program A just as you did with Program B in Step 3 above. Let’s call this number m. For Sample Program A, m = 42.

Step 11: Adjust Program A.

Add the following short routine to Program A, replacing n-1, m, and r by their appropriate values and adding the appropriate line numbers:

STOP
FOR I=0 TO n-1 (line z)
POKE (16590+m+I),PEEK (r+I)
NEXT I
STOP
REM (type in n-6 A's)

This REM line sets aside a total of n bytes for Program B to fit into. The value 16590 assumes that n is two digits, i.e., n is between 10 and 99. For each additional digit in n add 1 to 16590. If you expect to chain these two programs again, SAVE this modified version of Program A, e.g., SAVE “AA”. For Sample Program A the lines would be

500 STOP
550 FOR I=0 TO 53
600 POKE (16632+I),PEEK (18738+I)
650 NEXT I
700 STOP
800 REM (type in 48 A's)

If your Program B is very large, you might want to use the technique discussed by Jasper Kump (SYNC 3:2, p. 60) to reduce the drudgery.

Step 12: Execute the chaining routine.

Type in

GOTO line z

This replaces the REM line in Program A with Program B. In our example this would be

GOTO 550

Step 13: Delete the separating lines.

Delete the remaining lines that separate Programs A and B. In our example, delete lines 500, 550, 600, 650, 700.

Step 14: Restoring RAMTOP (Optional).

If you are short on memory and no longer need the copy of Program B above RAMTOP, SAVE your chained program, and return RAMTOP to its original location by unplugging the computer. Then turn on the computer and LOAD the chained program again.

Conclusion

This process can be extended to chain as many programs as RAM space permits. Also, since the modified versions of the programs have been SAVEd, chaining A and B on subsequent occasions is greatly simplified:

  • 1) Reserve n bytes above RAMTOP.
  • 2) LOAD “BB”
  • 3) LOAD “AA”
  • 4) GOTO z
  • 5) Delete the 5 unwanted lines.

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