TS 1000/1500 Program Chaining, Part One

Author(s): Earl Dunnington
Pub Details: Time Designs Magazine v2 n4 (Volume 2, Issue 4)
Date: May/June 1986
Pages: 16-18

Program Chaining can, in effect, increase your RAM memory many times its actual size. It is one of the most neglected subjects of programming texts and instruction. There was an article published in SYNC, Jan/Feb ’84, but it was actually on merging a program on tape with one in the computer, not “Chaining” as defined below. Program Chaining is not just Linking unrelated programs together. Linking can be as simple as saving several programs, by name, on one recording medium with the operator calling the program he wants with the direct LOAD “NAME” command. The programs can be self-running or not. In this case the operator is doing the Linking. Another form of Linking is to have one program load another. This is done simply by inserting as a line in the program, LOAD “PROGRAM NAME”. If the program to be loaded is on a different tape, then a prompt and a pause should proceed the load line. For example:

100 PRINT "CHANGE TAPE, THEN PRESS A KEY"
110 PAUSE 32767
120 LOAD "TWO"

Another common form of Linking is to have a Menu program which automatically loads the chosen self-running program.

To make a program self running, add the line, SAVE “PROGRAM NAME”. Unless it is the first line in the program, it must be preceded by a command that will avoid an endless loop and followed by a RUN or eventually if not immediately by a GOTO. For example:

9997 STOP
9998 SAVE "TWO"
9999 GOTO 10

You have to record the program using the direct command: GOTO 9998

Timex Sinclair 1500 wrapped in chains
Timex Sinclair 1500 wrapped in chains

Chaining is a specialized form of program Linking. It is a method of Linking dependent program modules together making them operate, in effect, like one large program, similar to a GOTO, but using LOAD “NAME” instead. If an endless tape were used it might also be considered similar to a GOSUB and RETURN in a program. A dependent module is a program that operates using variables, strings, or text entered into the computer memory by a previous program. Why is there a need to Chain? One reason is to save memory so that more data or text can be stored. Another is that in some practical applications, the program simply becomes too large to fit into the available memory and the program must be broken into separate pieces that will work together as one program.

Subscribers who have read my series of articles “Adventures in the RAM Jungle and Other Mysteries” Sept/Oct ’85 through Jan/Feb ’86, have already been exposed to two examples of Chained programs with the operator doing the Linking. The first example consisting of the Flypaper program and Program One (Nov/Dec ’85). The second example is the two programs Fig. No.3 and No.4 of the Conclusion.

Constant variables present no problem as they can be recorded in each program module. The variables referred to in this article are those computed by a previous module. There are four ways to pass variables, ect., in Chained programs for the T/S 1000 and 1500. The first is the VARS method, the second is the System Variables method, the third is the Safe area method, and the fourth is the above RAMTOP method. The passage of variables, strings, code, or text from one module to another using the VARS or Safe area methods is made possible because the LOAD, RUN, or GOTO commands do not clean the memory like NEW. Data stored above RAMTOP is safe from all of these commands. CLEAR and RUN clean the VARS area of DIM statements, arrays, variables, and strings. To protect data stored in the Safe Area, never use expanding undimensioned strings in a program. For example, DO NOT use:

10 LET T$=""
20 FOR N=18000 TO 19000
30 LET T$=T$+CHR$ PEEK N
40 NEXT N

In the VARS method, the first module must be the largest. The area in VARS, where the data to be passed is stored in the first module, must be in the Safe area of the second module. The area in VARS of the second module, where the data is stored, must be in the Safe area of the next module, ect. In other words, the top of the Calculator stack during the program RUN of the succeeding module, must not enter the area occupied by the data in the VARS area of the preceding module. (see Fig. No.1). The position of the area of VARS occupied by the data can be adjusted by enlarging the program using REM or DIM statements.

FIGURE NO. 1 Program Chaining (VARS Method) memory map
FIGURE NO. 1 Program Chaining (VARS Method) memory map

Passing the text in a string from one module to the next is not difficult. Variables however, are stored in the program and VARS area using a special floating point Sinclair format. Passing a variable in this format is much more complicated. Any good text on Assembly language or Machine Code for T/S computers, covers the Sinclair format for numerical data and is beyond the scope of this article. It is much easier to pass variables by converting them to a string. For example, type the following into the computer and RUN it:

10 LET A=12345678912345
20 LET A$=STR$ A
30 PRINT A$
The result displayed should be:
1.2345679E+13
If you change line 10 to a negative value
the result displayed should be:
-1.2345679E+13
To convert the string back to a variable
add:
40 LET A=VAL A$
50 PRINT A
and ENTER: GOTO 40

Lines 40 and 50 would be in the next module. As you can see, variables with over thirteen digits to the left of the decimal point are converted to the Scientific notation. The same thing applies to strictly decimal values with more than four zeros to the right of the decimal point.

If you want the string to be passed to be the first data in the VARS area, then use a DIM statement for the string before any other assignments are made, including loops. This reserves the space. Normally the string to be passed will be the last data in assignment and a DIM statement is not required. The last character of the string will then be two addresses below E_Line if undimensioned or dimensioned with only one dimension, like DIM A$(14). For string arrays (two or more dimensions), refer to Appendix of your Owner’s Manual.

To illustrate a Chained program using the VARS method, ENTER: NEW and type in the following module “ONE”

10 LET A=12345678912
20 DIM X$(900)
30 LET A$=STR$ A
40 LOAD "TWO"
50 SAVE "ONE"
60 RUN

Line 10: Assigns a numerical value to the variable A.

Line 20: Makes a large increase in the size of the program by reserving space in the VARS area, raising the address where A$ will be stored into the Safe area of the next module. This Safe Area was determined for module “TWO” using the methods described in the referenced RAM Jungle article.

Line 30: Converts the variable A, whose value is to be passed to the next module, into a string with a length of 11 digits.

Line 40: Automatically loads the next module and prevents the program from going into an endless save loop.

Lines 50 and 60: Make module “ONE” self-running when saved with a GOTO 50 and loaded with the direct command LOAD “ONE”.

Save this program on tape using the direct command GOTO 50. When the diagonal load lines are displayed, stop the tape. Do not rewind the tape as you now have the correct amount of tape from module “ONE” to record module “TWO”. Use the BREAK key to return to the programming mode. Find the address of E_Line by entering the direct command:

PRINT PEEK 16404+256*PEEK 16405

The result should be 18312 (16K RAM) or 17544 (2K RAM). The address of the last character of string A$ is this value minus two.

Enter NEW and type in module “TWO” as follows:

 10 DIM A$(11)
20 LET B=1
For 16K RAM
30 FOR N=18300 TO 18310
For 2K RAM
30 FOR N=17532 TO 17542
Both RAMs
40 LET A$(B)=CHR$ PEEK N
50 LET B=B+1
60 NEXT N
70 LET A=VAL A$
80 PRINT A
90 STOP
100 SAVE "TWO"
110 RUN

Line 10: Reserves space in the VARS area of program module “TWO” for the 11 digits of the string.

Line 30: This is the address of the string passed from the VARS area of module “ONE” to the Safe Area of module “TWO”.

Line 40: Converts the recovered decimal code to the character and stores it in its proper place in the VARS area of module “TWO”.

Line 70: Converts the string to a variable. The other program lines are self explanatory.

Save program module “TWO” on the tape using the direct command GOTO 100. When the error report C/70 appears on the screen, stop the recorder. Rewing the tape. Enter NEW. To operate the chained programs, use the direct command LOAD “ONE”. When the diagonal load lines are displayed, start the tape. After module “ONE” loads and runs, diagonal load lines will again be displayed. After module “TWO” loads and runs the value of the variable passed from module “ONE” to module “TWO” will be displayed.