Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture

Vol. 1 6-1
CHAPTER 6
PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS
This chapter describes the facilities in the Intel 64 and IA-32 architectures for
executing calls to procedures or subroutines. It also describes how interrupts and
exceptions are handled from the perspective of an application programmer.
6.1 PROCEDURE CALL TYPES
The processor supports procedure calls in the following two different ways:
CALL and RET instructions.
ENTER and LEAVE instructions, in conjunction with the CALL and RET
instructions.
Both of these procedure call mechanisms use the procedure stack, commonly
referred to simply as “the stack,” to save the state of the calling procedure, pass
parameters to the called procedure, and store local variables for the currently
executing procedure.
The processor’s facilities for handling interrupts and exceptions are similar to those
used by the CALL and RET instructions.
6.2 STACKS
The stack (see Figure 6-1) is a contiguous array of memory locations. It is contained
in a segment and identified by the segment selector in the SS register. When using
the flat memory model, the stack can be located anywhere in the linear address
space for the program. A stack can be up to 4 GBytes long, the maximum size of a
segment.
Items are placed on the stack using the PUSH instruction and removed from the
stack using the POP instruction. When an item is pushed onto the stack, the
processor decrements the ESP register, then writes the item at the new top of stack.
When an item is popped off the stack, the processor reads the item from the top of
stack, then increments the ESP register. In this manner, the stack grows down in
memory (towards lesser addresses) when items are pushed on the stack and shrinks
up (towards greater addresses) when the items are popped from the stack.
A program or operating system/executive can set up many stacks. For example, in
multitasking systems, each task can be given its own stack. The number of stacks in
a system is limited by the maximum number of segments and the available physical
memory.