# 80-Bus News

## May–June 1983, Volume 2, Issue 3

The workings of NAS­COM ROM BASIC Ver 4.7

PAGE 8

******** How variables and arrays are stored ********

Variables such as AB AB\$ and FN AB are all stored in the simple variable area of memory. The start address of this area is held in PROGND and the end address is held in VAREND.

If AB=10 and AB\$="TEXT” and FN AB(XY) had been defined then the memory would look like this

 42 41 Name of AB in reverse (42 41 = “B” “A") 00 00 20 84 Floating point value for 10
 C2 41 Name for AB\$ (C2 is “B” with bit 7 set) 04 Length of string (4 characters) ?? This byte unused for string LL HH Address where “TEXT” is to be found
 42 C1 Name of FN AB (C1 is “A” with bit 7 set) LL HH Address of function (After “=") 59 58 Argument name in reverse (59 58 = “Y” “X")

Arrays such as AB(1,3) and AB\$(3,1) are stored in the array area of memory. The start address of this area is held in VAREND and the end address is held in ARREND.

If DIM AB(1,3),​AB\$(3,1) had been entered then the memory would look like this:–

 42 41 Name of array AB in reverse 25 00 Bytes used for array (0025 = 37) 02 2 dimensions 04 00 Size of second dimension including zero element 02 00 Size of first dimension including zero element 00 00 00 00 AB(0,0) 00 00 00 00 AB(1,0) 00 00 00 00 AB(0,1) 00 00 00 00 AB(1,1) 00 00 00 00 AB(0,2) 00 00 00 00 AB(1,2) 00 00 00 00 AB(0,3) 00 00 00 00 AB(1,3)
 C2 41 Name of array AB\$ in reverse 25 00 Bytes used for array (0025 = 37) 02 2 dimensions 02 00 Size of second dimension including zero element 04 00 Size of first dimension including zero element 00 00 00 00 AB\$(0,0) 00 00 00 00 AB\$(1,0) 00 00 00 00 AB\$(2,0) 00 00 00 00 AB\$(3,0) 00 00 00 00 AB\$(0,1) 00 00 00 00 AB\$(1,1) 00 00 00 00 AB\$(2,1) 00 00 00 00 AB\$(3,1)

The workings of NAS­COM ROM BASIC Ver 4.7

PAGE 9

******** Usage of the stack for GOSUB/​RETURN and FOR/​NEXT ********

#### GOSUB and RETURN usage of the stack

When a GOSUB is executed the address of where to RETURN to and the number of the line to RETURN to are PUSHed on the stack as follows:–

This GOSUB block remains on the stack until a RETURN is executed at which point BASIC looks back through the stack until it finds a GOSUB block and then sets the stack there and then POPs the line number and the address of the statement after the GOSUB and continues execution.

The fact that the stack is set to this GOSUB block kills all active FOR loops which were set up inside the subroutine.

#### FOR and NEXT usage of the stack

When a FOR is executed the address of the first statement in the loop, the line number of the loop statement, the TO value, the STEP value and the sign of the STEP are all PUSHed onto the stack as follows:–

 HIGH MEMORY: XX XX Address of first statement in loop XX XX Line number of loop statement XX XX XX XX TO value in floating point XX XX XX XX STEP value in floating point XX Sign of STEP XX XX Address of index variable STACK POINTER: 81 FOR token as marker

This FOR block remains on the stack until a matching NEXT is executed. When next is executed BASIC looks back through the stack to find the matching FOR block. If it is found then the STEP value is added to the value of the index variable and the result is compared with the TO value. With the use of the TO value and the sign of the step BASIC knows if the loop has been completed or not. If it has not been completed then the FOR block remains on the stack until the loop has been completed. The stack is set to point to this FOR block and effectively kills all FORs nested within this loop. When the loop is completed the FOR block is removed from the stack and execution continues from after the NEXT instruction.

If the FOR block cannot be found then a “?NF Error” occurs.

NASCOM ROM BASIC source code is available in ASM and LST file format.