|
| Previous: 4.1 The Architecture | TOC | Index | Back | Next: 4.3 Instance Contexts |
A procedure should always begin with a HDR instruction, which saves the caller's context and creates a new stack frame. It should end with an END instruction, which restores the saved stack frame and jumps back to the caller. A procedure call
P(a, b, c)
where a, b and c be global variables, is coded as follows:
LDG a LDG b LDG c CALL LP CLEAN 3
Each LDG instruction loads the value of a global variable onto the stack. CALL performs the procedure call which returns with its result in the Return Register (RR). LP denotes the label tagging the procedure P. The final CLEAN instruction removes the three arguments from the stack and replaces them with the value returned in RR so that the top of the stack finally holds the procedure return value.
Each procedure may expect the following stack configuration when called:
| FP+M | Argument #1 |
| FP+3 | Argument #N-1 |
| FP+2 | Argument #N |
| FP+1 | Return Address (saved by CALL or CALR) |
| FP+0 | Old SP (saved by HDR) |
| FP-1 | Local Variable #1 |
| FP-2 | Local Variable #2 |
| FP-J | Local Variable #K |
| SP | ( Free memory below ) |
Note: The arguments are passed to the procedure in reverse order with the first argument at the highest address.
Both arguments and local variables may be accessed using LDL instructions. Given the above context,
LDL -M
would access the first argument.
LDL -2
always loads the value of the last argument, if any. Local storage is accessed using positive offets:
NUM 25 SAVL 2
would load the second local variable with the value 25.
| Previous: 4.1 The Architecture | TOC | Index | Back | Next: 4.3 Instance Contexts |