微機(jī)原理程序題_第1頁
微機(jī)原理程序題_第2頁
微機(jī)原理程序題_第3頁
微機(jī)原理程序題_第4頁
微機(jī)原理程序題_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上1. 將下面C語言程序的代碼片段轉(zhuǎn)換為功能等價(jià)的匯編語言代碼片段,其中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語言程序的代碼片段轉(zhuǎn)換為功能等價(jià)的匯編語言代碼片段,其中ch1與caps均為字節(jié)變量。if (ch1>

2、 =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語言程序的代碼片段轉(zhuǎn)換為功能等價(jià)的匯編語言代碼片段,其中sum與i變量均為雙字變量。sum=0;for ( i=1;i< =100;i+)if ( i%2= =0) sum=sum+i;mov ecx,imov ec

3、x,1.while(ecx<=100)mov eax,ecx xor edx,edxmov ebx,2div ebxcmp edx,0jnz nextadd sum,ecxnext:inc ecx.endw1. 能被4整除但不能被100整除,或者年被400整除的年份是閏年。編程寫一個(gè)完整的程序,求出2012年2099年中的所有閏年年份,并把它們存放在數(shù)組Lyear中。算法描述; esi=0;ecx=2012; while (ecx<2100); if (year mod 4=0 and year mod 100 <>0) or (year mod 400=0) then

4、; Lyearesi=ecx;esi+; ecx+; ; Lcounter=esi;include io32.inc.data Lyear dword 100 dup(?)Lcounter dword 0.codemainproc xoresi,esi ;esi閏年個(gè)數(shù)計(jì)數(shù)器,兼做Lyear下標(biāo)。 movecx,2012 ;ecx年份計(jì)數(shù)器。 .while (ecx<2100) moveax,ecxxoredx,edxmovebx,400divebxcmpedx,0jzleap;if year mod 400=0 then goto leap moveax,ecx xoredx,edx

5、movebx,4 divebx 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;輸出,用于驗(yàn)證。可以刪掉calldispcrlf ;輸出,用于驗(yàn)證。可以刪掉next:incecx .endw movLcounter,esi moveax,esi calldi

6、spuid ;輸出,用于驗(yàn)證??梢詣h掉 calldispcrlf ;輸出,用于驗(yàn)證。可以刪掉 retmainendp ;end of mainend main ;end of assembly2. 編程寫一個(gè)完整的程序,求出2100之間的所有素?cái)?shù),并將它們存入Prime數(shù)組中,素?cái)?shù)的個(gè)數(shù)存入變量Pcounter中。; 采用偽代碼pseudo code描述算法; 1. i=2 to 100 do; 1.1 if i is prime number then print i; 細(xì)化 1.1 如下:; 1.1 j=2 to i/2 do; 1.1.1 if i mod j=0 then goto n

7、ext i; 1.1.2 print i; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除數(shù),ecx做除數(shù).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,2iLoo

8、p:cmpebx,100 ;i循環(huán)入口jadonemovecx,ebxshrecx,1 ;j=i/2jLoop:cmpecx,2 ;j循環(huán)入口jbprintmoveax,ebxcdq;xor edx,edxdivecx;被除數(shù)送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

9、:calldispcrlf moveax,offset msg1call dispmsg movpcounter,esi moveax,esi call dispuidcalldispcrlf ret;返回操作系統(tǒng)mainendp ;主程序結(jié)束end main ;end of assembly3. 編程寫一個(gè)完整的程序,將數(shù)組aray中的元素按逆序存放,要求程序中附加的變量最少。數(shù)據(jù)段的定義如下:.data aray dword 12,4, 168,122,33,56,78,99,345, 66,5; 采用偽代碼pseudo code描述算法; 1. i=n-1 downto 1 do; 1.

10、1j=0 to i-1; 1.1.1 if aj>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;計(jì)數(shù)循環(huán)

11、的初值iLoop: ;i循環(huán)入口 dececx;ecx=i-1 xoredx,edxjLoop:;j循環(huán)入口,循環(huán)控制變量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*

12、4 calldispsid mov eax,offset blank3;顯示空格 call dispmsg incecx jmpagaindone: mov eax,offset msg;顯示:press any key to continue call dispmsg mov eax,offset char ;暫停,等待按任意鍵 call readcret ;返回操作系統(tǒng)mainendpend main ;end of assembly4. 編程寫一個(gè)完整的程序,求數(shù)組aray中的最大值與最小值,并將它們分別存入max和min元中。數(shù)據(jù)段的定義如下:.data aray dword 12,4

13、,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 ? promptbyte'Enter an integers :',0 maxStrbyte'max=%d',13,10,0 ;顯示字符串的格式描述串 minStrbyte'min=%d',13,10,0.code mainprocmovecx,lengthof ar

14、a -1moveax,ara0;eax:maxmovebx,eax ;ebx,minmovesi,1again:cmpeax,araesi*4jgesmallmoveax,araesi*4small:cmpebx,araesi*4jlenextmovebx,araesi*4next:incesiloopagain movmax,eaxmovmin,ebxret ;返回操作系統(tǒng) mainendpend main 5. 編程寫一個(gè)完整的程序統(tǒng)計(jì)msg中的空格的個(gè)數(shù)與小寫字母的個(gè)數(shù),并分別將它們存入space單元與char單元中。數(shù)據(jù)段的定義如下:.data msg byte 'I love

15、 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,'a'jb done2cmp al,'z'j

16、a 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. 編程寫一個(gè)完整的程序,將字符串msg中所有的小寫字母轉(zhuǎn)換為大寫字母。數(shù)據(jù)段的定義如下:.data msg byte 'I love XUT !',13,10,0inclu

17、de io32.inc.datamsg byte '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是一無符號(hào)數(shù)數(shù)組,數(shù)據(jù)段的定義如下。要求:編程寫一個(gè)完整的程序求出數(shù)組元素中偶數(shù)的和,并將它存入esum單元中。.data arra

18、y 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; 判斷偶數(shù),采用 test 指令測(cè)試最低位。; 合理分配寄存器:采用loop循環(huán),ecx=lengthof array esum=eax=0,esi=0,做下標(biāo),采用帶比例因子的相對(duì)尋址處理數(shù)組。include io32.inc.data array dword12,34,123,78,43,234,79,86,98,20 esum dw

19、ord ? fmtStr byte' esum=%d',13,10,0 ;格式描述串 .codemain procmovecx,lengthof arrayxor eax,eax ;esum=eax=0movesi,eax ;數(shù)組下標(biāo)again: movebx,arrayesi*4 testebx,1jnznext ;if ai is evennunber then esum=esum+ai; addeax,ebx ;注意:轉(zhuǎn)換成匯編語言時(shí),測(cè)試的是不是偶數(shù)時(shí)則取下一個(gè)數(shù)測(cè)試。next:incesiloopagain movesum,eaxinvoke printf,offse

20、t fmtStr,esum ret;return to Windowsmain endp ;end of mainend main ;end of assembly8. “回文串”是一個(gè)正讀和反讀都一樣的字符串,比如“eye”、“l(fā)evel”、“noon”等。請(qǐng)寫一個(gè)程序測(cè)試一字符串是否是“回文”, 是“回文”則顯示“Y”,否則顯示“N”。 顯示一個(gè)字符的子程序?yàn)椋篸ispc,入口參數(shù):AL=要顯示個(gè)字符的SACII碼。; 算法描述:; left,right分別指向串的第一個(gè)和最后一個(gè)元素,采用首尾比較。; 1. left=0,right=n-1 ,flag='Y' ;; 2.

21、 while left<right do; 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指針,串長不包括結(jié)束標(biāo)志0moval,'Y' ; f

22、lag=al='Y'.while(esi<edi);循環(huán)入口,注意:兩個(gè)內(nèi)存變量不能比較!movah,msgesi ; left=esi cmpah,msgedi ; right=edijenextmoval,'N'jmpdisplaynext: incesi ;移動(dòng)指針指向下一個(gè)元素decedi ;移動(dòng)指針指向下一個(gè)元素.endwdisplay: call dispc call dispcrlf ret;return to Windowsmain endp ;end of mainend main ;end of assembly9. 回文是指正讀和反讀

23、都一樣的數(shù)或文本。例如:11、121、12321等,編寫程序,求10到10000之間所有回文數(shù)并輸出。顯示一個(gè)無符號(hào)數(shù)的子程序?yàn)椋篸ispuid,入口參數(shù):EAX=要顯示無符號(hào)數(shù)的值。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 e

24、bximul esi,10add esi,edx.endwcmp esi,ecxjne nextmov eax,ecxcall dispuid call dispcrlfnext: inc ecx.until(ecx>10000)retmain endpend main10. 編程寫一個(gè)名為Prime的子程序,用于測(cè)試一個(gè)整數(shù)是否是素?cái)?shù),主子程序間的參數(shù)傳遞通過堆棧完成。調(diào)用Prime子程序求出2100之間的所有素?cái)?shù),并將它們存入Parray數(shù)組中,素?cái)?shù)的個(gè)數(shù)存入變量Pcounter中。; 采用偽代碼pseudo code描述算法; 1. i=2 to 100 do; 1.1 if pr

25、ime(i) then print i; 構(gòu)造函數(shù)prime(i):; if i is prime number then prime(i)=1 else prime(i)=0; 合理分配寄存器,i=ebx,j=ecx,edxeax做被除數(shù),ecx做除數(shù).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 t

26、o continue .',13,10,0 parray dword 100 dup(?)pcounter dword 0.codemainproc ;主程序開始mov esi,0moveax,offset msgcall dispmsgmovebx,2 ;i循環(huán)的初值 .while (ebx<=100) ;i循環(huán)入口 pushebx ;push parameter on stack參數(shù)進(jìn)棧 callprime testeax,eax jznext l1: movparrayesi*4,ebxincesimoveax,ebx calldispuid;輸出,用于驗(yàn)證。可以刪掉 mo

27、veax,offset blank calldispmsgnext: incebx .endw call dispcrlf moveax,offset msg1call dispmsg movpcounter,esi moveax,esi call dispuidcall dispcrlf ret;返回操作系統(tǒng)mainendp ;主程序結(jié)束; function: 判斷一個(gè)無符號(hào)整數(shù)i是否是素?cái)?shù); Receives: 從棧獲取無符號(hào)整數(shù)i; Returns: if i is prime number then eax=1 else eax=0; 采用偽代碼pseudo code描述算法; 1 j

28、=2 to i/2 do; 1.1 if i mod j=0 then eax=0 return; 2 eax=1;return; 合理分配寄存器,j=ecx,edxeax做被除數(shù),ecx做除數(shù).primeprocpushebp;save ebpmovebp,esppushecxpushedxpushedi pushesi ;save ,ecx,edx,edi,esimovesi,ebp+8;get parameter imovedi,esishresi,1 ;esi=i/2movecx,2 ;j循環(huán)入口.while (ecx<=esi)moveax,edixoredx,edx ;edx

29、=0 divecx ;被除數(shù)送eax,32位除法 oredx,edx ;cmp edx,0jzretZero ;if i mod j=0 then eax=0incecx.endwmoveax,1;i是否是素?cái)?shù)eax=1jmprestoreRegretZero:moveax,0restoreReg:popesipopedipopedxpopecxpopebpret1*4;clean up stackprimeendpend main ;end of assembly11. 編程寫一個(gè)名為Gcd的求兩個(gè)數(shù)最大公約數(shù)子程序,主子程序間的參數(shù)傳遞通過堆棧完成。調(diào)用Gcd子程序求出三個(gè)雙自變量:dva

30、r1、dvar2與dvar3的最大公約數(shù)并輸出。顯示一個(gè)無符號(hào)數(shù)的子程序?yàn)椋篸ispuid,入口參數(shù):EAX=要顯示無符號(hào)數(shù)的值。include io32.inc.data dvar1 dword2012 dvar2 dword128 dvar3 dword456 dgcd dword? ;存放2個(gè)無符號(hào)整數(shù)的最大公約數(shù) fmtStr byte ' gcd(%d,%d,%d)=%d',13,10,0.codemainproc push dvar1 push dvar2 ;參數(shù)dvar1,dvar2進(jìn)棧,傳值 pushoffsetdgcd ;dgcd的地址進(jìn)棧,傳地址 callG

31、cd ;dgcd=dvar1,dvar2的最大公約數(shù) push dvar3 push dgcd ;參數(shù)dvar3,dgcd進(jìn)棧,傳值 push offset dgcd ;dgcd的地址進(jìn)棧,傳地址 call Gcd invoke printf,offset fmtStr,dvar1,dvar2,dvar3,dgcd ret;return to Windowsmainendp ;end of mainGcd proc; function: 求2個(gè)無符號(hào)整數(shù)的最大公約數(shù)。; Receives: 從棧中獲取無符號(hào)整數(shù)a,b及 存放最大公約數(shù)變量gcd的地址; Returns: gcd=無符號(hào)整數(shù)a,

32、b的最大公約數(shù)。; 算法描述:; 1. while b<>0 do; r=a mod b;a=b;b=r; 2. gcd=a; 注意合理的分配使用寄存器,edx,eax做被除數(shù); ebx=gcd的地址,eax=a,ecx=b pushebp movebp,esp ; 建立訪問棧參數(shù)的指針基準(zhǔn)pusheax ; 保護(hù)子程序中要使用的寄存器pushebxpushecx pushedx movebx,ebp+8 ;ebx=變量gcd的地址 movecx,ebp+12 ;ecx=b moveax,ebp+16 ;eax=a .while (ecx!=0) xoredx,edx ;被除數(shù)送e

33、dx,eax,32位除法 divecx;div指令執(zhí)行后eax=商,edx=余數(shù) moveax,ecx ;a=b movecx,edx ;b=r,edx=余數(shù) .endw movebx,eax ;gcd=最大公約數(shù)restore: popedx ; 恢復(fù)子程序中使用過的寄存器 popecx popebx popeax popebp ret 3*4 ;清理?xiàng)V械膮?shù)Gcd endp ;end of Gcdend main ;end of assembly12. 在一個(gè)已知長度的字符串中查找是否包含“BUG”子字符串。如果存在,顯示“Y”,否則顯示“N”。 顯示一個(gè)字符的子程序?yàn)椋篸ispc,入口

34、參數(shù):AL=要顯示個(gè)字符的SACII碼。include 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

35、 esi inc edi dec edx jne 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. 已知一個(gè)字符串的長度,剔除其中所有的空格字符。請(qǐng)從字符串最后一個(gè)字符開始逐個(gè)向前判斷、并進(jìn)行處理。include io32.inc.datastring byte 'Let us have a try !',0dh,0ah,0 .codestart:mov ecx,sizeof string c

36、mp ecx,2 jb done lea eax,string ; 顯示處理前的字符串 call dispmsg mov esi,ecx dec esi outlp: cmp stringesi,' ' ; 檢測(cè)是否是空格 jnz next ; 不是空格繼續(xù)循環(huán) mov edi,esi ; 是空格,進(jìn)入剔除空格分支 dec ecx inlp: inc edi mov al,stringedi ; 前移一個(gè)位置 mov stringedi-1,al cmp edi,ecx jb inlp next: dec esi ; 繼續(xù)進(jìn)行 cmp esi,0 jnz outlp ; 為0結(jié)

37、束 lea eax,string ; 顯示處理后的字符串 call dispmsg done: exit 0 end start14. 編寫一子程序,將一個(gè)32位二進(jìn)制數(shù)用8位十六進(jìn)制形式在屏幕上顯示出來。采用堆棧方法傳遞這個(gè)32位二進(jìn)制數(shù),并寫主程序驗(yàn)證它。顯示一個(gè)字符的子程序?yàn)椋篸ispc,入口參數(shù):AL=要顯示個(gè)字符的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 ; 轉(zhuǎn)換為ASCII碼 add al,30h cmp al,'

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論