4-19
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
The lexical nesting level determines the number of stack frame pointers to copy into the new
stack frame from the preceding frame. A stack frame pointer is a doubleword used to access the
variables of a procedure. The set of stack frame pointers used by a procedure to access the
variables of other procedures is called the display. The first doubleword in the display is a
pointer to the previous stack frame. This pointer is used by a LEAVE instruction to undo the
effect of an ENTER instruction by discarding the current stack frame.
After the ENTER instruction creates the display for a procedure, it allocates the dynamic local
variables for the procedure by decrementing the contents of the ESP register by the number of
bytes specified in the first parameter. This new value in the ESP register serves as the initial top-
of-stack for all PUSH and POP operations within the procedure.
To allow a procedure to address its display, the ENTER instruction leaves the EBP register
pointing to the first doubleword in the display. Because stacks grow down, this is actually the
doubleword with the highest address in the display. Data manipulation instructions that specify
the EBP register as a base register automatically address locations within the stack segment
instead of the data segment.
The ENTER instruction can be used in two ways: nested and non-nested. If the lexical level is
0, the non-nested form is used. The non-nested form pushes the contents of the EBP register on
the stack, copies the contents of the ESP register into the EBP register, and subtracts the first
operand from the contents of the ESP register to allocate dynamic storage. The non-nested form
differs from the nested form in that no stack frame pointers are copied. The nested form of the
ENTER instruction occurs when the second parameter (lexical level) is not zero.
The following pseudo code shows the formal definition of the ENTER instruction. STORAGE
is the number of bytes of dynamic storage to allocate for local variables, and LEVEL is the
lexical nesting level.
PUSH EBP;
FRAME_PTR
<
ESP;
IF LEVEL > 0
THEN
DO (LEVEL
?
1) times
EBP
<
EBP
?
4;
PUSH Pointer(EBP); (* doubleword pointed to by EBP *)
OD;
PUSH FRAME_PTR;
FI;
EBP
<
FRAME_PTR;
ESP
<
ESP
?
STORAGE;
The main procedure (in which all other procedures are nested) operates at the highest lexical
level, level 1. The first procedure it calls operates at the next deeper lexical level, level 2. A level
2 procedure can access the variables of the main program, which are at fixed locations specified
by the compiler. In the case of level 1, the ENTER instruction allocates only the requested
dynamic storage on the stack because there is no previous display to copy.
A procedure which calls another procedure at a lower lexical level gives the called procedure
access to the variables of the caller. The ENTER instruction provides this access by placing a
pointer to the calling procedures stack frame in the display.