您好,欢迎访问三七文档
当前位置:首页 > 医学/心理学 > 药学 > 清华大学“汇编语言课程”教案第7章
第七章高级汇编语言技术汇编语言宏汇编重复汇编条件汇编主要内容:汇编语言1.宏汇编宏:源程序中一段有独立功能的程序代码。宏指令:用户自定义的指令。在编程时,将多次使用的功能用一条宏指令来代替。汇编语言程序指令伪指令(伪操作)宏指令汇编语言宏展开:汇编程序把宏调用展开宏定义:macro_nameMACRO[哑元表];形参/虚参[LOCAL标号表]…………;宏定义体ENDM宏调用:(必须先定义后调用)macro_name[实元表];实参汇编语言例:保存寄存器宏展开:1pushax1pushbx1pushcx1pushdx1pushsi1pushdi宏定义:saveregMACROpushaxpushbxpushcxpushdxpushsipushdiENDM宏调用:savereg汇编语言例:两个字相乘宏定义:multiplyMACROopr1,opr2,resultpushdxpushaxmovax,opr1imulopr2movresult,axpopaxpopdxENDM宏调用:multiplycx,var,xyz[bx]宏展开:1pushdx1pushax1movax,cx1imulvar1movxyz[bx],ax1popax1popdx汇编语言例:求绝对值宏定义:absolMACROoperLOCALnextcmpoper,0jgenextnegopernext:ENDM宏调用:……absolvar……absolbx……宏展开:……1cmpvar,01jge??00001negvar1??0000:…………1cmpbx,01jge??00011negbx1??0001:……汇编语言宏汇编操作符:&;;%符号1&符号2宏展开时,合并前后两个符号形成一个符号。;;注释宏展开时,;;后面的注释不予展开。%表达式汇编程序将%后面的表达式转换为数字,并在展开期间用这个数取代哑元。汇编语言leapmacrocond,labj&condlabendm宏定义:宏调用:leapz,there……leapnz,here宏展开:1jzthere……1jnzhere汇编语言宏定义:msgmacrolab,num,xyzlab&numdb‘HELLOMR.&xyz’endm宏调用:msgary,1,wang……msgary,2,zhang宏展开:1ary1db‘HELLOMR.wang’……1ary2db‘HELLOMR.zhang’汇编语言msgmacrolab,num,xyzlab&numdb'HelloMR.&xyz'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyzlab&numdb'Hello&MR.&xyz'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyzlab&numdb'HelloMR.&xyz&'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyzlab&numdb'HelloMR.xy&z&'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyz&lab&num&db'HelloMR.&xyz'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyzla&b&numdb'HelloMR.&xyz'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言msgmacrolab,num,xyzla&b&n&umdb'HelloMR.&xyz'endmdatasegmentmsgary,1,wangmsgary,2,zhangdataends汇编语言宏定义:msgmacrocount,stringmsg&countdbstringendmerrmsgmacrotextcntr=cntr+1msg%cntr,textendm宏调用:cntr=0……errmsg‘SYNTAXERROR’……errmsg‘INVALIDOPERAND’宏展开:1cntr=cntr+11msg%cntr,‘SYNTAXERROR’2msg1db‘SYNTAXERROR’……1cntr=cntr+11msg%cntr,‘INVALIDOPERAND’2msg2db‘INVALIDOPERAND’汇编语言列表伪操作:.LALL:在LST清单中列出宏展开后的全部语句(包括注释)。.SALL:在LST清单中不列出任何宏展开后的语句。.XALL:省略的列表方式,只列出产生目标代码的语句。汇编语言宏库的建立与调用:;MACRO.MACmacro1MACRO[哑元表]……ENDMmacro2MACRO[哑元表]……ENDM……macroNMACRO[哑元表]……ENDM;EXP.ASMinclude路径\MACRO.MAC……macro1[实元表]……macro2[实元表]……macroN[实元表]……purgemacroN汇编语言宏调用与子程序调用的区别:子程序:在程序执行期间被调用,只占用自身大小的一段空间。宏调用:在汇编期间被展开,每调用一次就把宏定义体展开一次。汇编语言主程序……callsub1……callsub1……callsub1……子程序sub1………………ret汇编语言宏定义:Qmacrox,yendm宏调用:……Qa,b……Qc,d……Qe,f……宏展开:……………………汇编语言子程序:优点:模块化,节省内存,可被多次调用,编程效率高。缺点:额外开销(保存返回地址,计算转向地址,传递参数等)大,增加了执行时间。适于子功能代码较长、调用比较频繁的情况。宏调用:优点:参数传送简单,执行效率高。缺点:不节省空间,适于子功能代码较短、传参较多的情况。汇编语言重复伪操作REPTREPT表达式……;重复块ENDM2.重复汇编用于连续产生完全相同或基本相同的一组代码。不定重复伪操作IRP/IRPCIRP哑元,自变量表……;重复块ENDMIRPC哑元,字符串……;重复块ENDM汇编语言例:把字符‘A’到‘Z’的ASCII码填入数组TABLECHAR=‘A’TABLELABELBYTEREPT26DBCHARCHAR=CHAR+1ENDM1DB61H1DB62H1DB63H……1DB7AH汇编后:汇编语言例:;宏定义PUSH_TABMACROKPUSHTAB+kENDM;宏调用I=0REP5PUSH_TAB%II=I+2ENDM;宏展开2PUSHTAB+02PUSHTAB+22PUSHTAB+42PUSHTAB+62PUSHTAB+8汇编语言例:ARRAYLABELWORDREPT7DW$+2ENDMDWARRAY1DW$+21DW$+21DW$+21DW$+21DW$+21DW$+21DW$+2DWARRAY汇编后:汇编语言IRPREG,AX,BX,CX,DXPUSHREGENDM1PUSHAX1PUSHBX1PUSHCX1PUSHDX汇编后:例:arraylabelbyteIRPCK,12345db‘NO.&K’ENDM1db‘NO.1’1db‘NO.2’1db‘NO.3’1db‘NO.4’1db‘NO.5’汇编后:汇编语言3.条件汇编根据条件把一段源程序包括在汇编语言程序内或者排除在外。IF自变量;为条件……;自变量满足条件则汇编此块[ELSE]……;自变量不满足条件则汇编此块ENDIF一般格式:汇编语言IF表达式IFE表达式IF1IF2IFDEF符号IFNDEF符号IFB自变量IFNB自变量IFIDN字符串1,字符串2IFDIF字符串1,字符串2汇编语言例:求最大值放入AX宏定义:MAXMACROK,A,B,CLOCALNEXT,OUTMOVAX,AIFK-1IFK-2CMPC,AXJLENEXTMOVAX,CENDIFNEXT:CMPB,AXJLEOUTMOVAX,BENDIFOUT:ENDM1MOVAX,P1??0002:CMPQ,AX1JLE??00031MOVAX,Q1??0003:宏展开:1MOVAX,P1??0001:1MOVAX,P1CMPR,AX1JLE??00041MOVAX,R1??0004:CMPQ,AX1JLE??00051MOVAX,Q1??0005:宏调用:MAX1,PMAX2,P,QMAX3,P,Q,R汇编语言;宏展开1SALAX,12SALAX,13SALAX,1例:宏定义的递归调用;宏定义POWERMACROX,NSALX,1CNT=CNT+1IFCNT-NPOWERX,NENDIFENDM;宏调用CNT=0POWERAX,3汇编语言movax,dividendmovbx,divisorsubcx,cxcomp:cmpax,bxjboutsubax,bxinccxjmpcompout:movquotient,cxendmdividemacrodividend,divisor,quotientlocalcomp,outcnt=0ifndefdividendcnt=1endififndefdivisorcnt=1endififndefquotientcnt=1endififcntexitmendif例:汇编语言dividemacrodividend,divisor,quotientlocalcomp,outcnt=0ifndefdivdendcnt=1endififndefdivisorcnt=1endififndefquotientcnt=1endififcntexitmendif……macro.inc汇编语言includemacro.incdatasegmentaadw100bbdw25ccdw?dataendscodesegmentmainprocfarassumecs:code,ds:datastart:movax,datamovds,ax.lalldivideaa,bb,ccmovax,4c00hint21hmainendpcodeendsendstart汇编语言movifmacrotagifidn&tag,brepmovsbexitmendififidn&tag,wrepmovswexitmendififbtagrepmovsbendifendm例:宏定义macro1.inc汇编语言includemacro1.inccodesegmentmainprocfarassumecs:codestart:movifbmovifwmovifmovax,4c00hint21hmainendpcodeendsendstart汇编语言if1includemacro1.incendifcodesegmentmainprocfarassumecs:codestart:movifbmovifwmovifmovifamovifBmovax,4c00hint21hmainendpcodeendsendstart
本文标题:清华大学“汇编语言课程”教案第7章
链接地址:https://www.777doc.com/doc-2243070 .html