《微机原理程序题(共17页).doc》由会员分享,可在线阅读,更多相关《微机原理程序题(共17页).doc(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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 & ch1
2、 =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=100)mov eax,ecx xor edx,edxmov ebx,2div ebxcmp edx
3、,0jnz nextadd sum,ecxnext:inc ecx.endw1. 能被4整除但不能被100整除,或者年被400整除的年份是闰年。编程写一个完整的程序,求出2012年2099年中的所有闰年年份,并把它们存放在数组Lyear中。算法描述; esi=0;ecx=2012; while (ecx2100); if (year mod 4=0 and year mod 100 0) or (year mod 400=0) then; Lyearesi=ecx;esi+; ecx+; ; Lcounter=esi;include io32.inc.data Lyear dword 100
4、dup(?)Lcounter dword 0.codemainproc xoresi,esi ;esi闰年个数计数器,兼做Lyear下标。 movecx,2012 ;ecx年份计数器。 .while (ecx2100) moveax,ecxxoredx,edxmovebx,400divebxcmpedx,0jzleap;if year mod 400=0 then goto leap moveax,ecx xoredx,edx movebx,4 divebx cmpedx,0 jnznext;if year mod 40 then goto next moveax,ecx xoredx,edx
5、 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 ;输出,用于验证。可以删掉 calldispcrlf ;输出,用于验证。可以删掉 retmainendp ;end of mainend main ;end of asse
6、mbly2. 编程写一个完整的程序,求出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; 1.1.1 if i mod j=0 then goto next i; 1.1.2 print i; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除数,ecx做除数.include io32.inc.datamsgbyte Lis
7、t 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循环入口jadonemovecx,ebxshrecx,1 ;j=i/2jLoop:cmpecx,2 ;j循环入口jbprintmoveax,ebxcdq;xor edx,edxdivecx;被除数送eax,32位除法
8、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 moveax,offset msg1call dispmsg movpcounter,esi moveax,esi call dispuidcalldispcrlf ret;返回操作系统mainendp ;主程序结束e
9、nd 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; 1.1.1 if ajaj+1 then swap; 合理分配寄存器,i=ecx,j=edx ,i-1=ecx-1 include io32.inc.data ;set data segment blank3byte 3
10、 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;ecx=i-1 xoredx,edxjLoop:;j循环入口,循环控制变量edx cmpedx,ecx jgnexti moveax,arrayedx*4 cmpeax,arrayedx*4+4 jgenextj xchgea
11、x,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,offset blank3;显示空格 call dispmsg incecx jmpagaindone: mov eax,offset msg;显示:press any key to continue call dispmsg m
12、ov 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,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 d
13、word ? promptbyteEnter an integers :,0 maxStrbytemax=%d,13,10,0 ;显示字符串的格式描述串 minStrbytemin=%d,13,10,0.code mainprocmovecx,lengthof ara -1moveax,ara0;eax:maxmovebx,eax ;ebx,minmovesi,1again:cmpeax,araesi*4jgesmallmoveax,araesi*4small:cmpebx,araesi*4jlenextmovebx,araesi*4next:incesiloopagain movmax,ea
14、xmovmin,ebxret ;返回操作系统 mainendpend main 5. 编程写一个完整的程序统计msg中的空格的个数与小写字母的个数,并分别将它们存入space单元与char单元中。数据段的定义如下:.data msg byte I love XUT !,13,10,0 space 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
15、,msgebxcmp al,20hjnz done1cmp al,0jz nextinc ediinc ebxjmp l1done1: cmp al,ajb done2cmp al,zja done2cmp al,0jz nextinc 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.
16、编程写一个完整的程序,将字符串msg中所有的小写字母转换为大写字母。数据段的定义如下:.data msg byte I love XUT !,13,10,0include io32.inc.datamsg byte I love XUT!,13,10,0.codemain procmov ebx,0l1: mov al,msgebxcmp al,ajb donecmp al,zja donesub al,20hdone: cmp al,0jz done1inc ebx call dispcjmp l1done1:retmain endpend main7. array是一无符号数数组,数据段的
17、定义如下。要求:编程写一个完整的程序求出数组元素中偶数的和,并将它存入esum单元中。.data array dword 12,34,123,78,43,234,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.da
18、ta array dword12,34,123,78,43,234,79,86,98,20 esum dword ? fmtStr byte esum=%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:inc
19、esiloopagain movesum,eaxinvoke printf,offset fmtStr,esum ret;return to Windowsmain endp ;end of mainend main ;end of assembly8. “回文串”是一个正读和反读都一样的字符串,比如“eye”、“level”、“noon”等。请写一个程序测试一字符串是否是“回文”, 是“回文”则显示“Y”,否则显示“N”。 显示一个字符的子程序为:dispc,入口参数:AL=要显示个字符的SACII码。; 算法描述:; left,right分别指向串的第一个和最后一个元素,采用首尾比较。;
20、1. left=0,right=n-1 ,flag=Y ;; 2. while leftright do; if aleft+aright- then ; flag= N; break; 3. printf flag; 合理分配寄存器:left=esi,right=edi ,flag=al=Yinclude io32.inc.data msg byte level,0 count equ lengthof msg.codemainprocmovesi,0 ;left指针movedi,count-2 ;right指针,串长不包括结束标志0moval,Y ; flag=al=Y.while(esi
21、0)xor edx,edxdiv ebximul esi,10add esi,edx.endwcmp esi,ecxjne nextmov eax,ecxcall dispuid call dispcrlfnext: inc ecx.until(ecx10000)retmain endpend main10. 编程写一个名为Prime的子程序,用于测试一个整数是否是素数,主子程序间的参数传递通过堆栈完成。调用Prime子程序求出2100之间的所有素数,并将它们存入Parray数组中,素数的个数存入变量Pcounter中。; 采用伪代码pseudo code描述算法; 1. i=2 to 100
22、 do; 1.1 if prime(i) then print i; 构造函数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 p
23、array 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 calldispmsgne
24、xt: 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 mod j=0
25、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位除法 oredx,edx
26、;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的最大公约数并输出。显示一个无符号数的子程
27、序为: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的最大公约数 push dvar3 push dgc
28、d ;参数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 b0 do; r=a mod b;a=b
29、;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指令执行后eax=商,edx=余数 moveax,ecx ;a
30、=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码。include io32.inc.datastring byte I
31、f 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 LN pop edi mov al,Y jmp L3 L2: pop edi inc edi lo
32、op 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,string ; 显示处理前的字符串 call dispmsg mov esi,ecx dec esi outlp: cmp stringesi, ; 检测是
33、否是空格 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 ; 显示处理后的字符串 call dispmsg done: exit 0 end start14. 编写一子程序,将一个32位二进制数用8位十六进制形式在屏幕上显示出来。采用堆栈方法传递这个32
34、位二进制数,并写主程序验证它。显示一个字符的子程序为:dispc,入口参数:AL=要显示个字符的SACII码。.include io32.inc.datawvar dword AFH .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,9 jb
35、e dhw2 add al,7 dhw2: call dispc mov eax,ebx loop dhw1 pop ecx pop ebx pop ebp ret disp endp exit 0 end start15. 编程写一个名为Bubble的冒泡排序子程序,主子程序间的参数传递通过堆栈完成;并写主程序验证它。显示一个无符号数的子程序为:dispuid,入口参数:EAX=要显示无符号数的值。.include io32.incdata array dword 12,4,168,122,-33,56,78,99,345,66,-5 count equ lengthof array fmtStr byte %5d.codemain proc pushoffset array ;array的首地址进栈,传地址 push count ;数组元素的个数进栈,传值 call Bubble xor esi,esi ;以下的while循环用于验证。可以删掉 .while (esicount) pushad popad incesi .endw r