[汇编语言]-第十章 ret,retf,call指令

时间:2023-03-09 09:28:21
[汇编语言]-第十章 ret,retf,call指令

1- ret 相当于 pop IP;用栈中数据,修改IP内容.从而实现近转移.

执行后(IP)=0, CS:IP指向代码段的第一条指令.

 assume cs:code
stack segment
db dup()
stack ends
code segment
mov ax,4c00h
int 21h
start:
mov ax,stack
mov ss,ax
mov sp,
mov ax,
push ax
mov bx,
ret
code ends
end start

2- retf 相当于 pop IP; pop CS;用栈中数据修改CS,IP内容.从而实现远转移.

执行后 CS:IP指向代码段的第一条指令.

 assume cs:code
stack segment
db dup()
stack ends
code segment
mov ax,4c00h
int
start:
mov ax,stack
mov ss,ax
mov sp,
mov ax,
push cs
push ax
retf
code ends
end start

reference:

http://bbs.****.net/topics/340237304

3- 实现从内存1000:0000处开始执行指令

 assume cs:code
stack segment
db dup()
stack ends
code segment
mov ax,4c00h
int
start:
mov ax,stack
mov ss,ax
mov sp,
mov ax,1000H
push ax
mov ax,
push ax
retf
code ends
end start

4- call 标号 (将当前的IP压栈后,转到标号处执行指令)

push Ip

jmp near ptr 标号

[汇编语言]-第十章 ret,retf,call指令

 assume cs:code
code segment
start:
mov ax,
call s
inc ax
s:
pop ax
code ends
end start

ax=6

CPU指令执行的过程:

1- 从CS:IP指向的内存单元读取指令,读取的指令,进入指令缓冲器.

2- (IP) = (IP) + 所读取的指令长度, 从而指向下一条指令

3- 执行指令, 转到1, 重复这个过程

5- call far ptr 标号

push cs

push ip

jmp far ptr 标号

[汇编语言]-第十章 ret,retf,call指令

[汇编语言]-第十章 ret,retf,call指令

执行步骤分析:

CS:1000 IP:3 时 读取call far ptr s放入缓存, ip = IP + 5 = 8

push cs  //1000

push ip  //8

跳转到s执行

pop ax  //ax = 8

add ax,ax // ax = 8 + 8 = 10 寄存器中存放的数为16进制

pop bx  //bx = 1000

add ax,bx  //ax = 1000 + 10 = 1010

最终结果 ax = 1010

6- call 16位寄存器

push IP

jmp 16位寄存器  // mov IP,16位寄存器; jmp IP

[汇编语言]-第十章 ret,retf,call指令

ax=000B

sp是栈顶地址,[bp]栈顶值为5

ax = 6 + 5 = B

reference:

http://bbs.****.net/topics/100016651