ret 和 retf
ret 指令用栈中的数据,修改IP的内容,从而实现近转移。
retf 指令用栈中的数据,修改CS和IP的内容,从而实现远转移。高位为CS(段地址 ),低位为IP(偏移地址)。入栈要先入CS的地址。
CPU执行ret指令时:
(IP) = ((ss) * 16 + (sp))
(sp) = (sp) + 2
相当于:
pop IP
CPU执行retf指令时:
(IP) = ((SS) * 16 + (SP))
(SP) = (SP) + 2
(CS) = ((SS) * 16 + (SP))
(SP) = (SP) + 2
相当于:
pop IP
pop CS
例:
assume cs:code, ss:stack
stack segment
db 16 dup (0)
stack ends
code segment
mov ax, 4c00H
int 21H
start: mov ax, stack
mov ss, ax
mov sp, 16
mov ax, 0
push ax
mov bx, 0
ret
code ends
end start
程序中ret拿到的IP的值为0,所以从程序的第一行开始执行。
assume cs:code, ss:stack
stack segment
db 16 dup (0)
stack ends
code segment
mov ax, 4C00H
int 21H
start: mov ax, stack
mov ss, ax
mov sp, 16
mov ax, code
push ax
mov ax, 0
push ax
mov bx
retf
code ends
end start
例: 从内存1000:0000处开始执行程序
assume cs:dode, ss:stack
stack segment
db 16 dup (0)
stack ends
code segment
stack: mov ax, stack
mov ss, ax
mov sp, 16
mov ax, 1000H //设CS = 1000H
push ax
mov ax, 0 //设IP = 0000H
push ax
retf
mov ax, 4c00H
int 21H
code ends
end start