27. Organization of memory

The memory is sorted into different areas for storing different kinds of information. The areas are only large enough for the information that they actually contain, and if you insert some more at a given point (for instance by adding a program line or variable) space is made by shifting up everything above that point. Conversely, if you delete information then everything above it is shifted down.

16384 System variables
16509 Program
D_FILE Display File
VARS Variables
  Byte containing 80h
E_LINE Line being typed + Workspace
STKBOT Calculator stack
STKEND Spare
Stack Pointer Machine stack
ERR_SP GOSUB stack
RAMTOP USR routines

The system variables contain various pieces of information that tell the computer what sort of state the computer is in. They are listed fully in the next chapter, but for the moment note that there are some (called D_FILE, VARS, E_LINE and so on) that contain the addresses of the boundaries between the various areas in the memory. These are not BASIC variables, and their names will not be recognized by the computer.

Each line in the program is stored as:

Most Significant byte Line number
Less Significant byte
Most Significant byte Length of text plus NEWLINE
Less Significant byte
  Text
  NEWLINE byte (1110110)

Note that, in contrast with all other cases of two-byte numbers in the Z80, the line number here (and also in a FOR-NEXT control variable) is stored with its most significant byte first: that is to say, in the order that you would write them down in.

A numerical constant in the program is followed by its binary form, using the character CHR$ 126 followed by five bytes for the number itself.

The display file is the memory copy of the television picture. It begins with a NEWLINE character, and then has the twenty four lines of text, each finishing with a NEWLINE. The system is so designed that a line of text does not need space a full thirty two characters: final spaces can be omitted. This is used to save space when the memory is small.

When the total amount of memory (according to the system variable RAMTOP) is less than 3 1/4 K, then a clear screen - as set up at the start or by CLS - consists of just twenty five NEWLINEs. When the memory is bigger than a clear screen is padded out with 24*32 spaces and on the whole it stays at its full size; SCROLL, however, and certain conditions where the lower part of the screen expands to more than two lines, can upset this by introducing short lines at the bottom.

The variables have different formats according to their different natures.

Number whose name is one letter only:

0 1 1 N N N N N N Letter Name
E E E E E E E E E Exponent Byte Value
S M M M M M M M M S=Sign bit
M M M M M M M M M M=Mantissa
M M M M M M M M M  
M M M M M M M M M  

Number whose name is longer than one letter:

1 0 1 N N N N N N 1st letter Name
0 0 N N N N N N N 2nd letter
1 0 N N N N N N N Last letter
  5 bytes Value

Array of numbers:

1 0 0 N N N N N N Letter - 20 hex Name
L L L L L L L L L 1 byte Total length of data below
1 0 D D D D D D D 1 byte Number of dimensions
  2 bytes 1st Dimension
~ ~ ~ ~ ~ ~ ~ ~ ~    
  2 bytes Last Dimension
  5 bytes each Elements

The order of the elements is:

first, the elements for which the first subscript is 1
next, the elements for which the first subscript is 2
next, the elements for which the first subscript is 3
and so on for all possible values of the first subscript.

The elements with a given first subscript are ordered in the same way using the second subscript, and so on down to the last.

As an example, the elements of the 3 x 6 array B in chapter 22 are stored in the order
B(1,1),B(1,2),B(1,3),B(1,4),B(1,5),B(1,6),B(2,1),B(2,2),...,B(2,6),B(3,1),B(3,2),...,B(3,6).

Control variable of a FOR-NEXT loop:

1 1 1 N N N N N N Letter Name
  5 bytes Value
  5 bytes Limit
  5 bytes Step
  2 bytes Looping line

String:

0 1 0 N N N N N N Letter - 20 hex Name
L L L L L L L L L
L L L L L L L L L
2 bytes Number of characters in string
  L bytes Text of string (may be empty)

Array of characters:

1 1 0 N N N N N N Letter - 20 hex Name
L L L L L L L L L
L L L L L L L L L
2 bytes Total length of data below
D D D D D D D D D 1 byte Number of dimensions
  2 bytes 1st dimension
~ ~ ~ ~ ~ ~ ~ ~ ~    
  2 bytes Last dimension
  1 byte each Elements

The part string at E_LINE contains the line being typed (as a command, a program line, or INPUT data) & also some work space.

The calculator is the part of the BASIC system that deals with arithmetic, and the numbers on which it is operating are held mostly in the calculator stack.

The spare part contains the space so far unused.
The machine stack is the stack used by the Z80 chip to hold return addresses and so on.
The GOSUB stack was mentioned in chapter 14.
The space for USR routines has to be set aside by you, using NEW as described in the last chapter.