Moreland’s Memo

See all articles from T-S Horizons n2
Author(s): Bill Moreland
Publication: T-S Horizons
Pub Details: T-S Horizons n2 (Issue 2)
Date: December 1983
Pages: 4-5

MEMORY REDUCTION

The Timex computer is a wonderful machine, but it does have its limitations. With clever programming, some thought about alternatives, and a few hours spent “on” the computer most of the difficulties can be overcome.

For example, I have all of my accounting and cash register functions set up on a modified Timex 1000. (The modification is a fancy keyboard.) As many of you know, we own an entertainment store in Columbus. All of our retail transactions are entered on the TS1000 with receipts printed for the customer as well as business records. Each evening we off-load the program after printing the daily receipts, week-, month-, and year-to-date totals (long load and save times are a pain for us too!)

Cash register receipt from Magic Castle Video.
Cash register receipt from Magic Castle Video.

Originally, this program was about 39K. But after waiting 25+ minutes for loading or saving, we decided to shorten and modify the program as much as possible. Using the tricks described below, the power of the program was increased while reducing the total memory requirements to less than 8K.

Let’s start with the little appreciated fact that the T/S stores numbers differently than it stores characters. The number “1” occupies seven bytes, while a character (even numbers stored as characters) are stored as single bytes.

For example, the expression

LET A = 1234

stores the number 1234 in ten (10) bytes in the variables area of memory (the number of bytes needed to store an integer is the number of digits plus six). On the other hand, the expression

LET A = VAL "1234"

stores the characters “1234” in four (4) bytes. (Formally speaking the leading and trailing quotes and the function VAL also occupy space–three (3) bytes.) A savings of three (3) bytes (including the other stored characters) does not sound like much, but the savings are cumulative.

A second technique that saves a lot of memory is the use of assignment statements to fix values in GOSUB or GOTO transfers. For example, using GOSUB 1000 for a transfer seems simple and straightforward. Yet, if statement 1000 is a major transfer point that is used as an entry many times during a program, then a simple LET A = 1000, GOSUB A sequence will save many bytes. To estimate how many, multiply the number of repetitions times the saving, eg. 25 (GOSUB calls) * 9 (bytes). = 225 bytes.

Combining both of the above techniques in an example may help. Suppose you want to use statement 1000 as an entry point for a print routine that is called several ways by the main program. (We have forty different ways to get to a print point in our cash register program.) Or, suppose you want to use statement 1000 as an entry point for calculation and storage in a large array of data. Whatever the uses of these techniques, it looks like the following:

 15 LET A = VAL "1000"
.
.
95 GOSUB A
.
.
200 GOSUB A
.
.
400 GOTO A

Each time the variable A is specified in a statement within the program instead of the four-digit integer, nine (9) bytes of precious memory are saved. Total savings will, of course, depend upon how many times A is used.

A third technique we use can save an enormous amount of memory when dealing with large data arrays. First the theory, then I will list a subroutine that may be very useful.

Most of us grew up learning the decimal number system (base 10), and we all learned that other number systems were possible, even desirable, under special circumstances. Storing large amounts of data in a limited memory is just such a circumstance. Since the computer uses a binary (base 2) number system, and since the computer can operate (translate) with other number systems quite easily, why not use a number system that maximizes the storage capacity of the machine?

Since most of us are not mathematicians, and are not interested in number theory, I won’t go into the details of conversion from decimal to octal or hexadecimal or base 36. Suffice it to say we chose to use the base 256 as our number system because the Timex works well as an eight-bit processor on this number base.

Using this method, the Timex can store any integer between -32,767 and +32,767 (decimal) in two (2) bytes. Normally, as we saw above, a four-digit integer requires ten bytes of memory. So significant memory savings occur by using a simple conversion routine before storing a number in an array. The listing below shows a simple input and output procedure that incorporates the base 256 conversion. One caution: Remember to convert the contents of your array back to decimal form before operating on or printing the data!

9900 DIM F$(5,25)
9910 FOR X=1 TO 5
9920 INPUT ZX
9930 LET F$(X,24)=CHAR$ INT (ZX/225)
9940 LET F$(X,25)=CHR$ (ZX-CODE F$(X,24)*256)
9950 NEXT X

9960 FOR X=1 TO 5
9970 LET ZX=CODE F$(X,24)*256
9980 LET ZX=ZX+CODE F$(X,25)
9990 PRINT X,ZX
9995 NEXT X

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