Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Carter P.A.PC assembly language.2005.pdf
Скачиваний:
15
Добавлен:
23.08.2013
Размер:
1.07 Mб
Скачать

4.4. THE CALL AND RET INSTRUCTIONS

69

The stack can be used as a convenient place to store data temporarily. It is also used for making subprogram calls, passing parameters and local variables.

The 80x86 also provides a PUSHA instruction that pushes the values of

EAX, EBX, ECX, EDX, ESI, EDI and EBP registers (not in this order).

The POPA instruction can be used to pop them all back o .

4.4The CALL and RET Instructions

The 80x86 provides two instructions that use the stack to make calling subprograms quick and easy. The CALL instruction makes an unconditional jump to a subprogram and pushes the address of the next instruction on the stack. The RET instruction pops o an address and jumps to that address. When using these instructions, it is very important that one manage the stack correctly so that the right number is popped o by the RET instruction!

The previous program can be rewritten to use these new instructions by changing lines 25 to 34 to be:

mov

ebx, input1

call

get_int

mov

ebx, input2

call

get_int

 

 

and change the subprogram get int to:

get_int:

 

call

read_int

mov

[ebx], eax

ret

 

 

 

There are several advantages to CALL and RET:

It is simpler!

It allows subprograms calls to be nested easily. Notice that get int calls read int. This call pushes another address on the stack. At the end of read int’s code is a RET that pops o the return address and jumps back to get int’s code. Then when get int’s RET is executed, it pops o the return address that jumps back to asm main. This works correctly because of the LIFO property of the stack.

Соседние файлы в предмете Электротехника