1K RAM

One of the most serious problems that TS1000 users are faced with (and ZX81 users even more so) is the lack of memory (RAM). Most commercial programs require a 16K RAM pack, and many printed in newsletters and magazines require over 2K (or 1K for ZX81 people). Although the package or listing calls for 16K, this does not mean that the program is 16K long. It just means that you must have more RAM than your machine comes with and 16K is the next increment.
If you do not intend to expand your system RAM, the only solution is to shorten programs which might fit then into your RAM.
This article collects over thirty tips to save memory. Some of them have appeared in SYNC; hopefully some of them are new to you. Some are simple, obvious hints, while others require some clever programming. When dealing with 2K (or less) every byte counts!
You can calculate the bytes in the various lines as follows: Line numbers take up two bytes no matter whether the number is 1 or 9999. Between the line number and the text of the line in the computer’s memory are two bytes telling the computer how long that line is from the first byte of the text of the line up to and including the end of line marker (CHR$ 118). In the line itself all letters, keywords, etc., typed by a single keystroke take up one byte except numbers. Numbers take up six bytes plus the number of digits and one byte for the decimal point if any. After the text of the line is an end of line marker telling the computer where the line ends. This marker takes one byte.
Memory Scrunching Tips
1) REM statements
Eliminate REM statements and include them in your documentation.
2) Game elements
Eliminate one or more game elements. This might make the game fit into 2K.
3) Restart mechanisms
Omit restart mechanisms completely. E.g., “Do you want to play again? Y or N.”
4) STOP statements
If the last program line is a STOP statement, delete it. The program will stop anyway only with a different error code.
5) Keywords and tokens
Use keyword and token expressions. When entering a line such as:
10 REM TO RUN USE GOTO 100
use the keywords for the words TO, RUN, and GOTO. This line is entered by typing exactly: 10, E (REM), SHIFT 4 (TO), SHIFT 3 (THEN), R (RUN), SHIFT 5 (BACKSPACE), SHIFT 0 (DELETE THEN), SHIFT 8, U, S, E, SHIFT 3 (THEN), G (GOTO), SHIFT 5 (BACKSPACE), SHIFT 0 (DELETE THEN), SHIFT 8, 1, 0, 0, ENTER. Do this several times to get used to it. If necessary, reword prompts to allow for this technique.
6) PRINT statements
Shorten or eliminate PRINT statements, and include the information in your documentation in a notebook for programs. For example, replace A with B.
A: 10 PRINT "DATE OF BIRTH?"
B: 10 PRINT "BIRTHDATE?" or "BORN?"
7) Commas
When lining up PRINT lines, use commas or hyphens instead of spaces. For example, replace A with B or C.
A:
10 PRINT "DO YOU WISH TO FIGHT THE CENTIPEDE OR FLEE?"
B:
10 PRINT "DO YOU WISH TO FIGHT THE","CENTIPEDE OR FLEE?"
C:
10 PRINT "DO YOU WISH TO FIGHT THE CENTI- PEDE OR FLEE?"
8) PRINT AT’s
Combine PRINT AT’s into one line. For example, replace
10 PRINT AT 2,14;"HI"
20 PRINT AT 5,12;"THERE,"
30 PRINT AT 8,12;"SALLY"
with
10 PRINT AT 2,14;"HI";AT 5,12;"THERE,";AT 8,12;"SALLY"
9) Combine lines
Rearrange programs to allow for a combination of lines. This can change the results of the program so be careful. For example, replace
50 IF Y=9 THEN STOP
60 INPUT I$
70 IF I$="YES" THEN STOP
80 GOTO 20
with
50 INPUT I$
60 IF Y=9 OR I$="YES" THEN STOP
70 GOTO 20
Remember to change GOTOs and GOSUBs to their new line numbers.
10) Parentheses
Eliminate unnecessary parentheses. For example, replace A with B.
A: 10 LET I=(A*10)+(B*5)
B: 10 LET I=A*10+B*5
Because of the order of operations (refer to manual) this will be evaluated properly.
11) Conditional statements
In certain cases a condition can be changed without changing the effect. For example, replace A with B.
A: 10 IF Y=10 THEN PRINT "YOU KILLED IT."
B: 10 IF Y>9 THEN PRINT "YOU KILLED IT."
This saves one byte because 9 has only one digit. However, if floating point numbers are utilized in the program, this technique may not work.
12) Variable names
Do not use words for variables. Replace A with B.
A: 10 LET STRENGTH=10
B: 10 LET S=10
Include variable descriptions in your documentation.
13) IF…THEN STOP
If a line contains a conditional jump to a STOP statement, replace it with an IF…THEN STOP statement. For example, replace
100 IF Y<10 THEN GOTO 400
.
.
400 STOP
with
100 IF Y<10 THEN STOP
14) Arrays
Do not set up an array with more elements than needed. If 54 elements are required, use DIM A(54), not DIM A(60).
15) 0 in arrays
Eliminate statements which initially set array elements to 0. For example,
10 DIM A(4)
20 LET A(1)=0
30 LET A(2)=1
40 LET A(3)=0
50 LET A(4)=9
Delete lines 20 and 40. After execution of line 10 all elements are set to 0 automatically.
16) CLEAR
Try to use the CLEAR command if possible. CLEAR can sometimes save a program just before it runs out of memory. CLEAR is one of the least used commands in Basic.
17) GOTO/CLEAR
In very rare cases RUN can replace a GOTO/CLEAR combination. For example:
65 CLEAR
70 LET A=7
.
.
175 GOTO 65
Delete line 65 and change 175 to 175 RUN 70. Because RUN clears all variables, it can replace a CLEAR and a GOTO.
18) One time variables
If a variable is only used once do not assign the variable. For example, replace
10 LET D=INT (RND*10)+1
20 FOR I=1 TO D
30 PRINT "█"; (inverse space)
40 NEXT I
with
10 FOR I=1 TO INT (RND*10)+1
20 PRINT "█"; (inverse space)
30 NEXT I
This allows you to delete a line (with a slight lengthening of another) and not assign an unneeded variable.
19) Reuse variables
Reuse variables. Once a variable has been assigned it remains in memory, even if it is not used again in the program (unless RUN, CLEAR, or NEW is used). Look at this program:
10 FOR X=1 TO 10
20 PRINT " ";
(10 inverse spaces)
30 NEXT X
40 LET D=INT (RND*10)+1
50 PRINT "YOUR STRENGTH IS ";D
The variable X will take up memory until RUN, CLEARed, or NEWed. Even though no longer used, it occupies precious bytes of RAM. A better program would be:
10 FOR X=1 TO 10
20 PRINT " ";
(10 inverse spaces)
30 NEXT X
40 LET X=INT (RND*10)+1
50 PRINT "YOUR STRENGTH IS ";X
Now, instead of both X and D stored in memory, only X is in memory.
20) Reuse variables without reassignment
In certain cases a variable can be reused without being reassigned. For example:
10 FOR X=1 TO 10
20 PRINT " ";
(10 inverse spaces)
30 NEXT X
40 LET X=11
50 PRINT "YOU NOW HAVE ";X;" GOLD PIECES."
Line 40 can be deleted. After completing the loop X is already equal to 11. However, not many variables are initially set to 11; ten is more likely. Thus:
21) FOR-NEXT variable range
Replace the previous example with:
10 FOR X=0 TO 9
20 PRINT " ";
(10 inverse spaces)
30 NEXT X
40 PRINT "YOU NOW HAVE ";X;" GOLD PIECES."
After the loop X equals 10, which is easier to work with than 11. This also saves memory. The number of bytes needed to store an integer equals the number of digits plus six. In line 10, the use of 0 and 9 saves one byte in place of 1 and 10, because 10 has two digits and 9 has one. Compare:
Tip 21:
0 = 1 + 6 = 7 bytes
9 = 1 + 6 = 7 bytes
for a total of 14.
Tip 20:
1 = 1 + 6 = 7 bytes
10 = 2 + 6 = 8 bytes
for a total of 15.
22) Number substitutes
Because numbers require a minimum of seven bytes of memory, too many numbers quickly fill up the RAM. Therefore, replace line A with line B below.
A: 10 LET E=0
B: 10 LET E=NOT PI
0 requires 7 bytes; NOT PI uses 2 bytes. Some other expressions for 0 are USR PI, SIN PI, and TAN PI, each requiring two bytes.
A: 10 LET E=1
B: 10 LET E=SGN PI
1 requires 7 bytes; SGN PI uses 2 bytes
A: 10 LET E=3
B: 10 LET E=INT PI
3 requires 7 bytes; INT PI uses 2 bytes
Some other expressions and their equivalents are:
COS PI = -1
PEEK PI = 255
PEEK NOT PI = 211
PEEK SGN PI = 253
PEEK PEEK PI = 135
LEN STR$ PI = 9
CODE STR$ PI = 31
This list is by no means complete.
For any other numbers use VAL (see tips 23 and 24 for exceptions) as shown in B below to replace A.
A: 10 LET E=2
B: 10 LET E=VAL "2"
A: 10 LET E=327
B: 10 LET E=VAL "327"
In each case the use of VAL saves three bytes. Note:
2 = 7 bytes
VAL "2" = 4 bytes
I have yet to find a better expression to replace VAL “2”.
327 = 9 bytes
VAL "327" = 6 bytes
This tip is used not just in assigning variables. One of the above methods can be used to substitute for a number anywhere a number is used.
However, do not put VAL around each number in an expression:
10 LET E=(INT (RND*VAL "10")+VAL "5")*VAL "100"
Instead put VAL around the entire expression:
10 LET E=VAL "(INT (RND*10)+5)*100)"
This way you get the most out of VAL.
Remember, though, that extensive use of VAL, NOT PI, etc., can slow down a program tremendously, but that is the sacrifice for memory saving.
23) VAL exception
An exception to the use of VAL is sometimes encountered:
10 FOR X=NOT PI TO VAL "20"
VAL “20” uses five bytes. If this line is replaced with:
10 FOR X=NOT PI TO EXP INT PI
two bytes may be saved. EXP INT PI uses only three bytes. Although EXP INT PI = 20.085537, this is rounded to 20 when used in combination with FOR. This means that in the revised program in tip 18, the INT in line 10 may be deleted. EXP PI could substitute for the number 23 using the same method.
24) Variable instead of VAL
Another exception is when a single number is used many times. Assign a variable to it instead of using VAL. For example, replace
20 IF I>10 THEN GOTO 80
22 INPUT I$
24 IF I$="FIGHT" THEN GOTO 80
26 IF A>80 THEN GOTO 6400
28 LET U=B+80
30 IF U/T>80 THEN GOTO 251
with
10 LET Y=80
20 IF I>10 THEN GOTO Y
22 INPUT I$
24 IF I$="FIGHT" THEN GOTO Y
26 IF A>Y THEN GOTO Y*Y
28 LET U=B+Y
30 IF U/T>Y THEN GOTO Y*PI
25) Strings in PRINTs
Similarly, if the same set of characters must be PRINTed several times, assign a string variable to those characters and PRINT that string. Keep in mind that it takes up memory to assign the variable as well as to keep the variable in the variable storage of the computer. So use this method only when the string must be PRINTed enough times to make it worthwhile.
26) Destination changes
Change GOTO (or GOSUB) destinations. For example:
10 GOTO 100
.
.
100 PRINT "YOU FACE A DRAGON"
.
.
Line 10 can be changed to 10 GOTO 99 and take advantage of a characteristic of the TS1000, namely, that, if the GOTO or GOSUB destination line does not exist, the computer skips over it and goes to the next line. Thus in the example above, the computer, finding no line 99, will go on to 100. This saves one byte since 99 has two digits, not three.
27) Calculated destinations
The following is often seen in a program:
100 IF I=1 THEN GOTO 1000
110 IF I=2 THEN GOTO 2000
120 IF I=3 THEN GOTO 3000
130 IF I=4 THEN GOTO 4000
This can be replaced with a calculated GO TO. For example:
100 GOTO I*1000
If your program is not numbered to allow a calculated GOTO, renumber it, or:
28) Logical destinations
Use a bit of logic. For example, the following lines:
10 IF I=1 THEN GOTO 100
20 IF I=2 THEN GOTO 219
30 IF I=3 THEN GOTO 235
40 IF I=4 THEN GOTO 900
can be replaced by:
10 GOTO (I=1)*100+(I=2)*219+(I=3)*235+(I=4)*900
Of course, we could use VAL around that expression.
10 GOTO VAL "(I=1)*100+(I=2)*219+(I=3)*235+(I=4)*900"
29) Logical combinations
Use logic to combine statements. For a detailed discussion of the logical operators and techniques see Sharon Aker’s article in this issue.
30) Replacements
Replace A with B:
A: 10 IF Y=0 THEN...
B: 10 IF NOT Y THEN...
A: 10 IF Y<>0 THEN...
B: 10 IF Y THEN...
31) Shortened restarts
This is a very impressive tip. Many games have a restart mechanism. For example:
810 PRINT "ANOTHER GAME? (Y/N)"
820 INPUT A$
830 IF A$="Y" THEN RUN
This can be shortened, but a few simple adjustments are needed:
Change 820 to 820 INPUT A
Change 830 to 830 RUN
and make sure one of the program’s variables is Y and none are N. Then, if “Y” is entered in response to the prompt in line 810, the computer will accept that as a legitimate response (because of the expression evaluator) and continue to line 830, where the program will be RUN. If “N” is entered, the computer will stop with an error code 2/820. This is what your final program would look like:
10 LET Y=10
.
.
810 PRINT "ANOTHER GAME? (Y/N)"
820 INPUT A
830 RUN
Do not add a line in assigning the variable Y. Change one of the other variables in the program to Y. It is presumed that line 10 above had previously used a different variable such as 10 LET A=10.
32) False saving
There is one technique which some programmers use in an attempt to save memory, but it actually wastes memory instead. Lines such as these:
10 PRINT "YOU HAVE NO STRENGTH"
20 PRINT
are replaced with
10 PRINT "YOU HAVE NO STRENGTH",,
and then line 20 is deleted. This appears to save 4 bytes. Actually it wastes memory. The PRINT statement puts an end of line marker into the display file. Therefore a blank line on the screen takes up one byte when created by a PRINT used by itself as in line 20. When a comma is used, the computer fills the display file with spaces until the proper print position is attained. So it is better to have a slightly longer program than a much larger display file.
When all these tips are used, a program can often be reduced by as much as 50 percent. Note that in many of the above examples the revised program lines can be shortened even more by using some of the other tips in this article, but that would make the examples confusing.
Speed Tips
Along with the problem of memory, TS1000 and ZX81 users often complain about speed. Here are a few tips to increase the speed of a program:
1) Unnecessary calculations
Eliminate unnecessary calculations. VAL, NOT PI, and other memory savers slow down the program. Replace A with B.
A: 10 LET U=Y*20/4*X
B: 10 LET U=Y*5*X
2) Unnecessary lines
Delete unnecessary lines, e.g., REMs.
3) Eliminate GOTOs
Rearrange the program to eliminate as many GOTOs as possible.
4) Subroutines up front
Subroutines used often should be located as near the beginning of the program as possible. When the computer comes across a GOTO or GOSUB, it searches for its destination line from the beginning of the program.
5) Redundant GOTOs
In situations like this:
30 GOTO 180
.
.
180 GOTO 90
Change 30 to 30 GOTO 90.
6) Arrangement of variables
Assign the variables that are used the most first. When the computer comes across a variable, it searches through its variable area from the beginning to the end. Variables are stored in the order that they are assigned.
As a general rule, speed is often given up for memory saving and vice versa, unless machine language is used. Machine language is FAST and short.
This collection of memory saving tips is by no means complete. If you can add to the collection, send your tip to SYNC. Be sure to give examples.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.