《汇编语言》学习(十)call and ret

时间:2020-12-03 00:40:10

    《汇编语言》第十章主要介绍CALL和RET指令的原理。本文我将用一张知识图谱总结它的知识点,然后重点展示它的各个课后习题。

1,知识图谱

    CALL和RET是为函数调用和函数返回准备的一对汇编指令。他们都借助“栈空间”来存储或获取转移的目标地址。

《汇编语言》学习(十)call and ret


2,习题

检测点10.1

assume cs:code

stack segment
db 16 dup(0)
stack ends

code segment
start:mov ax,stack
mov ss,ax
mov sp,10h
mov ax,1000h
push ax
mov ax,0
push ax
retf
code ends

end start

检测点10.2

内存地址        机器码        汇编指令
1000:0 b8 00 00 mov ax,0
1000:3 e8 01 00 call s
1000:6 40 inc ax
1000:7 58 s:pop ax

    该程序执行后,ax中的值为“6”

它的运行流程是:mov ax,0,此时ax=0    -> call s,读取该条指令后,IP=6,执行该指令,IP压栈,程序跳转位移为“0001h”,即跳过一个字节,从1000:6跳到1000:7    ->  pop ax,出栈,ax = 6。


检测点10.3

内存地址        机器码                汇编指令
1000:0 b8 00 00 mov ax,0
1000:3 9a 09 00 00 10 call far ptr s
1000:8 40 inc ax
1000:9 58 s:pop ax
add ax,ax
pop bx
add ax,bx

    该程序执行后,ax = 1010h。

    程序执行过程:1000:0,ax=0 -> 1000:3,ip=8,push cs,push ip   -> 1000:9


检测点10.4

内存地址        机器码        汇编指令  
1000:0 b8 06 00 mov ax,6
1000:3 ff d0 call ax
1000:5 40 inc ax
1000:6 mov bp,sp
add ax,[bp]

    该程序执行后,ax=11。

   分析:

1)mov ax,6指令执行后,ax=6;

2)call ax指令读取后,ip=5,执行后,将5压栈,且sp所指内存单元存的就是“5”。同时,将ax的值赋给ip,即跳转到1000:6执行。

3)最后两条指令等价于“pop bx, add ax,bx”。


检测点10.5

assume cs:code

stack segment
dw 8 dup(0)
stack ends

code segment
start:mov ax,stack
mov ss,ax
mov sp,16
mov ds,ax
mov ax,0
call word ptr ds:[0EH]
inc ax
inc ax
inc ax
mov ax,4c00h
int 21h
code ends

end start


    该程序执行后,ax=3

    事实上,在call指令中,相当于先将当前IP压栈,然后有通过取内存的方式将这个刚压栈的值取出并赋给IP。整个过程,不影响程序的执行顺序。

检测点10.5b

assume cs:code

data segment
dw 8 dup(0)
data ends

code segment
start:mov ax,data
mov ss,ax
mov sp,16
mov word ptr ss:[0],offset s
mov ss:[2],cs
call dword ptr ds:[0]
nop
s:mov ax,offset s
sub ax,ss:[0ch]
mov bx,cs
sub bx,ss:[0eh]
mov ax,4c00h
int 21h
code ends

end start

    该程序退出前(mov ax,4c00h之前),ax=0,bx=0。


task1

assume cs:codesg

stack segment
dw 16 dup(0)
stack ends

data segment
db '1975','1976','1977','1978','1979','1980','1981','1982','1983'
db '1984','1985','1986','1987','1988','1989','1990','1991','1992'
db '1993','1994','1995'
dd 16,22,382,1356,2390,8000,16000,24486,50065,97479,140417,197514
dd 345980,590827,803530,1583000,1843000,2759000,3753000,4649000,5937000
dw 3,7,9,13,28,38,130,220,476,778,1001,1442,2258,2793,4037,5635,8226
dw 11542,14430,15257,17800
data ends

table segment
db 21 dup('year summ ne ?? ')
table ends

string segment
db 16 dup(0)
string ends

codesg segment
show_str:push ax
push es
push bx
push dx
push di
push cx
push si

mov ax,0b800h
mov es,ax
mov al,160
mul dh
mov bx,ax
mov al,2
mul dl
mov di,ax

mov ah,cl
s2:mov cl,[si]
mov ch,0
jcxz ok
mov al,[si]
mov es:[bx+di],ax
inc si
add di,2
jmp s2

ok:pop si
pop cx
pop di
pop dx
pop bx
pop es
pop ax
ret

dtoc:push ax
push bx
push cx
push dx
push si
push di

sub di,di
s3:mov cx,ax
jcxz ok1
mov cx,10
call divdw
add cx,30h
push cx
inc di
jmp s3

ok1:mov cx,di
s4:pop ax
mov [si],al
inc si
loop s4
mov byte ptr [si],0

pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret

divdw:push bx
push si
mov bx,ax
mov ax,dx
sub dx,dx
div cx
mov si,ax
mov ax,bx
;sub dx,dx
div cx
mov cx,dx
mov dx,si
pop si
pop bx
ret
start:mov ax,data
mov ss,ax
mov sp,0a8h
mov ax,table
mov ds,ax

mov bp,0
mov bx,0
mov cx,21
s:mov si,0
mov ax,0[bp]
mov [bx].0[si],ax
mov ax,054h[bp]
mov [bx].05h[si],ax

add bp,2
add si,2
mov dx,0[bp]
mov [bx].0[si],dx
mov dx,054h[bp]
mov [bx].05h[si],dx

pop [bx+0ah]
div word ptr [bx+0ah]
mov [bx+0dh],ax

add bp,2
add bx,10h
loop s

mov ax,string
mov ds,ax
mov ax,table
mov es,ax
mov cx,21
mov bx,0
s1:sub di,di
sub si,si
mov ax,es:[bx+di]
mov [bx+di],ax
add di,2
mov ax,es:[bx+di]
mov [bx+di],ax
mov byte ptr [si+4],0
mov dh,8
mov dl,3
mov cl,2
call show_str

add di,3
mov ax,es:[bx+di]
add di,2
mov dx,es:[bx+di]
sub si,si
call dtoc
mov dh,8
mov dl,10
mov cl,2
call show_str

add di,3
sub dx,dx
mov ax,es:[bx+di]
sub si,si
call dtoc
mov dh,8
mov dl,20
mov cl,2
call show_str

add di,3
sub dx,dx
mov ax,es:[bx+di]
sub si,si
call dtoc
mov dh,8
mov dl,25
mov cl,2
call show_str
add bx,10h
;loop s1

mov ax,4c00h
int 21h
codesg ends

end start

task 1可以参考:http://www.cnblogs.com/S-E-P/archive/2009/12/29/2045085.html