4-1
CHAPTER 4
PROCEDURE CALLS, INTERRUPTS, AND
EXCEPTIONS
This chapter describes the facilities in the Intel Architecture (IA) for executing calls to proce-
dures or subroutines. It also describes how interrupts and exceptions are handled from the
perspective of an application programmer.
4.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 processors facilities for handling interrupts and exceptions are similar to those used by the
CALL and RET instructions.
4.2.STACK
The stack (refer to Figure 4-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 gigabytes long, the maximum size of a segment.
The next available memory location on the stack is called the top of stack. At any given time,
the stack pointer (contained in the ESP register) gives the address (that is the offset from the base
of the SS segment) of the top of the stack.
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. When a system sets up