EhBASIC Internals

Enhanced BASIC internals

Floating point numbers.
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

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.70141173×10^38

BASIC program memory use.

A BASIC program is stored in memory from Ram_base upwards. It’s format is ..
$00 Start of program marker byte
.. then each BASIC program line which is stored as ..
start of next line pointer low byte
start of next line pointer high byte
line number low byte
line number high byte
code byte(s)
$00 End of line marker byte
.. and finally ..
$00 End of program marker byte 1
$00 End of program marker byte 2
If there is no program in memory only the start and end marker bytes are present.

BASIC variables memory use.
After the program come the variables and function references, all six bytes
long, which are stored as ..
1st character of variable or function name (+$80 if FN name)
2nd character of variable or function name (+$80 if string)
.. then for each type ..

Numeric
String
Function
Exponent String length BASIC execute pointer low byte
Sign (bit 7) + mantissa 1 String pointer low byte BASIC execute pointer high byte
Mantissa 2 String pointer high byte Function variable name 1st character
Mantissa 3 $00 Function variable name 2nd character

After the variables come the arrays, which are stored as ..
1st character of variable name
2nd character of variable name (+$80 if string)
array size in bytes low byte (size includes this header)
array size in bytes high byte
number of dimensions
[dimension 3 size high byte] (lowest element is zero)
[dimension 3 size low byte]
[dimension 2 size high byte] (lowest element is zero)
[dimension 2 size low byte]
dimension 1 size high byte (lowest element is zero)
dimension 1 size low byte
.. and then each element ..

Numeric
String
Exponent String length
Sign (bit 7) + mantissa 1 String pointer low byte
Mantissa 2 String pointer high byte
Mantissa 3 $00

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 high/low 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 three byte table that describes a string, it is of the format ..
base = string length
base+1 = string pointer low byte
base+2 = string pointer high byte

Stack use in BASIC.

GOSUB and DO both push on the stack ..
BASIC execute pointer high byte
BASIC execute pointer low byte
current line high byte
current line low byte
command token (TK_GOSUB or TK_DO)

FOR pushes on the stack ..
BASIC execute pointer low byte
BASIC execute pointer high byte
FOR line high byte
FOR line low byte
TO value mantissa3
TO value mantissa2
TO value mantissa1
TO value exponent
STEP sign
STEP value mantissa3
STEP value mantissa2
STEP value mantissa1
STEP value exponent
var pointer for FOR/NEXT high byte
var pointer for FOR/NEXT low byte
token for FOR (TK_FOR)