1、微机原理程序题教学资料微机原理程序题1. 将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中sign与sinteger均为双字变量。 if ( sinteger = = 0) sign = = 0; else If ( siteger 0) sign = 1; else sign = 1;mov eax,sintegermov edx,signcmp eax,0jnz L1mov ebx,0L1:cmp ebx,0jl L2mov ebx,1L2:mov ebx,-12. 将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中ch1与caps均为字节变量。 if (ch
2、1 =a & 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=100)mov eax,ecx xor edx,edxmov ebx,2d
3、iv ebxcmp edx,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 Lye
4、ar dword 100 dup(?) Lcounter dword 0.code main proc xor esi,esi ;esi闰年个数计数器,兼做Lyear下标。 mov ecx,2012 ;ecx年份计数器。 .while (ecx2100) mov eax,ecx xor edx,edx mov ebx,400 div ebx cmp edx,0 jz leap ;if year mod 400=0 then goto leap mov eax,ecx xor edx,edx mov ebx,4 div ebx cmp edx,0 jnz next ;if year mod 40
5、 then goto next mov eax,ecx xor edx,edx mov ebx,100 div ebx cmp edx,0 jz next ;if year mod 100=0 then goto nextleap: mov Lyearesi*4,ecx inc esi mov eax,ecx call dispuid ;输出,用于验证。可以删掉 call dispcrlf ;输出,用于验证。可以删掉next: inc ecx .endw mov Lcounter,esi mov eax,esi call dispuid ;输出,用于验证。可以删掉 call dispcrlf
6、;输出,用于验证。可以删掉 ret main endp ;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; 1.1.1 if i mod j=0 then goto next i; 1.1.2 print i; 合理分配寄存器,i
7、=ebx,j=ecx,edxeax做被除数,ecx做除数.include io32.inc.data msg byte List of prime number,13,10,0 msg1 byte Lcounter is : ,13,10,0 blank byte ,0 prime dword 100 dup(?) pcounter dword 0.codemain proc ;主程序开始 mov esi,0 mov eax,offset msg call dispmsg mov ebx,2iLoop: cmp ebx,100 ;i循环入口 ja done mov ecx,ebx shr ec
8、x,1 ;j=i/2jLoop: cmp ecx,2 ;j循环入口 jb print mov eax,ebx cdq ;xor edx,edx div ecx ;被除数送eax,32位除法 or edx,edx ;cmp edx,0 jz nexti ;if i mod j=0 then goto next i dec ecx jmp jLoopprint: mov primeesi*4,ebx inc esi mov eax,ebx mov eax,offset blank call dispmsg ;显示空格nexti: inc ebx ;i=i+1 jmp iLoopdone: call
9、 dispcrlf mov eax,offset msg1 call dispmsg mov pcounter,esi mov eax,esi call dispuidcall dispcrlf ret ;返回操作系统main endp ;主程序结束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
10、; 1.1 j=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 blank3 byte 3 dup(20h),0 array dword 12,4,-168,122,33,56,78,99,345,-66,-5 char byte ? msg byte 13,10,press any key to continue .,0 ;字符串.codemain proc mov ecx,(lengthof array)-1 ;计数循环的初
11、值iLoop: ;i循环入口 dec ecx ;ecx=i-1 xor edx,edxjLoop: ;j循环入口,循环控制变量edx cmp edx,ecx jg nexti mov eax,arrayedx*4 cmp eax,arrayedx*4+4 jge nextj xchg eax,arrayedx*4+4 mov arrayedx*4,eaxnextj: inc edx jmp jLoopnexti: inc ecx ;retrieve ecx loop iLoopprint: xor ecx,ecx again: cmp ecx,lengthof array-1 jg done
12、mov eax,arrayecx*4 call dispsid mov eax,offset blank3 ;显示空格 call dispmsg inc ecx jmp againdone: mov eax,offset msg ;显示:press any key to continue call dispmsg mov eax,offset char ;暂停,等待按任意键 call readc ret ;返回操作系统main endp end main ;end of assembly4. 编程写一个完整的程序,求数组aray中的最大值与最小值,并将它们分别存入max和min元中。数据段的定
13、义如下: .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 dword ? prompt byte Enter an integers :,0 maxStr byte max=%d,13,10,0 ;显示字符串的格式描述串 minStr byte min=%d,13,10,0.code main proc mov ecx,length
14、of ara -1 mov eax,ara0 ;eax:max mov ebx,eax ;ebx,min mov esi,1again: cmp eax,araesi*4 jge small mov eax,araesi*4small: cmp ebx,araesi*4 jle next mov ebx,araesi*4next: inc esi loop again mov max,eax mov min,ebx ret ;返回操作系统 main endp end main 5. 编程写一个完整的程序统计msg中的空格的个数与小写字母的个数,并分别将它们存入space单元与char单元中。数
15、据段的定义如下: .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,msgebxcmp al,20hjnz done1cmp al,0jz nextinc ediinc ebxjmp l1done1: cmp al,ajb done2cmp al,zja don
16、e2cmp 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. 编程写一个完整的程序,将字符串msg中所有的小写字母转换为大写字母。数据段的定义如下: .data msg byte I love XUT !,13,10,0include io32.inc.da
17、tamsg 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是一无符号数数组,数据段的定义如下。要求:编程写一个完整的程序求出数组元素中偶数的和,并将它存入esum单元中。 .data array dword 12,34,123,78,43,234,79,86,98,20 es
18、um 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 dword 12,34,123,78,43,234,79,86,98,20 esum dword ? fmtStr byte esum=%d,13,10,0 ;格式描述串
19、.codemain proc mov ecx,lengthof array xor eax,eax ;esum=eax=0 mov esi,eax ;数组下标again: mov ebx,arrayesi*4 test ebx,1 jnz next ;if ai is evennunber then esum=esum+ai; add eax,ebx ;注意:转换成汇编语言时,测试的是不是偶数时则取下一个数测试。next: inc esi loop again mov esum,eax invoke printf,offset fmtStr,esum ret ;return to Window
20、smain 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 leftright do; if aleft+aright- then ;
21、 flag= N; break; 3. printf flag; 合理分配寄存器:left=esi,right=edi ,flag=al=Yinclude io32.inc.data msg byte level,0 count equ lengthof msg.codemain proc mov esi,0 ;left指针 mov edi,count-2 ;right指针,串长不包括结束标志0 mov al,Y ; flag=al=Y .while (esi0)xor edx,edxdiv ebximul esi,10add esi,edx.endwcmp esi,ecxjne nextmo
22、v eax,ecxcall dispuid call dispcrlfnext: inc ecx.until(ecx10000)retmain endpend main10. 编程写一个名为Prime的子程序,用于测试一个整数是否是素数,主子程序间的参数传递通过堆栈完成。调用Prime子程序求出2100之间的所有素数,并将它们存入Parray数组中,素数的个数存入变量Pcounter中。; 采用伪代码pseudo code描述算法; 1. i=2 to 100 do; 1.1 if prime(i) then print i; 构造函数prime(i):; if i is prime numb
23、er then prime(i)=1 else prime(i)=0; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除数,ecx做除数.include io32.inc.data msg byte List of prime number,13,10,0 msg1 byte pcounter is : ,13,10,0 blank byte ,0 anyKey byte 13,10,press any key to continue .,13,10,0 parray dword 100 dup(?) pcounter dword 0.codemain proc ;主程序开始 mov
24、 esi,0 mov eax,offset msg call dispmsg mov ebx,2 ;i循环的初值 .while (ebx=100) ;i循环入口 push ebx ;push parameter on stack参数进栈 call prime test eax,eax jz next l1: mov parrayesi*4,ebx inc esi mov eax,ebx call dispuid ;输出,用于验证。可以删掉 mov eax,offset blank call dispmsg next: inc ebx .endw call dispcrlf mov eax,of
25、fset msg1 call dispmsg mov pcounter,esi mov eax,esi call dispuidcall dispcrlf ret ;返回操作系统main endp ;主程序结束 ; 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 then eax=0 return; 2 eax=1;return;
26、 合理分配寄存器,j=ecx,edxeax做被除数,ecx做除数.prime proc push ebp ;save ebp mov ebp,esp push ecx push edx push edi push esi ;save ,ecx,edx,edi,esi mov esi,ebp+8 ;get parameter i mov edi,esi shr esi,1 ;esi=i/2 mov ecx,2 ;j循环入口 .while (ecx=esi) mov eax,edi xor edx,edx ;edx=0 div ecx ;被除数送eax,32位除法 or edx,edx ;cmp edx,0 jz retZero ;if i mod j=0 then eax=0 inc ecx .endw mov eax,1 ;i是否是素数eax=1 jmp restoreRegretZero: mov eax,0restoreReg: pop esi pop edi pop edx pop ecx
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1