版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1. 将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中sign与sinteger均为双字变量。if ( sinteger = = 0) sign = = 0;else If ( siteger > 0) sign = 1;elsesign = 1;mov eax,sintegermov edx,signcmp eax,0jnz L1mov ebx,0L1:cmp ebx,0jl L2mov ebx,1L2:mov ebx,-12. 将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中ch1与caps均为字节变量。if (ch1> =a &&
2、; ch1< =z) caps= =0;if (ch1> =A && ch1< =Z) caps= =1;mov ax,ch1mov bx,capscmp ax,ajb nextcmp ax,zja nextmov bx,0next:cmp ax,Ajl donecmp ax,Zja donedone:3. 将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中sum与i变量均为双字变量。sum=0;for ( i=1;i< =100;i+)if ( i%2= =0) sum=sum+i;mov ecx,imov ecx,1.while(ecx
3、<=100)mov eax,ecx xor edx,edxmov ebx,2div ebxcmp edx,0jnz nextadd sum,ecxnext:inc ecx.endw1. 能被4整除但不能被100整除,或者年被400整除的年份是闰年。编程写一个完整的程序,求出2012年2099年中的所有闰年年份,并把它们存放在数组Lyear中。算法描述; esi=0;ecx=2012; while (ecx<2100); if (year mod 4=0 and year mod 100 <>0) or (year mod 400=0) then; Lyearesi=ec
4、x;esi+; ecx+; ; Lcounter=esi;include io32.inc.data Lyear dword 100 dup(?)Lcounter dword 0.codemainproc xoresi,esi ;esi闰年个数计数器,兼做Lyear下标。 movecx,2012 ;ecx年份计数器。 .while (ecx<2100) moveax,ecxxoredx,edxmovebx,400divebxcmpedx,0jzleap;if year mod 400=0 then goto leap moveax,ecx xoredx,edx movebx,4 dive
5、bx cmpedx,0 jnznext;if year mod 4<>0 then goto next moveax,ecx xoredx,edx movebx,100 divebx cmpedx,0 jznext;if year mod 100=0 then goto nextleap: movLyearesi*4,ecxincesimoveax,ecx calldispuid;输出,用于验证。可以删掉calldispcrlf ;输出,用于验证。可以删掉next:incecx .endw movLcounter,esi moveax,esi calldispuid ;输出,用于验
6、证。可以删掉 calldispcrlf ;输出,用于验证。可以删掉 retmainendp ;end of mainend main ;end of assembly2. 编程写一个完整的程序,求出2100之间的所有素数,并将它们存入Prime数组中,素数的个数存入变量Pcounter中。; 采用伪代码pseudo code描述算法; 1. i=2 to 100 do; 1.1 if i is prime number then print i; 细化 1.1 如下:; 1.1 j=2 to i/2 do; if i mod j=0 then goto next i; 1.1.2 print
7、i; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除数,ecx做除数.include io32.inc.datamsgbyte' List of prime number',13,10,0msg1 byte ' Lcounter is :' ,13,10,0blankbyte' ',0prime dword 100 dup(?)pcounter dword 0.codemainproc ;主程序开始mov esi,0moveax,offset msgcall dispmsgmovebx,2iLoop:cmpebx,100 ;i循环入口
8、jadonemovecx,ebxshrecx,1 ;j=i/2jLoop:cmpecx,2 ;j循环入口jbprintmoveax,ebxcdq;xor edx,edxdivecx;被除数送eax,32位除法oredx,edx ;cmp edx,0jznexti ;if i mod j=0 then goto next idececxjmpjLoopprint:movprimeesi*4,ebxincesimoveax,ebxmoveax,offset blankcall dispmsg;显示空格nexti:incebx;i=i+1jmpiLoopdone:calldispcrlf movea
9、x,offset msg1call dispmsg movpcounter,esi moveax,esi call dispuidcalldispcrlf ret;返回操作系统mainendp ;主程序结束end main ;end of assembly3. 编程写一个完整的程序,将数组aray中的元素按逆序存放,要求程序中附加的变量最少。数据段的定义如下:.data aray dword 12,4, 168,122,33,56,78,99,345, 66,5; 采用伪代码pseudo code描述算法; 1. i=n-1 downto 1 do; 1.1j=0 to i-1; if aj&
10、gt;aj+1 then swap; 合理分配寄存器,i=ecx,j=edx ,i-1=ecx-1 include io32.inc.data ;set data segment blank3byte 3 dup(20h),0 arraydword12,4,-168,122,33,56,78,99,345,-66,-5 char byte ? msg byte 13,10,'press any key to continue .',0;字符串.codemainproc movecx,(lengthof array)-1;计数循环的初值iLoop: ;i循环入口 dececx;e
11、cx=i-1 xoredx,edxjLoop:;j循环入口,循环控制变量edx cmpedx,ecx jgnexti moveax,arrayedx*4 cmpeax,arrayedx*4+4 jgenextj xchgeax,arrayedx*4+4 movarrayedx*4,eaxnextj: incedx jmpjLoopnexti: inc ecx;retrieve ecx loopiLoopprint: xorecx,ecx again: cmpecx,lengthof array-1 jgdone moveax,arrayecx*4 calldispsid mov eax,off
12、set blank3;显示空格 call dispmsg incecx jmpagaindone: mov eax,offset msg;显示:press any key to continue call dispmsg mov eax,offset char ;暂停,等待按任意键 call readcret ;返回操作系统mainendpend main ;end of assembly4. 编程写一个完整的程序,求数组aray中的最大值与最小值,并将它们分别存入max和min元中。数据段的定义如下:.data aray dword 12,4,168,122,33,56,78,99,345,
13、66,5 min dword ? max dword ?include io32.inc.data aray dword 12,4,168,122,33,56,78,99,345,66,5 min dword ? max dword ? promptbyte'Enter an integers :',0 maxStrbyte'max=%d',13,10,0 ;显示字符串的格式描述串 minStrbyte'min=%d',13,10,0.code mainprocmovecx,lengthof ara -1moveax,ara0;eax:maxmo
14、vebx,eax ;ebx,minmovesi,1again:cmpeax,araesi*4jgesmallmoveax,araesi*4small:cmpebx,araesi*4jlenextmovebx,araesi*4next:incesiloopagain movmax,eaxmovmin,ebxret ;返回操作系统 mainendpend main 5. 编程写一个完整的程序统计msg中的空格的个数与小写字母的个数,并分别将它们存入space单元与char单元中。数据段的定义如下:.data msg byte 'I love XUT !',13,10,0 space
15、 dword ? char dword ?include io32.inc.datamsg byte 'I love XUT!',13,10,0 space dword ? char dword ?.codemain procmov ebx,0xor edi,edimov esi,edil1: mov al,msgebxcmp al,20hjnz done1cmp al,0jz nextinc ediinc ebxjmp l1done1: cmp al,'a'jb done2cmp al,'z'ja done2cmp al,0jz nextinc
16、 esiinc ebxjmp l1done2: cmp al,0jz nextinc ebx jmp l1next: mov space,edimov eax,spacecall dispuidcall dispcrlfmov char,esimov eax,charcall dispuidcall dispcrlfretmain endpend main6. 编程写一个完整的程序,将字符串msg中所有的小写字母转换为大写字母。数据段的定义如下:.data msg byte 'I love XUT !',13,10,0include io32.inc.datamsg byte
17、'I love XUT!',13,10,0.codemain procmov ebx,0l1: mov al,msgebxcmp al,'a'jb donecmp al,'z'ja donesub al,20hdone: cmp al,0jz done1inc ebx call dispcjmp l1done1:retmain endpend main7. array是一无符号数数组,数据段的定义如下。要求:编程写一个完整的程序求出数组元素中偶数的和,并将它存入esum单元中。.data array dword 12,34,123,78,43,2
18、34,79,86,98,20 esum dword ?算法描述:; 1. esum=0; 2. for i=0 to n-1 do; if ai is evennunber then esum=esum+ai; 判断偶数,采用 test 指令测试最低位。; 合理分配寄存器:采用loop循环,ecx=lengthof array esum=eax=0,esi=0,做下标,采用带比例因子的相对寻址处理数组。include io32.inc.data array dword12,34,123,78,43,234,79,86,98,20 esum dword ? fmtStr byte' es
19、um=%d',13,10,0 ;格式描述串 .codemain procmovecx,lengthof arrayxor eax,eax ;esum=eax=0movesi,eax ;数组下标again: movebx,arrayesi*4 testebx,1jnznext ;if ai is evennunber then esum=esum+ai; addeax,ebx ;注意:转换成汇编语言时,测试的是不是偶数时则取下一个数测试。next:incesiloopagain movesum,eaxinvoke printf,offset fmtStr,esum ret;return
20、to Windowsmain endp ;end of mainend main ;end of assembly8. “回文串”是一个正读和反读都一样的字符串,比如“eye”、“level”、“noon”等。请写一个程序测试一字符串是否是“回文”, 是“回文”则显示“Y”,否则显示“N”。 显示一个字符的子程序为:dispc,入口参数:AL=要显示个字符的SACII码。; 算法描述:; left,right分别指向串的第一个和最后一个元素,采用首尾比较。; 1. left=0,right=n-1 ,flag='Y' ;; 2. while left<right do;
21、if aleft+<>aright- then ; flag= 'N' break; 3. printf flag; 合理分配寄存器:left=esi,right=edi ,flag=al='Y'include io32.inc.data msg byte 'level',0 count equ lengthof msg.codemainprocmovesi,0 ;left指针movedi,count-2 ;right指针,串长不包括结束标志0moval,'Y' ; flag=al='Y'.while(
22、esi<edi);循环入口,注意:两个内存变量不能比较!movah,msgesi ; left=esi cmpah,msgedi ; right=edijenextmoval,'N'jmpdisplaynext: incesi ;移动指针指向下一个元素decedi ;移动指针指向下一个元素.endwdisplay: call dispc call dispcrlf ret;return to Windowsmain endp ;end of mainend main ;end of assembly9. 回文是指正读和反读都一样的数或文本。例如:11、121、12321等
23、,编写程序,求10到10000之间所有回文数并输出。显示一个无符号数的子程序为:dispuid,入口参数:EAX=要显示无符号数的值。include io32.inc.datablank byte ' ',0char byte ?anykey byte 13,10,'press any key to continue.',0.codemain procmov ecx,10mov ebx,ecx.repeatxor esi,esimov eax,ecx.while(eax>0)xor edx,edxdiv ebximul esi,10add esi,edx.
24、endwcmp esi,ecxjne nextmov eax,ecxcall dispuid call dispcrlfnext: inc ecx.until(ecx>10000)retmain endpend main10. 编程写一个名为Prime的子程序,用于测试一个整数是否是素数,主子程序间的参数传递通过堆栈完成。调用Prime子程序求出2100之间的所有素数,并将它们存入Parray数组中,素数的个数存入变量Pcounter中。; 采用伪代码pseudo code描述算法; 1. i=2 to 100 do; 1.1 if prime(i) then print i; 构造函数
25、prime(i):; if i is prime number then prime(i)=1 else prime(i)=0; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除数,ecx做除数.include io32.inc.datamsgbyte' List of prime number',13,10,0 msg1 byte ' pcounter is :' ,13,10,0blankbyte' ',0 anyKey byte 13,10,'press any key to continue .',13,10,0
26、 parray dword 100 dup(?)pcounter dword 0.codemainproc ;主程序开始mov esi,0moveax,offset msgcall dispmsgmovebx,2 ;i循环的初值 .while (ebx<=100) ;i循环入口 pushebx ;push parameter on stack参数进栈 callprime testeax,eax jznext l1: movparrayesi*4,ebxincesimoveax,ebx calldispuid;输出,用于验证。可以删掉 moveax,offset blank calldis
27、pmsgnext: incebx .endw call dispcrlf moveax,offset msg1call dispmsg movpcounter,esi moveax,esi call dispuidcall dispcrlf ret;返回操作系统mainendp ;主程序结束; function: 判断一个无符号整数i是否是素数; Receives: 从栈获取无符号整数i; Returns: if i is prime number then eax=1 else eax=0; 采用伪代码pseudo code描述算法; 1 j=2 to i/2 do; 1.1 if i mo
28、d j=0 then eax=0 return; 2 eax=1;return; 合理分配寄存器,j=ecx,edxeax做被除数,ecx做除数.primeprocpushebp;save ebpmovebp,esppushecxpushedxpushedi pushesi ;save ,ecx,edx,edi,esimovesi,ebp+8;get parameter imovedi,esishresi,1 ;esi=i/2movecx,2 ;j循环入口.while (ecx<=esi)moveax,edixoredx,edx ;edx=0 divecx ;被除数送eax,32位除法
29、oredx,edx ;cmp edx,0jzretZero ;if i mod j=0 then eax=0incecx.endwmoveax,1;i是否是素数eax=1jmprestoreRegretZero:moveax,0restoreReg:popesipopedipopedxpopecxpopebpret1*4;clean up stackprimeendpend main ;end of assembly11. 编程写一个名为Gcd的求两个数最大公约数子程序,主子程序间的参数传递通过堆栈完成。调用Gcd子程序求出三个双自变量:dvar1、dvar2与dvar3的最大公约数并输出。显
30、示一个无符号数的子程序为:dispuid,入口参数:EAX=要显示无符号数的值。include io32.inc.data dvar1 dword2012 dvar2 dword128 dvar3 dword456 dgcd dword? ;存放2个无符号整数的最大公约数 fmtStr byte ' gcd(%d,%d,%d)=%d',13,10,0.codemainproc push dvar1 push dvar2 ;参数dvar1,dvar2进栈,传值 pushoffsetdgcd ;dgcd的地址进栈,传地址 callGcd ;dgcd=dvar1,dvar2的最大公约
31、数 push dvar3 push dgcd ;参数dvar3,dgcd进栈,传值 push offset dgcd ;dgcd的地址进栈,传地址 call Gcd invoke printf,offset fmtStr,dvar1,dvar2,dvar3,dgcd ret;return to Windowsmainendp ;end of mainGcd proc; function: 求2个无符号整数的最大公约数。; Receives: 从栈中获取无符号整数a,b及 存放最大公约数变量gcd的地址; Returns: gcd=无符号整数a,b的最大公约数。; 算法描述:; 1. while
32、 b<>0 do; r=a mod b;a=b;b=r; 2. gcd=a; 注意合理的分配使用寄存器,edx,eax做被除数; ebx=gcd的地址,eax=a,ecx=b pushebp movebp,esp ; 建立访问栈参数的指针基准pusheax ; 保护子程序中要使用的寄存器pushebxpushecx pushedx movebx,ebp+8 ;ebx=变量gcd的地址 movecx,ebp+12 ;ecx=b moveax,ebp+16 ;eax=a .while (ecx!=0) xoredx,edx ;被除数送edx,eax,32位除法 divecx;div指令
33、执行后eax=商,edx=余数 moveax,ecx ;a=b movecx,edx ;b=r,edx=余数 .endw movebx,eax ;gcd=最大公约数restore: popedx ; 恢复子程序中使用过的寄存器 popecx popebx popeax popebp ret 3*4 ;清理栈中的参数Gcd endp ;end of Gcdend main ;end of assembly12. 在一个已知长度的字符串中查找是否包含“BUG”子字符串。如果存在,显示“Y”,否则显示“N”。 显示一个字符的子程序为:dispc,入口参数:AL=要显示个字符的SACII码。inclu
34、de io32.inc.datastring byte 'If you find any error in the program, you can DEBUG it.' count = sizeof string bug byte 'BUG' .code start: mov ecx,count mov edi,offset string L1: mov esi,offset bug push edi mov edx,sizeof bug LN: mov al,esi cmp edi,al jne L2 inc esi inc edi dec edx jne
35、LN pop edi mov al,'Y' jmp L3 L2: pop edi inc edi loop L1 mov al,'N' L3: call dispc exit 0 end start13. 已知一个字符串的长度,剔除其中所有的空格字符。请从字符串最后一个字符开始逐个向前判断、并进行处理。include io32.inc.datastring byte 'Let us have a try !',0dh,0ah,0 .codestart:mov ecx,sizeof string cmp ecx,2 jb done lea eax,
36、string ; 显示处理前的字符串 call dispmsg mov esi,ecx dec esi outlp: cmp stringesi,' ' ; 检测是否是空格 jnz next ; 不是空格继续循环 mov edi,esi ; 是空格,进入剔除空格分支 dec ecx inlp: inc edi mov al,stringedi ; 前移一个位置 mov stringedi-1,al cmp edi,ecx jb inlp next: dec esi ; 继续进行 cmp esi,0 jnz outlp ; 为0结束 lea eax,string ; 显示处理后的
37、字符串 call dispmsg done: exit 0 end start14. 编写一子程序,将一个32位二进制数用8位十六进制形式在屏幕上显示出来。采用堆栈方法传递这个32位二进制数,并写主程序验证它。显示一个字符的子程序为:dispc,入口参数:AL=要显示个字符的SACII码。.include io32.inc.datawvar dword 307281AFH .code start:push wvar call disp add esp,4 mov al,'H' call dispc disp proc push ebp mov ebp,esp push ebx push ecx mov ecx,8 mov eax,ebp+8 dhw1: rol eax,4 mov ebx,eax and al,0fh ; 转换为ASCII码 add al,30h cmp al
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 项目5课后习题
- 白云石原矿买卖合同
- 消防档案管理制度
- 塑料成型工程技术-李洪飞03塑料成型工程技术
- 医院工程管理制度
- 消防水炮系统操作和维护保养规程
- 工地农民工权益保障制度
- 网络工程师选择题及答案
- 2026年心理咨询师基础知识考试题库及答案(浓缩50题)
- 2025年桂林市雁山区网格员招聘考试试题及答案解析
- 医务人员反歧视课件培训
- 碳达峰目标下工业企业减排路径与绿色转型发展研究答辩
- 罗森加盟合同范本
- 2026届高三生物二轮复习教学策略及尖优生精准辅导策略
- 《社会认知:从大脑到文化》阅读记录
- 《高级育婴员》职业资格通关500题(标准答案版)
- 超纯水设备培训
- 2017-2022年近6年全国卷高考物理真题分类汇编:热力学定律(含答案)
- 销售漏斗课件
- 展览搭建中重点与难点分析及解决策略
- 汛期车辆安全教育培训课件
评论
0/150
提交评论