A.5.65 ENTER: Create Stack Frame
ENTER imm,imm ; C8 iw ib [186]
ENTER constructs a stack frame for a high-level language procedure call.
The first operand (the "iw" in the opcode definition above refers to the
first operand) gives the amount of stack space to allocate for local
variables; the second (the "ib" above) gives the nesting level of the
procedure (for languages like Pascal, with nested procedures).
The function of ENTER, with a nesting level of zero, is equivalent to
PUSH EBP ; or PUSH BP in 16 bits
MOV EBP, ESP ; or MOV BP, SP in 16 bits
LEA ESP, [ESP - op1] ; or LEA SP, [BP - op1] in 16 bits
This creates a stack frame with the procedure parameters accessible
upwards from EBP, and local variables accessible downwards from EBP.
Note that the flags are not modified by the calculation.
With a nesting level of one, the stack frame created is 4 (or 2) bytes
bigger, and the value of the final frame pointer EBP is accessible in
memory at [EBP-4].
This allows ENTER, when called with a nesting level of two, to look at
the stack frame described by the _previous_ value of EBP, find the frame
pointer at offset -4 from that, and push it along with its new frame
pointer, so that when a level-two procedure is called from within a
level-one procedure, [EBP-4] holds the frame pointer of the most recent
level-one procedure call and [EBP-8] holds that of the most recent
level-two call. And so on, for nesting levels up to 31. The nesting
level is determined by bitwise AND-masking the second operand with 31.
Stack frames created by ENTER can be destroyed by the LEAVE instruction:
see section A.5.136.