Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture
Vol. 1 7-21
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
• An address specified using the standard addressing modes of the
processor — Here, the address can be a near pointer or a far pointer. If the
address is for a near pointer, the address is translated into an offset and copied
into the EIP register. If the address is for a far pointer, the address is translated
into a segment selector (which is copied into the CS register) and an offset
(which is copied into the EIP register).
In protected mode, the JMP instruction also allows jumps to a call gate, a task gate,
and a task-state segment.
Call and return instructions — The CALL (call procedure) and RET (return from
procedure) instructions allow a jump from one procedure (or subroutine) to another
and a subsequent jump back (return) to the calling procedure.
The CALL instruction transfers program control from the current (or calling proce-
dure) to another procedure (the called procedure). To allow a subsequent return to
the calling procedure, the CALL instruction saves the current contents of the EIP
register on the stack before jumping to the called procedure. The EIP register (prior
to transferring program control) contains the address of the instruction following the
CALL instruction. When this address is pushed on the stack, it is referred to as the
return instruction pointer or return address.
The address of the called procedure (the address of the first instruction in the proce-
dure being jumped to) is specified in a CALL instruction the same way as it is in a JMP
instruction (see “Jump instruction” on page 7-20). The address can be specified as a
relative address or an absolute address. If an absolute address is specified, it can be
either a near or a far pointer.
The RET instruction transfers program control from the procedure currently being
executed (the called procedure) back to the procedure that called it (the calling
procedure). Transfer of control is accomplished by copying the return instruction
pointer from the stack into the EIP register. Program execution then continues with
the instruction pointed to by the EIP register.
The RET instruction has an optional operand, the value of which is added to the
contents of the ESP register as part of the return operation. This operand allows the
stack pointer to be incremented to remove parameters from the stack that were
pushed on the stack by the calling procedure.
See Section 6.3, “Calling Procedures Using CALL and RET,” for more information on
the mechanics of making procedure calls with the CALL and RET instructions.
Return from interrupt instruction — When the processor services an interrupt, it
performs an implicit call to an interrupt-handling procedure. The IRET (return from
interrupt) instruction returns program control from an interrupt handler to the inter-
rupted procedure (that is, the procedure that was executing when the interrupt
occurred). The IRET instruction performs a similar operation to the RET instruction
(see “Call and return instructions” on page 7-21) except that it also restores the
EFLAGS register from the stack. The contents of the EFLAGS register are automati-
cally stored on the stack along with the return instruction pointer when the processor
services an interrupt.