Mind Games Issue 3 Contents Issue 4

helpline




Andrew Hewson

Making the best use of memory

In this column Andrew Hewson, author of Hints & Tips for the ZX-80 and Hints & Tips for the ZX-81, answers your questions on hardware and software for Sinclair computers.

THERE is a great deal of interest in the various ZX-81 RAM packs on the market and this month's column is devoted to questions about the efficient use of memory to enable you to decide which RAM would suit you best. I also answer some questions about the RAMS. Susan James writes:

I have a 1K ZX-81 and I keep running out of memory. Is there an easy way to work out how much space is required by a program? Have you any tips about how to shorten programs without altering their function?

The answer to the first question is no. The amount of space used to hold the program can be found by entering:

PRINT PEEK 16396+256*PEEK 16397-16509

That line calculates the address of the end of the program area - held in D FILE at 16396 and 16397 - and subtracts the address of the beginning of the program area - i.e., 16509 - but more space is required when the program is running and to hold the display file.

In the ZX-81, the display file occupies 25 bytes plus one byte for each character on the screen including leading blanks generated by PRINT AT, TAB and commas in PRINT statements. Space is also needed during execution to hold the variables area, the calculator stack and the machine and GO SUB stacks.

The amount of spare space at any time is the difference between the address held in the stack pointer (SP) and the address held in STKEND - at 16412. Unfortunately SP can be obtained only using a machine code routine.

In any case, the use of memory changes dynamically during execution and so the amount of spare space varies, too; hence my advice is that if you want to know how much space your program alone uses, then have a look at D FILE by entering:

PRINT PEEK 16396+256*PRINT PEEK 16397

The bigger your program, the bigger the result. Alternatively, for an idea of how much space has been used after a program has RUN, have a look at STKEND by entering:

PRINT PEEK 16412+256*PRINT PEEK 16413
8K-16K-48K RAM

The difference between the result and RAMTOP - 17408 on the 1K ZX-81, 32768 with the 16K RAM - reflects the amount of spare space but allows about 100 bytes for the stack.

There are many tricks you can use to shorten a program without changing its function and to use them to full effect you need to understand the code used to store Basic program lines. There are three rules: Each line has an overhead of five bytes - two bytes to hold the line number, followed by two bytes to hold the length of the remainder of the line and a single byte containing decimal 118 at the end of the line.

Each character - letters, punctuation marks, graphics characters occupies one byte each, as do keywords like PRINT, LET, FOR.

Numbers are held both in character form and in numeric form. The character form uses one byte per digit and is followed by a byte containing 126 and then five bytes containing the numeric form. I explained the interpretation of the numeric form in detail last month.

For an idea of the way the rules work, plug in your ZX-81 and enter:

10 FOR A=16509 TO 16548
20 PRINT CHR PEEK A
30 NEXT A.

That program looks at the contents of the first 40 bytes of the program area, so it looks at itself.

My advice on shortening programs is:

Omit REM statements and keep PRINT statements as short as possible.

Use the code function to set variable values if possible; for example, use:

LET A=CODE "K"

rather than

LET A=48

Keep the number of variables to a minimum by re-using them for a new purpose. Loop counters in particular use a good deal of space in the variables area.

Use single letters only for variable names.

The next question concerns the variables area and is from Ken Griffiths, who asks:

What is the formula for the size of an array?

The answer to the question is on page 173 of the ZX-81 Basic Programming manual. The formula is:

4 + 2*number of dimensions + 5*total number of elements.

An array B (2, 5, 6) has three dimensions and 2*5*6 = 60 elements. Hence it requires 4+2*3+5*60 = 310 bytes.

The equivalent formulae for the other variables are:

NUMBER: 5 + one byte per character of the number

LOOP CONTROL VARIABLE: 18 bytes

STRING: 3 + one byte per string item

CHARACTER ARRAY: 4+2*number of dimensions + number of elements.

I have received a number of questions about the RAMs, from, in particular, Jason Lowe, Steve Harrop, Geoff Hewitt, and Russell Walkinson, who, incidentally, wrote the funniest letter this month.

Can any program written for a ZX-81 with or without 16K RAM be used with any of the RAMS? Yes, provided that you have sufficient RAM to accommodate the program it can be run using any of the RAMS but it is no use attempting to run a 16K program if you have, say, only a 4K RAM.

Are special programs required for RAMS larger than 16K? No; you can add up to 48K of RAM starting at address 16384 without requiring special software, although there are two problems. The first is that the ZX-81 expects 16K of RAM at most and if you have more you must reset the RAMTOP pointer from the keyboard when you plug in the power supply. To do so, enter:

POKE 16389,4*M+64-256*INT((4*M+64)/256) followed by NEW

where M is the amount of extra RAM you have. For example, if you add 32K of RAM enter:

POKE 16389,192
NEW
8K-16K-48K RAM

The second problem is that the display file must remain in the bottom 16K of RAM, as explained by Stephen Adam in Sinclair User, page 22, May 1982. Hence, unless you use special software, you are restricted to Basic programs smaller than about 15K. The remainder of the memory can be used to hold large arrays. The following line will give you an approximate idea of the size of your program:

PRINT PEEK 16397/4-16;"K".

What is the point of adding RAM between 8K and 16K?

The Sinclair ROM uses addresses 0 to 8191; 8192 to 16383 is normally unused and the 1K internal RAM and 16K and other add-on RAM normally use 16384 and upwards. Some of the memories now available provide RAM between 8192 and 16383.

The advantage of the facility is that the area is not accessible to Sinclair Basic except by using PEEK and POKE commands. So it can be used to store data without the risk of it being over-written. In particular, it is not over-written when a program is LOADed from cassette and it can be used to pass data between programs in a similar way to the routines I described for saving data above RAMTOP in the April 1982 Sinclair User.

What does paging mean? The microprocessor on which the ZX-81 is based can look only at 64K of locations at any one time. Paging is a facility for separating RAM, usually but not necessarily into 64K blocks or "pages", so that processors can switch from one "page" to another.

© Copyright Hewson Consultants 1982



Mind Games Issue 3 Contents Issue 4

Sinclair User
June 1982