@sinclair_research_ltd #AltText4Me #AltText4U

«Knowing where the next byte is coming from: Stephen Devine explains how to squeeze programs into [1K] by memory-saving techniques.
ZX81 owners are often disappointed by the lack of memory on the basic machine. They often find themselves with programs [just] too long to fit within [1K]. However, by using various memory saving techniques, it is often possible lo sufficiently shorten these programs so that they can be run in 1K.
Since [floating] point numbers on the ZX81 uses six bytes for each number, considerable savings in memory can be obtained by replacing all numbers in a program (except [line] numbers) by equivalent symbols. If the numbers 0 and 1 are replaced by NOT PI and SGN PI respectively then four byles are saved each time.
For larger single- or double-digit numbers the VAL function can be used. For example, VAL "4" saves two bytes since the digit 4 is stored as a string and not as a floating point number.
The CODE function can be used for even larger numbers, [if] there is a symbol in the character set which corresponds to the number to be replaced. For exampie, CODE "IF" will replace the number 250.
If the same number is used repeatedly throughout a program, then it may be worthwhile assigning that number to a variable at the start of the program and using the variable each time the number is required. If the number 500 appears repeatedly in a program then it could first be assigned to a variable by, say, LET N =
500. Then statements such as LET X = X+500 could be replaced by LET X = X+N saving five bytes each time.» 1/3

@sinclair_research_ltd #AltText4Me #AltText4U «Replacing all the literal numbers in a program will often save enough memory to enable the program to be run successfully but, if not, [some] further techniques which can be applied.
One method of saving memory is to reduce the memory requirements of conditional statements. For example, a statement of the form: IF A = X THEN LET P=P+1 can be replaced by LET P = P+(A=X) with a saving of six bytes. Much used statements in arcade type games are those using the INKEY function such as:
10 IF INKEYS = "8" THEN LET Y=Y+ 1
20 IF INKEY$ = "5" THEN LET Y= Y-1
which might be used to move an object back and forth across the soreen, These can be replaced by the single statement:
10 LET X=X+VAL "2"*(INKEY$ "9"-INKEYS"5')
saving 13 bytes.

Many PRINT statements can also be modified to consume less memory. For example, when printing insiructions many words can often be replaced by single byte keywords.
Single stroke keywords
Take the statement PRINT "ENTER YOUR NAME". This can be replaced by the equivalent PRINT "INPUT YOUR NAME" where INPUT Is entered as a single-stroke keyword. This saves six bytes, since the spaces before and after the word INPUT are free. Many more keywords such as IF, OR, TO, AND, THEN and others can also be used in this way.
However, to enable certain words to be entered, it may be necessary to first enter THEN and edit it out afterwards.
When a text of concurrent lines is to be printed, such as:
10 PRINT "…….."
20 PRINT "…….."
30 PRINT "…….."
it can be replaced by:
10 PRINT "…….."
"…….."
"…….."
which, in this case, saves six bytes.» 2/3

@sinclair_research_ltd #AltText4Me #AltText4U «It should also be remembered that GOTO's and GOSUB's need not be assigned a literal numerical value, such as GOTO 100, but can be used with functions. For example the routine:
10 IF X = 1 THEN GOTO 100
20 IF X = 2 THEN GOTO 200
30 IF X = 3 THEN GOTO 300
can be replaced by the single statement:
10 GOTO 100*X
saving a massive 40 bytes
By using some, or all, of the techniques outlined above and by experimenting with others you should find that there are few short programs which cannot be squeezed into 1K. Even some larger ones, which would normally use up to 2K of memory can be effectively halved and run in 1K.
If a 16K RAM pack is added to the basic machine, the conservative use of memory should not be neglected. It makes for very efficient programming and, even with [16K], [some] programs [are] just a little bit too long.» 3/3

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.