Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаб2012 / 25366517.pdf
Скачиваний:
65
Добавлен:
02.02.2015
Размер:
3.33 Mб
Скачать

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

6.3CALLING PROCEDURES USING CALL AND RET

The CALL instruction allows control transfers to procedures within the current code segment (near call) and in a different code segment (far call). Near calls usually provide access to local procedures within the currently running program or task. Far calls are usually used to access operating system procedures or procedures in a different task. See “CALL—Call Procedure” in Chapter 3, Instruction Set Reference A-M of the IA-32 Intel Architecture Software Developer’s Manual, Volume 2A for a detailed description of the CALL instruction.

The RET instruction also allows near and far returns to match the near and far versions of the CALL instruction. In addition, the RET instruction allows a program to increment the stack pointer on a return to release parameters from the stack. The number of bytes released from the stack is determined by an optional argument (n) to the RET instruction. See “RET—Return from Procedure” in Chapter 4, Instruction Set Reference N-Z of the IA-32 Intel Architecture Software Developer’s Manual, Volume 2B for a detailed description of the RET instruction.

6.3.1Near CALL and RET Operation

When executing a near call, the processor does the following (see Figure 6-2):

1.Pushes the current value of the EIP register on the stack.

2.Loads the offset of the called procedure in the EIP register.

3.Begins execution of the called procedure.

When executing a near return, the processor performs these actions:

1.Pops the top-of-stack value (the return instruction pointer) into the EIP register.

2.If the RET instruction has an optional n argument, increments the stack pointer by the number of bytes specified with the n operand to release parameters from the stack.

3.Resumes execution of the calling procedure.

6.3.2Far CALL and RET Operation

When executing a far call, the processor performs these actions (see Figure 6-2):

1.Pushes the current value of the CS register on the stack.

2.Pushes the current value of the EIP register on the stack.

3.Loads the segment selector of the segment that contains the called procedure in the CS register.

4.Loads the offset of the called procedure in the EIP register.

5.Begins execution of the called procedure.

Vol. 1 6-5

PROCEDURE CALLS, INTERRUPTS, AND EXCEPTIONS

When executing a far return, the processor does the following:

1.Pops the top-of-stack value (the return instruction pointer) into the EIP register.

2.Pops the top-of-stack value (the segment selector for the code segment being returned to) into the CS register.

3.If the RET instruction has an optional n argument, increments the stack pointer by the number of bytes specified with the n operand to release parameters from the stack.

4.Resumes execution of the calling procedure.

 

 

 

Stack During

 

 

 

 

 

 

 

 

 

 

 

 

 

Stack During

 

 

 

 

 

 

Stack

Near Call

 

 

 

 

 

Stack

 

Far Call

 

 

 

 

 

 

Frame

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Frame

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Before

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Before

 

 

 

 

 

 

 

 

 

 

 

 

 

Call

 

 

Param 1

 

 

 

 

 

 

 

 

 

 

Param 1

 

 

 

 

 

 

 

 

 

 

 

 

Call

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 2

 

 

 

 

 

 

 

 

 

 

Param 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ESP Before Call

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 3

 

 

 

 

 

 

 

 

 

 

Param 3

 

 

 

 

 

ESP Before Call

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Stack

 

 

Calling EIP

 

 

 

 

 

ESP After Call

 

 

 

 

Calling CS

 

 

 

 

 

ESP After Call

Frame

 

 

 

 

 

 

 

Stack

 

 

 

Calling EIP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

After

 

 

 

 

 

 

Frame

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Call

 

 

 

 

 

 

After

 

 

 

 

 

 

 

 

 

 

 

Stack During

 

 

 

 

 

Call

 

Stack During

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Near Return

 

 

 

 

ESP After Return

 

Far Return

 

 

 

 

 

ESP After Return

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 1

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 1

 

 

 

 

 

 

 

 

 

Param 2

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 2

 

 

 

 

 

 

 

 

 

Param 3

 

 

 

 

 

 

 

 

 

 

 

 

 

Param 3

 

 

 

 

 

 

 

 

 

Calling EIP

 

 

 

 

 

ESP Before Return

 

Calling CS

 

 

 

 

 

ESP Before Return

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Calling EIP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Note: On a near or far return, parameters are released from the stack based on the optional n operand in the RET n instruction.

Figure 6-2. Stack on Near and Far Calls

6-6 Vol. 1

Соседние файлы в папке Лаб2012