Up one Internals. By Lee Davison. Up to top

BASIC program memory use.

A BASIC program is stored in memory as follows ..

The start of memory is held in Smeml which is at RAMBASE+$2E. Each BASIC program line is stored as a longword pointer to the next line, a longword line number, the tokenised line text and an end of line byte. Optionally, if the next byte is at an odd address, there is a null pad byte.

This repeats for each line until the pointer after the last line which is null, this is the end of program marker.

If there is no program in memory only the end marker will be present.

BASIC functions, variables and arrays.

After the program comes the functions, variables and arrays. Each has a name of up to four characters in length and may have b7 of the second character set if the type is string, and b7 of the third character set if the type is integer. Characters that are not defined are left as zero.

Directly following the name is the data, this is as follows ..

Functions
Function name Execute pointer Variable name

A function is twelve bytes in length made up of its name, a longword pointer to the BASIC code of the function and the name of the function variable. The type of function variable is independant of the type of function so a string function can have a numeric variable and vice versa.

Variables

Numeric variables take eight bytes, four for the name and four for the floating point or integer value.

Or
Variable name Floating point value
 
Variable name Integer value

The floating point value is stored as an exponent byte, biased to +$80, and a left justified twenty four bit mantissa. As the highest bit of the mantissa is always one this bit is replaced by a bit representing the sign. If this bit is 1 then the value is negative.

ExponentS23 Mantissa bits

Example.

Zero is represented by the exponent being zero.

Integer values are stored big endian with the most significant byte first.

MS ByteByte ByteLS Byte

String variables take ten bytes, four for the name, four for the string pointer and two for the string length. A string variable also needs enough bytes to store the string, this rounded up to the next even length if the string length was odd but the recorded length is the actual length of the string.

String name String pointer Length

Arrays

Array nameArray size DimensionsDim max Dim maxDim max

The array size is a longword and is the total length of the array including the header. Dimensions is a word count of the number of dimensions in the array and Dim max is the word index of the highest element number in that dimension. The dimension maxima are stored in reverse order with the last dimension being stored first.

After the header come the array elements, four bytes each for the elements of numeric arrays and six bytes each for the elements of string arrays, stored in index order. So the first element is 0,0,0 then 1,0,0 then 2,0,0 and on until n,0,0 where n=maximum, and then 0,1,0 and 1,1,0 and 2,1,0 and so on.


Last page update: 17th February, 2005. e-mail me e-mail