• 沒有找到結果。

Procedure Calls, Interrupts, and

CHAPTER 6 PROCEDURE CALLS, INTERRUPTS, AND

6.5 PROCEDURE CALLS FOR BLOCK-STRUCTURED LANGUAGES

6.5.1 ENTER Instruction

The ENTER instruction creates a stack frame compatible with the scope rules typically used in block-structured languages. In block-structured languages, the scope of a procedure is the set of variables to which it has access. The rules for scope vary among languages. They may be based on the nesting of procedures, the division of the program into separately compiled files, or some other modularization scheme.

ENTER has two operands. The first specifies the number of bytes to be reserved on the stack for dynamic storage for the procedure being called. Dynamic storage is the memory allocated for variables created when the procedure is called, also known as automatic variables. The second parameter is the lexical nesting level (from 0 to 31) of the procedure. The nesting level is the depth of a procedure in a hierarchy of procedure calls. The lexical level is unrelated to either the protection privilege level or to the I/O privilege level of the currently running program or task.

ENTER, in the following example, allocates 2 Kbytes of dynamic storage on the stack and sets up pointers to two previous stack frames in the stack frame for this procedure.

ENTER 2048,3

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.

6-20 Vol. 1

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

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 that 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 procedure's stack frame in the display.

A procedure that calls another procedure at the same lexical level should not give access to its variables. In this case, the ENTER instruction copies only that part of the display from the calling procedure which refers to previously nested procedures operating at higher lexical levels.

The new stack frame does not include the pointer for addressing the calling procedure’s stack frame.

Vol. 1 6-21 PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

The ENTER instruction treats a re-entrant procedure as a call to a procedure at the same lexical level. In this case, each succeeding iteration of the re-entrant procedure can address only its own variables and the variables of the procedures within which it is nested. A re-entrant procedure always can address its own variables; it does not require pointers to the stack frames of previous iterations.

By copying only the stack frame pointers of procedures at higher lexical levels, the ENTER instruction makes certain that procedures access only those variables of higher lexical levels, not those at parallel lexical levels (see Figure 6-6).

Block-structured languages can use the lexical levels defined by ENTER to control access to the variables of nested procedures. In Figure 6-6, for example, if procedure A calls procedure B which, in turn, calls procedure C, then procedure C will have access to the variables of the MAIN procedure and procedure A, but not those of procedure B because they are at the same lexical level. The following definition describes the access to variables for the nested procedures in Figure 6-6.

1. MAIN has variables at fixed locations.

2. Procedure A can access only the variables of MAIN.

3. Procedure B can access only the variables of procedure A and MAIN. Procedure B cannot access the variables of procedure C or procedure D.

4. Procedure C can access only the variables of procedure A and MAIN. Procedure C cannot access the variables of procedure B or procedure D.

5. Procedure D can access the variables of procedure C, procedure A, and MAIN. Procedure D cannot access the variables of procedure B.

Figure 6-6. Nested Procedures Main (Lexical Level 1) Procedure A (Lexical Level 2) Procedure B (Lexical Level 3)

Procedure C (Lexical Level 3) Procedure D (Lexical Level 4)

6-22 Vol. 1

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

In Figure 6-7, an ENTER instruction at the beginning of the MAIN procedure creates three doublewords of dynamic storage for MAIN, but copies no pointers from other stack frames. The first doubleword in the display holds a copy of the last value in the EBP register before the ENTER instruction was executed. The second doubleword holds a copy of the contents of the EBP register following the ENTER instruction. After the instruction is executed, the EBP register points to the first doubleword pushed on the stack, and the ESP register points to the last doubleword in the stack frame.

When MAIN calls procedure A, the ENTER instruction creates a new display (see Figure 6-8).

The first doubleword is the last value held in MAIN's EBP register. The second doubleword is a pointer to MAIN's stack frame which is copied from the second doubleword in MAIN's display. This happens to be another copy of the last value held in MAIN’s EBP register. Proce-dure A can access variables in MAIN because MAIN is at level 1.

Therefore the base address for the dynamic storage used in MAIN is the current address in the EBP register, plus four bytes to account for the saved contents of MAIN’s EBP register. All dynamic variables for MAIN are at fixed, positive offsets from this value.

Figure 6-7. Stack Frame After Entering the MAIN Procedure EBP

Display Old EBP

ESP Main’s EBP

Dynamic Storage

Vol. 1 6-23 PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

When procedure A calls procedure B, the ENTER instruction creates a new display (see Figure 6-9). The first doubleword holds a copy of the last value in procedure A’s EBP register.

The second and third doublewords are copies of the two stack frame pointers in procedure A’s display. Procedure B can access variables in procedure A and MAIN by using the stack frame pointers in its display.

When procedure B calls procedure C, the ENTER instruction creates a new display for proce-dure C (see Figure 6-10). The first doubleword holds a copy of the last value in proceproce-dure B’s EBP register. This is used by the LEAVE instruction to restore procedure B’s stack frame. The second and third doublewords are copies of the two stack frame pointers in procedure A’s display. If procedure C were at the next deeper lexical level from procedure B, a fourth double-word would be copied, which would be the stack frame pointer to procedure B’s local variables.

Note that procedure B and procedure C are at the same level, so procedure C is not intended to access procedure B’s variables. This does not mean that procedure C is completely isolated from procedure B; procedure C is called by procedure B, so the pointer to the returning stack frame is a pointer to procedure B’s stack frame. In addition, procedure B can pass parameters to proce-dure C either on the stack or through variables global to both proceproce-dures (that is, variables in the scope of both procedures).

Figure 6-8. Stack Frame After Entering Procedure A Display EBP

Old EBP

ESP Main’s EBP

Dynamic Storage

Procedure A’s EBP Main’s EBP Main’s EBP

6-24 Vol. 1

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

Figure 6-9. Stack Frame After Entering Procedure B EBP Display

Old EBP

ESP Main’s EBP

Dynamic Storage

Procedure A’s EBP Main’s EBP Main’s EBP

Procedure A’s EBP

Procedure B’s EBP Main’s EBP Procedure A’s EBP

Vol. 1 6-25 PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

Figure 6-10. Stack Frame After Entering Procedure C EBP Display

Old EBP

ESP Main’s EBP

Dynamic Storage

Procedure A’s EBP Main’s EBP Main’s EBP

Procedure A’s EBP

Procedure B’s EBP Main’s EBP Procedure A’s EBP

Procedure B’s EBP

Procedure C’s EBP Main’s EBP Procedure A’s EBP

6-26 Vol. 1

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS