Inside Sinclair Issue 1 Contents Books

helpline




Andrew Hewson

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

Questions on how to organise memory

HELLO and welcome to HELPLINE. Each month I shall answer a selection of letters from my postbag on a theme of interest to Sinclair users. Often I shall refer to pages in the manual supplied with the ZX machines and this month the questions centre on chapters 27 and 28 of the ZX-81 manual which are about the Organisation of Memory and the System Variables. The first request, from Mr Adler is:

"Please explain the meaning of an "address". How can a byte, which is a number, have an address?"

An important part of a computer is its memory and typical microcomputers have several thousand memory locations available for immediate use. Clearly each location needs a separate label or address to distinguish it from its fellows. The word address came into use because writing information to one of many memory locations is similar to writing a letter to one of many people. Letters are sent to an address so that they reach the person who lives there. Similarly, a computer sends information to the memory location at a given address.

Computer addresses are simply whole numbers starting at zero; so, for example, in the unexpanded ZX-81 locations 0 to 8191 are used by the ROM, locations 8192 to 16383 are unused, and locations 16384 to 17407 are used by the RAM. The add-on 16K RAM uses locations 16384 to 32767. Only the contents of RAM may be altered and so users are generally interested in addresses 16384 and upwards.

Each location in memory contains one byte of information. A byte can be thought of as a whole number between 0 and 255 inclusive. In practice, the a word byte is often also used to mean a "location in memory" as well as to mean the number which is stored at that location. Thus if location 17000 contains 34, we might say "byte 17000 is 34".

The next question, from Mr Lypartin develops our theme:

"I am keen to understand how my ZX-81 works but as a beginner I am perplexed by the manner in which addresses are stored in the system variables. I know, for example, that D-FILE is the beginning of the display file - but how is that information stored?"

The area at the bottom of RAM between 16384 and 16508 holds the system variables and is followed by the program area, starting at 16509. The display file is next but as programs can vary in length, the display file does not start at a fixed address. The ZX-81 keeps track of it by storing the current value of the starting address in D-FILE.

If you look at page 178 of the manual you will see that the value of D-FILE is stored at address 16396 and so you might infer that you have only to look at the contents of 16396 to find the value of D-FILE.

Unfortunately, that is not true. Remember that the value of D-FILE is an address and that addresses are whole numbers, like 16384 and 17407 and 32767. A single location can only hold a number between 0 and 255 and so two adjacent locations are used to store larger numbers. The value of D-FILE is given by:

value held in 16396+256*value held in 16397

Any whole number between 0 and 65535 inclusive can be stored using this system.

The value held at an address can be found by PEEKing at it and so you can PRINT the value of D-FILE by entering:

PRINT PEEK 16396+256*PEEK 16397

You may know that the contents of the first location in the display file is always 118 and you can show Mat by entering:

PRINT PEEK (PEEK 16396+256*PEEK 16397)

Finally, I have been asked many times if there is a simple way of allowing two programs to use the same variables. Mr Peters asks:

"I want to write several programs which use the same data but there seems to be no way to do so using the Sinclair functions. Do I have to write routines to save and load data on cassette or is there some way of passing data between programs directly?"

There is. The trick is to alter the RAMTOP system variable to give you some space at the top of RAM which is out of reach of the Sinclair system in normal use. Your first program can then copy data into the area above RAMTOP. You can then load a second program, replacing the first, but the data saved above RAMTOP will still be intact. The second program can then copy the data back into its own variables area.

Let us take the job step by step. All the calculations following refer to the unexpanded ZX-81 with values for the 16K expansion in square brackets where they are different.

In each case, lines 50 to 90 represent the essential part of the program but it is important to assign the array at the beginning of the program, so that it lies at the bottom of the variables area. The same technique works for ordinary variables but strings cannot, in general, be copied in this manner, because they can move around in RAM.



Inside Sinclair Issue 1 Contents Books

Sinclair User
April 1982