[Return to top]

Enhanced BASIC internals by Lee Davison
[Back]



Floating point numbers.
Floating point numbers are stored in memory in four bytes. The format of the numbers is as follows.
ExponentSMantissa 1Mantissa 2Mantissa 3
Exponent
This is the power of two to which the mantissa is to be raised. This number is biased to +$80 i.e. 2^0 is represented by $80, 2^1 by $81 etc. Zero is a special case and is used to represent the value zero for the whole of the number.
S
Sign bit. This bit (b7 of mantissa 1) is one if the number is negative.
Mantissa 1/2/3
This is the 24 bit mantissa of the number and is normalised to make the highest bit (b7 of mantissa 1) always one. So the absolute value of the mantissa varies between 0.5 and 0.9999999403954 . As we know that the highest bit is always one it is replaced by the sign bit in memory.

Example.

$82,$49,$0F,$DB = +3.14159274	nearest floating equivalent to pi
 |   ||  |   |  
 |   |\--+---+- = 0.785398185	absolute value of mantissa  
 |   |
 |   \--------- = +		b7 of mantissa 1 is zero
 |
 \------------- = x 2^2 = 4	mantissa to be multiplied by 4
Values represented in this way range between + and - 1.70141173x10^38
Integer numbers.
Integer numbers are stored in memory in four bytes. They are stored as twos' complement longwords with the most significant byte first.

BASIC program memory use.
A BASIC program is stored in memory from Ram_base upwards. It's format is ..
$0000 Start of program marker word
.. then each BASIC program line which is stored as ..
start of next line pointer lowngword
line number longword
code byte(s)
$00 End of line marker byte
$00 Optional pad byte (only if this address is odd)
.. and finally ..
$00000000 End of program marker longword
If there is no program in memory only the start and end markers are present.

BASIC functions and variables memory use.
After the program come the function references, all ten bytes long, which are stored as ..
1st chr of function name (+$80 for FN name)
2nd chr of function name (+$80 if string function)
3rd chr of function name (+$80 if integer function)
4th chr of function name
Then comes ..
Longword BASIC execute pointer
Longword function variable name
After The function references come the numeric variables which are stored as ..
1st chr of variable name
2nd chr of variable name
3rd chr of variable name (+$80 if integer variable)
4th chr of variable name
Then comes ..
Floating Integer
ExponentMost significant byte
Sign (bit 7) + first mantissa byteNext most significant byte
Second mantissa byteNext most significant byte
Third mantissa byteLeast significant byte

After the numeric variables come the strings, which are stored as ..
1st chr of variable name
2nd chr of variable name (+$80 for string variable)
3rd chr of variable name
4th chr of variable name
Then comes ..
String pointer longword
String length word (0 to 65535)

After the string variables come the arrays, which are stored as ..

1st chr of variable name
2nd chr of variable name (+$80 for string variable)
3rd chr of variable name (+$80 for integer variable)
4th chr of variable name
array size in bytes longword (size includes this header)
number of dimensions word
[dimension 3 size word] (lowest element is zero)
[dimension 2 size word] (lowest element is zero)
dimension 1 size word (lowest element is zero)
.. and then each element ..

Floating
Integer
String
Longword float Longword integer String Pointer longword
(as for variables) (as for variables) String length word

The elements of every array are stored in the order ..

index1 [0-n], index2 [0-n], index3 [0-n]
i.e. element (1,2,3) in an array of (3,4,5) would be the ..
1 + 1 + 2*(3+1) + 3*(3+1)*(4+1) = 70th element
(As array dimensions range from 0 to n element n will always be the (n+1)th element in memory.)


String placement in memory.
Strings are generally stored from the top of available RAM, Ram_top, working down, however if the interpreter encounters a line such as ..
	100 A$ = "This is a string"
.. then the pointer in the A$ descriptor will point to the string in program memory and will not make a copy of the string in the string memory.

String descriptors in BASIC.
A string descriptor is a six byte table that describes a string, it is of the format ..
base = String pointer longword
base+4 = String length word (0 to 65535)

Stack use in BASIC.
GOSUB and DO both push on the stack ..
BASIC execute pointer longword
current line number longword
command token word (TK_GOSUB or TK_DO)
FOR pushes on the stack ..
BASIC execute pointer longword
FOR line number longword
FOR variable data type word
TO value mantissa longword
TO value exponent & sign word
STEP value mantissa longword
STEP value exponent & sign word
FOR variable pointer longword
token for FOR word (TK_FOR)

e-mail me [e-mail]
Last page update: 1st March, 2003.