汇编语言第6章子程序结构.ppt
1,第6章 子程序结构,过程定义伪操作 子程序的调用与返回 现场保护与恢复 子程序的参数传送 子程序的嵌套与递归,2,一.过程定义伪操作,3,4,1.子程序调用:隐含使用堆栈保存返回地址 call near ptr subp(1)保存返回地址(2)转子程序 call far ptr subp(1)保存返回地址(2)转子程序2.子程序返回:ret,二.子程序的调用与返回,5,子程序调用和返回指令:,code1 segmentmain proc far call far ptr subp retmain endpcode1 endscode2 segmentsubp proc far retsubp endpcode2 ends,段间调用和返回,code segmentmain proc far call subp retmain endpsubp proc near retsubp endpcode ends,段内调用和返回,6,例:带立即数返回,(SP),堆栈段,code segmentmain proc far push ax push bx push cx call sub retmain endpsub proc near ret 6sub endpcode ends,(SP),7,下面程序段是判断寄存器AH和AL中D3位是否相同,如相同,AH置0,否则AH置全1.试把空白处填上适当指令。AND AH,08H MOV AH,0FFH JMP NEXTZERO:MOV AH,0NEXT:,8,三.现场保护与恢复,subt proc far push ax push bx push cx push dx pop dx pop cx pop bx pop ax retsubt endp,9,子程序的参数传送 原则:寄 存 器:参数少 堆 栈:参数较多 存储单元:参数多,10,(1)通过寄存器传送参数(2)通过存储器传送参数(3)通过地址表传送参数地址(4)通过堆栈传送参数或参数地址(5)多个模块之间的参数传送,11,例:十六进制到十进制的转换(通过寄存器传送参数)hexidec segment;1610 assume cs:hexidec main proc farstart:push ds sub ax,ax push axrepeat:call hexibin;162 call crlf call binidec;210 call crlf jmp repeat retmain endphexidec ends end start,12,1 a b 5 31 61 62 35,Sub al,7,13,例:累加数组中的元素(通过存储器传送参数)data segment ary dw 1,2,3,4,5,6,7,8,9,10 count dw 10 sum dw?data endscode segmentmain proc far assume cs:code,ds:data mov ax,data mov ds,ax call proadd mov ax,4c00h int 21hmain endpcode ends end main,proadd proc near push ax push cx push si lea si,ary mov cx,count xor ax,axnext:add ax,si add si,2 loop next mov sum,ax pop si pop cx pop ax retproadd endp,14,如果数据段定义如下:data segment ary dw 1,2,3,4,5,6,7,8,9,10 count dw 10 sum dw?ary1 dw 10,20,30,40,50,60,70,80,90,100 count1 dw 10 sum1 dw?data ends,如果直接访问内存变量,那么累加数组ary和数组ary1中的元素不能用同一个子程序 proadd。,15,例:累加数组中的元素(通过地址表传送参数地址)data segment ary dw 10,20,30,40,50,60,70,80,90,100 count dw 10 sum dw?table dw 3 dup(?);地址表data endscode segmentmain proc far assume cs:code,ds:data push ds sub ax,ax push ax mov ax,data mov ds,ax mov table,offset ary mov table+2,offset count mov table+4,offset sum mov bx,offset table call proadd retmain endp,16,proadd proc near push ax push cx push si push di mov si,bx mov di,bx+2 mov cx,di mov di,bx+4 xor ax,axnext:add ax,si add si,2 loop next mov di,ax pop di pop si pop cx pop ax retproadd endpcode ends end main,550d,17,例:累加数组中的元素(通过堆栈传送参数地址)data segment ary dw 10,20,30,40,50,60,70,80,90,100 count dw 10 sum dw?data endsstack segment dw 100 dup(?)tos label wordstack ends,18,code1 segmentmain proc far assume cs:code1,ds:data,ss:stackstart:mov ax,stack mov ss,ax mov sp,offset tos mov ax,data mov ds,ax mov bx,offset ary push bx mov bx,offset count push bx mov bx,offset sum push bx call far ptr proadd mov ax,4c00h int 21hmain endpcode1 ends,19,(ip)(cs)sumcountarray,(bp)+0ah,20,多个模块之间的参数传送:局部符号:在本模块中定义,在本模块中引用的符号外部符号:在某一模块中定义,在另一模块中引用的符号 PUBLIC 符号 EXTRN 符号:类型,21,例:,;proadd1.asmextrn proadd:fardata segment common ary dw 1,2,3,4,5,6,7,8,9,10 count dw 10 sum dw?data endscode1 segment main proc far assume cs:code1,ds:datastart:mov ax,data mov ds,ax call far ptr proadd mov ah,4ch int 21hmain endpcode1 ends end start,22,;proadd2.asmpublic proadddata segment common ary dw 1,2,3,4,5,6,7,8,9,10 count dw 10 sum dw?data endscode2 segment proadd proc far assume cs:code2,ds:data mov ax,data mov ds,ax push ax push cx push si lea si,ary mov cx,count xor ax,ax,next:add ax,si add si,2 loop next mov sum,ax pop si pop cx pop ax retproadd endpcode2 ends end,23,一、子程序的嵌套:注意:层数不限,防止堆栈溢出 主程序 子程序A 子程序B,二、递归子程序:n!n(n-1)(n-2)1 3!3 21=6,6.2 子程序的嵌套与递归调用,24,fact proc near push ax push bp mov bp,sp mov ax,bp+6 cmp ax,0 jne fact1 inc ax jmp exitfact1:dec ax push ax call fact pop ax mul word ptrbp+6exit:mov bp+6,ax pop bp pop ax ret fact endp,例:计算n!,主程序:,0!=1,1!=1,2!=2,3!=6,25,例:将字符串反序输出,revers proc near push ax push bx push dx push bp mov bp,sp mov bx,bp+10 mov al,bx cmp al,$jne re_call jmp returnre_call:inc bx push bx call revers pop bx mov dl,bx mov ah,2 int 21hreturn:pop bp pop dx pop bx pop ax retrevers endp,mov bx,offset strpush bxcall reverspop bxmov dl,bxmov ah,2int 21h,主程序:,str db happy!,$,26,本章小结:掌握子程序的定义、调用和参数传递方法。,