当前位置:文档之家› 微机原理程序题

微机原理程序题

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< =’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<=100)mov eax,ecxxor 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; {Lyear[esi]=ecx;esi++;}; ecx++;; }; Lcounter=esi;include io32.inc.dataLyear dword 100 dup(?)Lcounter dword 0.codemainprocxor esi,esi ;esi闰年个数计数器,兼做Lyear下标。

mov ecx,2012 ;ecx年份计数器。

.while (ecx<2100)mov eax,ecxxor edx,edxmov ebx,400div ebxcmp edx,0jz leap ;if year mod 400=0 then goto leapmov eax,ecxxor edx,edxmov ebx,4div ebxcmp edx,0jnz next ;if year mod 4<>0 then goto nextmov eax,ecxxor edx,edxmov ebx,100div ebxcmp edx,0jz next ;if year mod 100=0 then goto nextleap: mov Lyear[esi*4],ecxinc esimov eax,ecxcall dispuid ;输出,用于验证。

可以删掉call dispcrlf ;输出,用于验证。

可以删掉next: inc ecx.endwmov Lcounter,esimov eax,esicall d ispuid ;输出,用于验证。

可以删掉call d ispcrlf ;输出,用于验证。

可以删掉retmainendp ;end of mainend main ;end of assembly2. 编程写一个完整的程序,求出2~100之间的所有素数,并将它们存入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.datamsg byte ' List of prime number',13,10,0msg1 byte ' Lcounter is :' ,13,10,0blank byte ' ',0prime dword 100 dup(?)pcounter dword 0.codemainproc ;主程序开始mov esi,0mov eax,offset msgcall d ispmsgmov ebx,2iLoop: cmp ebx,100 ;i循环入口ja donemov ecx,ebxshr ecx,1 ;j=i/2jLoop: cmp ecx,2 ;j循环入口jb printmov eax,ebxcdq ;xor edx,edxdiv ecx ;被除数送eax,32位除法or edx,edx ;cmp edx,0jz nexti ;if i mod j=0 then goto next idec ecxjmp jLoopprint: mov prime[esi*4],ebxinc esimov eax,ebxmov eax,offset blankcall d ispmsg ;显示空格nexti: inc ebx ;i=i+1jmp iLoopdone: call dispcrlfmov eax,offset msg1call d ispmsgmov pcounter,esimov eax,esicall dispuidcall dispcrlfret ;返回操作系统mainendp ;主程序结束end main ;end of assembly3. 编程写一个完整的程序,将数组aray中的元素按逆序存放,要求程序中附加的变量最少。

数据段的定义如下:.dataaray dword 12,4, 168,122,-33,56,78,99,345, 66,-5; 采用伪代码pseudo code描述算法; 1. i=n-1 downto 1 do; 1.1 j=0 to i-1; 1.1.1 if a[j]>a[j+1] then swap; 合理分配寄存器,i=ecx,j=edx ,i-1=ecx-1include io32.inc.data ;set data segmentblank3 byte 3 dup(20h),0array dword 12,4,-168,122,33,56,78,99,345,-66,-5char byte ?msg byte 13,10,'press any key to continue ...',0 ;字符串.codemainprocmov ecx,(lengthof array)-1 ;计数循环的初值iLoop: ;i循环入口dec ecx ;ecx=i-1xor edx,edxjLoop: ;j循环入口,循环控制变量edxcmp edx,ecxjg nextimov eax,array[edx*4]cmp eax,array[edx*4+4]jge nextjxchg eax,array[edx*4+4]mov array[edx*4],eaxnextj: inc edxjmp jLoopnexti: inc ecx ;retrieve ecxloop iLoopprint: xor ecx,ecxagain: cmp ecx,lengthof array-1jg donemov eax,array[ecx*4]call dispsidmov eax,offset blank3 ;显示空格call dispmsginc ecxjmp againdone: mov eax,offset msg ;显示:press any key to continuecall dispmsgmov eax,offset char ;暂停,等待按任意键call readcret ;返回操作系统mainendpend main ;end of assembly4. 编程写一个完整的程序,求数组aray中的最大值与最小值,并将它们分别存入max和min 元中。

数据段的定义如下:.dataaray dword 12,4,-168,122,-33,56,78,99,345,-66,-5min dword ?max dword ?include io32.inc.dataaray dword 12,4,-168,122,-33,56,78,99,345,-66,-5min dword ?max dword ?prompt byte 'Enter an integers :',0maxStr byte 'max=%d',13,10,0 ;显示字符串的格式描述串minStr byte 'min=%d',13,10,0main procmov ecx,lengthof ara -1mov eax,ara[0] ;eax:maxmov ebx,eax ;ebx,minmov esi,1again: cmp eax,ara[esi*4]jge smallmov eax,ara[esi*4]small: cmp ebx,ara[esi*4]jle nextmov ebx,ara[esi*4]next: inc esiloop againmov max,eaxmov min,ebxret ;返回操作系统main endpend main5. 编程写一个完整的程序统计msg中的空格的个数与小写字母的个数,并分别将它们存入space单元与char单元中。

数据段的定义如下:.datamsg byte 'I love XUT !',13,10,0space dword ?char dword ?include io32.inc.datamsg byte 'I love XUT!',13,10,0space dword ?char dword ?.codemain procmov ebx,0xor edi,edimov esi,edil1: mov al,msg[ebx]cmp al,20hjnz done1cmp al,0jz nextinc ediinc ebxjmp l1done1: cmp al,'a'cmp al,'z'ja done2cmp al,0jz nextinc esiinc ebxjmp l1done2: cmp al,0jz nextinc ebxjmp l1next: mov space,edimov eax,spacecall dispuidcall dispcrlfmov char,esimov eax,charcall dispuidcall dispcrlfretmain endpend main6. 编程写一个完整的程序,将字符串msg中所有的小写字母转换为大写字母。

相关主题