Because the standard 2K byte Timex/Sinclair 1000 has limited memory capacity, the conservation of memory space is important. A program that wastes precious memory space—and uses all of it—may of necessity be shorter than a program that uses all of that space sparingly. For a given memory capacity, space-saving programs can mean longer programs, which, in turn, may mean more interesting programs that do more.
The following are programming techniques and strategies for conserving memory space on the T/S 1000:
1. Use short variable names. For example, use M instead of MONTHLY PAYMENT.
2. Eliminate REM statements.
3. Reuse variables.
4. Use CLEAR to clear space used by variables.
5. Use CLS to clear the TV screen and display a file.
6. Use predefined constants rather than defining them.
Constant Use 0 PI-PI
PI-PI requires 3 bytes. An even shorter way of expressing it would be to use NOT PI.
Note: To see the symbol PI, press SHIFT and ENTER to get the F cursor. Then press only the M key, labeled PI below.
Constant Use 1 PI/PI
7. For general constants up to 255, use CODE. Refer to Appendix A for the appropriate character codes.
Constant Use 28 CODE "0" 29 CODE "1" 50 CODE "M" 66 CODE "PI" 128 CODE BS
In the last example, the graphics character of the black square (inverse video blank) was used. Of course, CODE ” ” (blank) can be used for 0, but this notation uses 4 bytes instead of the 2 bytes used by NOT PI.
8. Use VAL instead of numbers. For example, use
VAL "8000"
instead of the number 8000. The computer will evaluate the VAL of the string “8000” and return the number 8000. VAL is a more general way of saving memory than CODE because number strings of any length can be used. CODE is restricted to numbers from 0 to 255. Not all of these numbers are available because no characters are defined for codes 67 through 111 and 122 through 125, whereas other codes correspond to nonprinting characters, such as BREAK.
VAL can also be used with expressions. To round off numbers at several places in a program, use a character string as the rounding function. For example, the following program rounds off input numbers X to 2 decimal places:
10 LET A$="INT(100*X+.5)/100"
20 PRINT "X=?";
30 INPUT X
40 PRINT X
50 PRINT VAL A$
60 GOTO 20
Try running this program for 1.555. The result will be 1.56. The function VAL A$ will round off the value of X to 2 decimal places. To round off numbers in 10 lines, use VAL A$ in those 10 lines—or anywhere else that rounding is needed. Set the X=value to be rounded, then do a VAL A$.
9. Use a single PRINT statement wherever possible rather than multiple PRINT’s. For example,
10 PRINT "CHECKS-1"
20 PRINT "DEPOSITS-2"
can be replaced by
10 PRINT "CHECKS-1",,"DEPOSITS-2"
10. Use AND, NOT, and OR whenever possible rather than IF. For exapmle,
IF INKEY$ ="6" THEN LET X=X+1
can be replaced by
LET X=X+1 AND INKEY$="6"
which is one byte shorter.
Also, in an IF, to check to see if a number is greater than 0, use
IF X THEN GOTO 50
which saves 8 bytes, instead of
IF X>0 THEN GOTO 50
If the IF test is true, then the test is equal to 0.
11. Make a program end with the last line rather than using a STOP in the program. For example, instead of
10 PRINT 2+2
20 STOP
use
10 PRINT 2+2
The logic of a program can also be rearranged to end without a STOP.
12. Use parentheses as little as possible because the symbols () use memory space for storage and execution. Parentheses also slow down program execution because the computer must store temporarily the results during execution.
13. Use numeric and string variables in a program when employing the same constants in several places. For example,
10 LET A=5.35
20 LET N$="CHECK"
substitutes A for 5.35 throughout the program. Similarly, for the string “CHECK” use N$.
14. If you are really cramped for memory, define program variables using the direct keyboard input of their values. For example, instead of
10 LET A=2000
20 PRINT 2*A
use
10 PRINT 2*A
then
LET A=2000
GOTO 10
Using RUN here will produce an undefined variable message because the value of A was not defined in a statement.
15. Use slices to store data in variables. For example, to store the following,
X(1)=13
X(2)=18
X(3)=27
X(4)=19
use
10 DIM X(4)
20 LET A$="13182719"
30 FOR I=1 TO 4
40 LET X(I)=VAL A$(2*I-1 TO 2*I)
50 NEXT I
Slicing can also be used to assign strings and characters.
Of course, for the simple example above, four LET statements would need less memory. However, the slicing technique is advantageous when much data must be assigned.
16. Use concatenation to construct numbers. For example, when the numbers 205, 5.35, and 2055.35 must be used many times in a program, use
10 LET X$="205"
20 LET Y$+"5.35"
30 LET W$=X$+Y$$L
Now W$ is the string “2055.35” by concatenation, which saves 4 bytes of memory, compared to a line like
30 LET W$="2055.35"
Notice that because every character following the equal sign takes up one byte of memory, the X$+Y$ is 4 characters shorter than “2055.35.” Again, use judgment in deciding whether it is more economical to concatenate or define directly.
17. Use BASIC tokens in strings as much as possible rather than spelling out each letter. For example, to print “RUN PROGRAM,” it is not necessary to spell out each letter in RUN, as in
PRINT "RUN PROGRAM"
Instead, use
PRINT "RUN Program"
to enter RUN as a token.
1. Press SHIFT and THEN.
2. Press RUN keyword.
3. Press SHIFT and the 5 key to lock the K cursor before RUN.
4. Press SHIFT and DELETE to delete the THEN.
This trick with THEN can be used to enter keywords and then delete the THEN. Of course, once THEN is entered, it does not have to be entered again. For example, to print the words THEN STOP, just use
PRINT "THEN STOP"
Note that the BASIC shifted words, such as AND, STOP, etc., can be directly entered by pressing SHIFT and the word, even with the L cursor.
Similarly, the shifted functions under the keys can be entered directly. Try entering
PRINT " AND NOT STOP "
The string between quotation marks uses only 3 bytes with BASIC tokens, compared to the 14 bytes the string would need if each letter and space were entered separately.