赞
踩
call和ret也是转移指令,它们都修改IP或同时修改CS和IP。他们经常被共同用来实现子程序设计。
ret指令用栈中数据修改IP,实现近转移。使用方法:ret
retf指令用栈中数据修改CS和IP,实现远转移。使用方法:retf
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
1000h
0
CPU执行call指令:
(1)将当前的IP或CS和IP压入栈中。
(2)转移。
call指令不能实现短转移,其他与jmp类似。具体分类如下:
call 标号(将当前IP压栈,转到标号处执行指令)(对应的机器指令中没有目的地址,只有相对转移位移)
(sp) = (sp)-2
((ss*16)+sp) = (IP)
(IP) = (IP)+16位位移
16位位移 = 标号处地址- call指令后第一个字节的地址 (-32768-32767)
相当于进行 push ip jmp near ptr 标号
6(执行完call s后,IP先变为6,然后将IP的值压栈,最后跳转至s)
call far ptr 标号(对应机器指令中有转移的目的地址)
段间转移,执行:
(sp) = (sp)-2
((ss * 16)+sp) = (CS)
(sp) = (sp)-2
((ss * 16)+sp) = (IP)
(CS)=(标号所在段的段地址)
(IP)=(标号在段中的偏移地址)
相当于进行 push cs push ip jmp far ptr 标号
1010H(执行完call far ptr s后,IP先变为8,然后将CS、IP的值分别为1000和8依此压栈,最后再跳转至s继续执行)
call 16位reg(转移地址在寄存器中)
执行:
(sp) = (sp)-2
((ss * 16)+sp) = (IP)
(IP) = (16位reg)
相当于 push ip jmp 16位reg
0BH
转移地址在内存中:
call word ptr 内存单元地址
相当于 push ip jmp word ptr 内存单元地址
call dword ptr 内存单元地址
相当于 push cs push ip jmp dword ptr 内存单元地址
(1)3(ds与ss中存放的段地址相同,在执行了call word ptr ds:[0EH]之后,程序会先将下一条指令inc ax的偏移量压栈,然后从ds:[0EH]读取两个内存单元作为IP的值,即刚刚压入的指令inc ax的偏移地址,CS:IP从inc ax继续执行,故最后ax的值为3。)
(2)ax=1,bx=0 (程序的一开始将s的偏移量和cs放入ss:[0]和ss:[2]中,然后调用call指令,将CS和IP(nop指令的偏移量)依此压栈后(sp = 0ch)跳转到s处继续执行,ax最终为s的偏移量减去nop指令所在位置的偏移量,为1,bx最终为cs的段地址相减,为0。)
程序返回前,(bx) = 8。从s到ret实现了计算2的N次方,N由cx提供。
子程序:具有某些功能的一段程序,在需要的时候用call转去执行,在子程序后加入ret,实现执行子程序后转回call之后的指令继续执行。
具有子程序的程序框架:
assume cs:code
code segment
main:
...
call sub1 ;调用子程序sub1
...
mov ax,4c00h
int 21h
sub1: ;子程序sub1开始
...
ret ;子程序返回
code ends
end main
mul指令
使用乘法指令需要注意:
(1)两个相乘的数:必须同时为16位或8位,如果是16位,一个在ax中,另一个在16位reg中或内存字单元中。如果是8位,一个在al中,一个在8位reg或内存字节单元中。
(2)结果:8位乘法的结果放在ax中,16位乘法的结果高位放dx,低位放ax。
指令:
mul reg 或 mul 内存单元
待解决问题:
1.如何存储子程序需要的参数和产生的返回值?
使用寄存器是最常用的一种方法。
2.如何传递批量数据?
将批量数据放入内存,将他们所在内存空间的首地址放在寄存器中,传递给子程序。对于有批量返回结果的子程序,也可以采用类似方法。
3.子程序与主程序使用寄存器冲突
使用栈。在子程序使用寄存器前将寄存器中的值保存起来,在子程序返回之前把这些保存的值恢复。
1.show_str.asm
assume cs:code, ss:stack stack segment dw 16 dup (0) stack ends data segment db 'Welcome to masm!',0 data ends code segment start: mov dh,8 mov dl,3 mov cl,2 mov ax,data mov ds,ax mov si,0 call show_str mov ax,4c00h int 21h show_str: push cx push bx push ax push si push di push es ;using cx, bx, ax, si, di, es mov ax, 0b800h mov es,ax mov bx,0 mov di,0 mov al,160 mul dh add bx,ax mov al,2 mul dl add bx,ax ;bx stores address of start character mov al,cl ;al stores the color of character char: mov ch,0 mov cl,ds:[si] jcxz zero mov ch,al mov es:[bx+di],cx add di,2 inc si jmp char zero: pop es pop di pop si pop ax pop bx pop cx ret code ends end start
2.divdw.asm
assume cs:code,ss:stack stack segment dw 16 dup (0) stack ends code segment start: mov ax,stack mov ss,ax mov sp,32 mov ax,4240h ;L16 mov dx,000fh ;H16 mov cx,0ah call divdw mov ax,4c00h int 21h divdw: push bx ;using bx mov bx,ax ;bx stores L mov ax,dx ;ax stores H mov dx,0 div cx ;after div, ax holds int(H/N), dx holds rem(H/N) push ax ; push int(H/N) temporarily mov ax,bx ;ax stores L, dx stores rem(H/N) div cx mov cx,dx pop dx ; dx stores int(H/N) pop bx ret code ends end start
3.dtoc.asm
assume cs:code,ss:stack stack segment dw 16 dup (0) stack ends data segment db 10 dup (0) data ends code segment start: mov ax,12666 mov bx,data mov ds,bx mov si,0 call dtoc mov dh,8 mov dl,3 mov cl,2 call show_str mov ax,4c00h int 21h dtoc: push ax push si push di push dx push bx push cx mov di,0 mov dx,0 mov bx,10 devide: mov cx,ax jcxz stop div bx inc di push dx mov dx,0 jmp devide stop: mov cx,di string: pop bx add bx,30h mov[si],bl inc si loop string pop cx pop bx pop dx pop di pop si pop ax ret show_str: push cx push bx push ax push si push di push es ;using cx, bx, ax, si, di, es mov ax, 0b800h mov es, ax mov bx, 0 mov di, 0 mov al, 160 mul dh add bx, ax mov al, 2 mul dl add bx, ax ;bx stores address of start character mov al, cl ;al stores the color of character char: mov ch, 0 mov cl, ds:[si] jcxz zero mov ch, al mov es:[bx+di], cx add di, 2 inc si jmp char zero: pop es pop di pop si pop ax pop bx pop cx ret code ends end start
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。