
Chapter 3: General-Purpose Programming
53
24593—Rev. 3.09—September 2003
AMD 64-Bit Technology
The rBP register is used as a
stack frame pointer—
a base address
of the stack area used for parameters passed to subroutines and
local variables. Positive offsets of the stack frame pointed to by
rBP provide access to parameters passed while negative offsets
give access to local variables. This technique allows creating re-
entrant subroutines.
The ENTER and LEAVE instructions provide support for
procedure calls, and are mainly used in high-level languages.
The ENTER instruction is typically the first instruction of the
procedure, and the LEAVE instruction is the last before the
RET instruction.
The ENTER instruction creates a stack frame for a procedure.
The first operand,
size
, specifies the number of bytes allocated
in the stack. The second operand,
depth
, specifies the number of
stack-frame pointers copied from the calling procedure’s stack
(i.e., the nesting level). The depth should be an integer in the
range 0–31.
Typically, when a procedure is called, the stack contains the
following four components:
Parameters passed to the called procedure (created by the
calling procedure).
Return address (created by the CALL instruction).
Array of stack-frame pointers (pointers to stack frames of
procedures with smaller nesting-level depth) which are used
to access the local variables of such procedures.
Local variables used by the called procedure.
All these data are called the
stack frame
. The ENTER
instruction simplifies management of the last two components
of a stack frame. First, the current value of the rBP register is
pushed onto the stack. The value of the rSP register at that
moment is a
frame pointer
for the current procedure: positive
offsets from this pointer give access to the parameters passed to
the procedure, and negative offsets give access to the local
variables which will be allocated later. During procedure
execution, the value of the frame pointer is stored in the rBP
register, which at that moment contains a frame pointer of the
calling procedure. This frame pointer is saved in a temporary
register. If the depth operand is greater than one, the array of
depth-1
frame pointers of procedures with smaller nesting level
is pushed onto the stack. This array is copied from the stack