Enhanced BASIC internals by Lee Davison
[Back]
Floating point numbers are stored in memory in four bytes. The format of the numbers is as follows.
Exponent | S | Mantissa 1 | Mantissa 2 | Mantissa 3 |
ExponentInteger numbers.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.SSign bit. This bit (b7 of mantissa 1) is one if the number is negative.Mantissa 1/2/3This 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.Values represented in this way range between + and - 1.70141173x10^38
$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
Integer numbers are stored in memory in four bytes. They are stored as twos' complement longwords with the most significant byte first.
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.. and finally ..
line number longword
code byte(s)
$00 End of line marker byte
$00 Optional pad byte (only if this address is odd)$00000000 End of program marker longwordIf there is no program in memory only the start and end markers are present.
After the program come the function references, all ten bytes long, which are stored as ..String placement in memory.1st chr of function name (+$80 for FN name)Then comes ..
2nd chr of function name (+$80 if string function)
3rd chr of function name (+$80 if integer function)
4th chr of function nameLongword BASIC execute pointerAfter The function references come the numeric variables which are stored as ..
Longword function variable name1st chr of variable nameThen comes ..
2nd chr of variable name
3rd chr of variable name (+$80 if integer variable)
4th chr of variable nameAfter the numeric variables come the strings, which are stored as ..
Floating Integer Exponent Most significant byte Sign (bit 7) + first mantissa byte Next most significant byte Second mantissa byte Next most significant byte Third mantissa byte Least significant byte
1st chr of variable nameThen comes ..
2nd chr of variable name (+$80 for string variable)
3rd chr of variable name
4th chr of variable nameString pointer longword
String length word (0 to 65535)After the string variables come the arrays, which are stored as ..
1st chr of variable name.. and then each element ..
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)
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.)
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.
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)
GOSUB and DO both push on the stack ..BASIC execute pointer longwordFOR pushes on the stack ..
current line number longword
command token word (TK_GOSUB or TK_DO)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)