微机原理程序题

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中sign与sinteger均为双字变量。if(sinteger==0)sign==0;elseIf(siteger0)sign=1;elsesign=-1;moveax,sintegermovedx,signcmpeax,0jnzL1movebx,0L1:cmpebx,0jlL2movebx,1L2:movebx,-12.将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中ch1与caps均为字节变量。if(ch1=’a’&&ch1=’z’)caps==0;if(ch1=’A’&&ch1=’Z’)caps==1;movax,ch1movbx,capscmpax,ajbnextcmpax,zjanextmovbx,0next:cmpax,Ajldonecmpax,Zjadonedone:3.将下面C语言程序的代码片段转换为功能等价的汇编语言代码片段,其中sum与i变量均为双字变量。sum=0;for(i=1;i=100;i++)if(i%2==0)sum=sum+i;movecx,imovecx,1.while(ecx=100)moveax,ecxxoredx,edxmovebx,2divebxcmpedx,0jnznextaddsum,ecxnext:incecx.endw1.能被4整除但不能被100整除,或者年被400整除的年份是闰年。编程写一个完整的程序,求出2012年~2099年中的所有闰年年份,并把它们存放在数组Lyear中。算法描述;esi=0;ecx=2012;;while(ecx2100);{if(yearmod4=0andyearmod1000)or(yearmod400=0)then;{Lyear[esi]=ecx;esi++;};ecx++;;};Lcounter=esi;includeio32.inc.dataLyeardword100dup(?)Lcounterdword0.codemainprocxoresi,esi;esi闰年个数计数器,兼做Lyear下标。movecx,2012;ecx年份计数器。.while(ecx2100)moveax,ecxxoredx,edxmovebx,400divebxcmpedx,0jzleap;ifyearmod400=0thengotoleapmoveax,ecxxoredx,edxmovebx,4divebxcmpedx,0jnznext;ifyearmod40thengotonextmoveax,ecxxoredx,edxmovebx,100divebxcmpedx,0jznext;ifyearmod100=0thengotonextleap:movLyear[esi*4],ecxincesimoveax,ecxcalldispuid;输出,用于验证。可以删掉calldispcrlf;输出,用于验证。可以删掉next:incecx.endwmovLcounter,esimoveax,esicalldispuid;输出,用于验证。可以删掉calldispcrlf;输出,用于验证。可以删掉retmainendp;endofmainendmain;endofassembly2.编程写一个完整的程序,求出2~100之间的所有素数,并将它们存入Prime数组中,素数的个数存入变量Pcounter中。;采用伪代码pseudocode描述算法;1.i=2to100do;1.1ifiisprimenumberthenprinti;细化1.1如下:;1.1j=2toi/2do;1.1.1ifimodj=0thengotonexti;1.1.2printi;合理分配寄存器,i=ebx,j=ecx,edxeax做被除数,ecx做除数.includeio32.inc.datamsgbyte'Listofprimenumber',13,10,0msg1byte'Lcounteris:',13,10,0blankbyte'',0primedword100dup(?)pcounterdword0.codemainproc;主程序开始movesi,0moveax,offsetmsgcalldispmsgmovebx,2iLoop:cmpebx,100;i循环入口jadonemovecx,ebxshrecx,1;j=i/2jLoop:cmpecx,2;j循环入口jbprintmoveax,ebxcdq;xoredx,edxdivecx;被除数送eax,32位除法oredx,edx;cmpedx,0jznexti;ifimodj=0thengotonextidececxjmpjLoopprint:movprime[esi*4],ebxincesimoveax,ebxmoveax,offsetblankcalldispmsg;显示空格nexti:incebx;i=i+1jmpiLoopdone:calldispcrlfmoveax,offsetmsg1calldispmsgmovpcounter,esimoveax,esicalldispuidcalldispcrlfret;返回操作系统mainendp;主程序结束endmain;endofassembly3.编程写一个完整的程序,将数组aray中的元素按逆序存放,要求程序中附加的变量最少。数据段的定义如下:.dataaraydword12,4,168,122,-33,56,78,99,345,66,-5;采用伪代码pseudocode描述算法;1.i=n-1downto1do;1.1j=0toi-1;1.1.1ifa[j]a[j+1]thenswap;合理分配寄存器,i=ecx,j=edx,i-1=ecx-1includeio32.inc.data;setdatasegmentblank3byte3dup(20h),0arraydword12,4,-168,122,33,56,78,99,345,-66,-5charbyte?msgbyte13,10,'pressanykeytocontinue...',0;字符串.codemainprocmovecx,(lengthofarray)-1;计数循环的初值iLoop:;i循环入口dececx;ecx=i-1xoredx,edxjLoop:;j循环入口,循环控制变量edxcmpedx,ecxjgnextimoveax,array[edx*4]cmpeax,array[edx*4+4]jgenextjxchgeax,array[edx*4+4]movarray[edx*4],eaxnextj:incedxjmpjLoopnexti:incecx;retrieveecxloopiLoopprint:xorecx,ecxagain:cmpecx,lengthofarray-1jgdonemoveax,array[ecx*4]calldispsidmoveax,offsetblank3;显示空格calldispmsgincecxjmpagaindone:moveax,offsetmsg;显示:pressanykeytocontinuecalldispmsgmoveax,offsetchar;暂停,等待按任意键callreadcret;返回操作系统mainendpendmain;endofassembly4.编程写一个完整的程序,求数组aray中的最大值与最小值,并将它们分别存入max和min元中。数据段的定义如下:.dataaraydword12,4,-168,122,-33,56,78,99,345,-66,-5mindword?maxdword?includeio32.inc.dataaraydword12,4,-168,122,-33,56,78,99,345,-66,-5mindword?maxdword?promptbyte'Enteranintegers:',0maxStrbyte'max=%d',13,10,0;显示字符串的格式描述串minStrbyte'min=%d',13,10,0.codemainprocmovecx,lengthofara-1moveax,ara[0];eax:maxmovebx,eax;ebx,minmovesi,1again:cmpeax,ara[esi*4]jgesmallmoveax,ara[esi*4]small:cmpebx,ara[esi*4]jlenextmovebx,ara[esi*4]next:incesiloopagainmovmax,eaxmovmin,ebxret;返回操作系统mainendpendmain5.编程写一个完整的程序统计msg中的空格的个数与小写字母的个数,并分别将它们存入space单元与char单元中。数据段的定义如下:.datamsgbyte'IloveXUT!',13,10,0spacedword?chardword?includeio32.inc.datamsgbyte'IloveXUT!',13,10,0spacedword?chardword?.codemainprocmovebx,0xoredi,edimovesi,edil1:moval,msg[ebx]cmpal,20hjnzdone1cmpal,0jznextincediincebxjmpl1done1:cmpal,'a'jbdone2cmpal,'z'jadone2cmpal,0jznextincesiincebxjmpl1done2:cmpal,0jznextincebxjmpl1next:movspace,edimoveax,spacecalldispuidcalldispcrlfmovchar,esimoveax,charcalldispuidcalldispcrlfretmainendpendmain6.编程写一个完整的程序,将字符串msg中所有的小写字母转换为大写字母。数据段的定义如下:.datamsgbyte'IloveXUT!',13,10,0includeio32.inc.datamsgbyte'IloveXUT!',13,10,0.codemainprocmovebx,0l1:moval,msg[ebx]cmpal,'a'jbdonecmpal,'z'jadonesubal,20hdone:cmpal,0jzdone1incebxcalldispcjmpl1done1:retmainendpendmain7.array是一无符号数数组,数据段的定义如下。要求:编程写一个完整的程序求出数组元素中偶数的和,并将它存入esum单元中。.dataarraydword12,34,123,78,43,234,79,86,98,20esumdword?算法描述:;1.esum=0;;2.fori=0ton-1do;ifa[i]isevennunberthenesum=esum+a[i];;判断偶数,采用test指令测试最低位。;合理分配寄存器:采用loop循环,ecx=lengthofarrayesum=eax=0,esi=0,做下标,采用带比例因子的相对寻址处理数组。includeio32.inc.dataarraydword12,34,123,78,43,234,79,86,98,20esumdword?fmtStrbyte'esum=%d',13,10,0;格式描述串.codemainprocmovecx,lengthofarrayxoreax,eax;esum=eax=0movesi,eax;数组下标again:movebx,array[esi*4]testebx,1jnznext;ifa[i]isevennunberthenesum=esum+a[i];addeax,ebx;注意:转换成汇编语言时,测试的是不是偶数时则取下一

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功