您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > IBM―PC汇编语言程序设计(第二版)电子教案第六章
1第六章子程序结构过程定义伪操作子程序的调用与返回保存与恢复寄存器子程序的参数传送子程序的嵌套与递归21.过程定义伪操作过程名PROCNEAR(FAR)过程名ENDP(1)NEAR属性:调用程序和子程序在同一代码段中(段内调用)(2)FAR属性:调用程序和子程序不在同一代码段中(段间调用)...3codesegmentmainprocfar……callsubr1……retmainendpsubr1procnear……retsubr1endpcodeendssegxsegmentsubtprocfar……retsubtendp……callsubt……segxendssegysegment……callfarptrsubt……segyends4子程序调用:隐含使用堆栈保存返回地址callnearptrsubp(1)保存返回地址(2)转子程序callfarptrsubp(1)保存返回地址(2)转子程序子程序返回:ret2.子程序的调用与返回(SP)→(IP)(IP)(CS)(SP)→53.保存与恢复寄存器subtprocfarpushaxpushbxpushcxpushdx…………popdxpopcxpopbxpopaxretsubtendp6(1)通过寄存器传送参数(2)通过存储器传送参数(3)通过地址表传送参数地址(4)通过堆栈传送参数或参数地址(5)多个模块之间的参数传送4.子程序的参数传送7例:十六进制到十进制的转换(通过寄存器传送参数)hexidecsegment;1610assumecs:hexidecmainprocfarstart:pushdssubax,axpushaxrepeat:callhexibin;162callcrlfcallbinidec;210callcrlfjmprepeatretmainendp………………hexidecendsendstart8hexibinprocnearmovbx,0newchar:movah,1int21hsubal,30hjlexitcmpal,10jladd_tosubal,27hcmpal,0ahjlexitcmpal,10hjgeexitadd_to:movcl,4shlbx,clmovah,0addbx,axjmpnewcharexit:rethexibinendpbinidecprocnearmovcx,10000dcalldec_divmovcx,1000dcalldec_divmovcx,100dcalldec_divmovcx,10dcalldec_divmovcx,1dcalldec_divretbinidecendpdec_divprocnearmovax,bxmovdx,0divcxmovbx,dxmovdl,aladddl,30hmovah,2int21hretdec_divendp1ab5316162359例:累加数组中的元素(通过存储器传送参数)datasegmentarydw1,2,3,4,5,6,7,8,9,10countdw10sumdw?dataendscodesegmentmainprocfarassumecs:code,ds:datamovax,datamovds,axcallproaddmovax,4c00hint21hmainendpcodeendsendmainproaddprocnearpushaxpushcxpushsileasi,arymovcx,countxorax,axnext:addax,[si]addsi,2loopnextmovsum,axpopsipopcxpopaxretproaddendp10如果数据段定义如下:datasegmentarydw1,2,3,4,5,6,7,8,9,10countdw10sumdw?ary1dw10,20,30,40,50,60,70,80,90,100count1dw10sum1dw?dataends如果直接访问内存变量,那么累加数组ary和数组ary1中的元素不能用同一个子程序proadd。11例:累加数组中的元素(通过地址表传送参数地址)datasegmentarydw10,20,30,40,50,60,70,80,90,100countdw10sumdw?tabledw3dup(?);地址表dataendscodesegmentmainprocfarassumecs:code,ds:datapushdssubax,axpushaxmovax,datamovds,axmovtable,offsetarymovtable+2,offsetcountmovtable+4,offsetsummovbx,offsettablecallproaddretmainendp12proaddprocnearpushaxpushcxpushsipushdimovsi,[bx]movdi,[bx+2]movcx,[di]movdi,[bx+4]xorax,axnext:addax,[si]addsi,2loopnextmov[di],axpopdipopsipopcxpopaxretproaddendpcodeendsendmain30d40d50d60d70d80d90d100d10dary20d10dcountsumtable0000001400160018000000140016(bx)(si)(di)550d13例:累加数组中的元素(通过堆栈传送参数地址)datasegmentarydw10,20,30,40,50,60,70,80,90,100countdw10sumdw?dataendsstacksegmentdw100dup(?)toslabelwordstackends14code1segmentmainprocfarassumecs:code1,ds:data,ss:stackstart:movax,stackmovss,axmovsp,offsettosmovax,datamovds,axmovbx,offsetarypushbxmovbx,offsetcountpushbxmovbx,offsetsumpushbxcallfarptrproaddmovax,4c00hint21hmainendpcode1ends15code2segmentassumecs:code2proaddprocfarpushbpmovbp,sppushaxpushcxpushsipushdimovsi,[bp+0ah]movdi,[bp+8]movcx,[di]movdi,[bp+6]code2endsendstartxorax,axnext:addax,[si]addsi,2loopnextmov[di],axpopdipopsipopcxpopaxpopbpret6proaddendp(ip)(cs)sumcountarray(di)(si)(cx)(ax)(sp)(bp)(bp)(bp)+0ah(bp)+8(bp)+6tos16多个模块之间的参数传送:局部符号:在本模块中定义,在本模块中引用的符号外部符号:在某一模块中定义,在另一模块中引用的符号PUBLIC符号EXTRN符号:类型extrnproadd:far…………code1segmentstart:……callfarptrproadd……code1endsendstart;proadd1.asmpublicproadd…………code2segmentproaddprocfar……retproaddendpcode2endsend;proadd2.asm17例:;proadd1.asmextrnproadd:fardatasegmentcommonarydw1,2,3,4,5,6,7,8,9,10countdw10sumdw?dataendscode1segmentmainprocfarassumecs:code1,ds:datastart:movax,datamovds,axcallfarptrproaddmovah,4chint21hmainendpcode1endsendstart18;proadd2.asmpublicproadddatasegmentcommonarydw1,2,3,4,5,6,7,8,9,10countdw10sumdw?dataendscode2segmentproaddprocfarassumecs:code2,ds:datamovax,datamovds,axpushaxpushcxpushsileasi,arymovcx,countxorax,axnext:addax,[si]addsi,2loopnextmovsum,axpopsipopcxpopaxretproaddendpcode2endsend19子程序的嵌套:主程序子程序A子程序B递归子程序:n!=n(n-1)(n-2)…13!=3×2×1=65.子程序的嵌套与递归调用……callproc_A……proc_A……callproc_B……Callproc_Aretproc_B……ret20例:计算n!framestrucsave_bpdw?save_cs_ipdw2dup(?)ndw?result_addrdw?frameends;---------------------------------datasegmentn_vdw3resultdw?dataends;---------------------------------stacksegmentdw128dup(?)toslabelwordstackendsmovax,[bp].nmovbx,[bp].result_addr0!=1n!=n(n-1)!21codesegmentmainprocfarassumecs:code,ds:data,ss:stackstart:movax,stack;用户堆栈地址movss,axmovsp,offsettospushds;保存返回DOS地址subax,axpushaxmovax,data;数据段地址movds,axmovbx,offsetresult;结构数据入栈pushbxmovbx,n_vpushbxcallfarptrfactretmainendpcodeends22code1segmentassumecs:code1factprocfarpushbpmovbp,sppushbxpushaxmovbx,[bp].result_addrmovax,[bp].ncmpax,0jedonepushbxdecaxpushaxcallfarptrfactmovbx,[bp].result_addrmovax,[bx]mul[bp].njmpshortreturndone:movax,1return:mov[bx],axpopaxpopbxpopbpret4factendpcode1ends(AX)(BX)(BP)CODE中的(IP)CODE中的(CS)3RESULT的地址第1帧(AX)(BX)(BP)CODE1中的(IP)CODE1中的(CS)2RESULT的地址第2帧(AX)(BX)(BP)CODE1中的(IP)CODE1中的(CS)1RESULT的地址第3帧(AX)(BX)(BP)CODE1中的(IP)CODE1中的(CS)0RESULT的地址第4帧00FA00F200E400D600C8000000F200E400D623factprocnearpushaxpushbpmovbp,spmovax,[bp+6]cmpax,0jnefact1incaxjmpexitfact1:decaxpushaxcallfactpopaxmulwordptr[bp+6]exit:mov[bp+6],axpopbppopaxretfactendp例:计算n!movbx,n_vpushbxcall
本文标题:IBM―PC汇编语言程序设计(第二版)电子教案第六章
链接地址:https://www.777doc.com/doc-3496437 .html