日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

汇编语言程序设计实验(六)——子程序设计

發(fā)布時(shí)間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 汇编语言程序设计实验(六)——子程序设计 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

    • 1.實(shí)驗(yàn)?zāi)康募皟?nèi)容
    • 2、實(shí)驗(yàn)內(nèi)容
      • 2.1 二進(jìn)制輸入輸出子程序
        • (1) 二進(jìn)制輸入子程序
        • (2) 二進(jìn)制顯示子程序
      • 2.2 主存區(qū)域數(shù)據(jù)顯示子程序
      • 2.3 十進(jìn)制數(shù)的輸入和顯示子程序
        • (1) 有符號(hào)十進(jìn)制數(shù)輸入子程序
        • (2) 有符號(hào)十進(jìn)制數(shù)顯示子程序
        • (3) 無(wú)符號(hào)十進(jìn)制數(shù)顯示子程序
        • (3) 無(wú)符號(hào)十進(jìn)制數(shù)輸入子程序

1.實(shí)驗(yàn)?zāi)康募皟?nèi)容

理解子程序結(jié)構(gòu)的特點(diǎn),熟悉子程序參數(shù)傳遞的方法,掌握子程序的編寫。

2、實(shí)驗(yàn)內(nèi)容

2.1 二進(jìn)制輸入輸出子程序

(1) 二進(jìn)制輸入子程序

編寫二進(jìn)制輸入子程序、以及驗(yàn)證子程序的主程序,并運(yùn)行正確。
代碼思路:(1)利用字符輸入子程序readc輸入一個(gè)字符,判斷是否是0或1,若合法則直接減去30h轉(zhuǎn)化為數(shù)值。
(2) 重復(fù)轉(zhuǎn)換字符的同時(shí),需要將前面一次得到的總數(shù)值左移一位,并與剛得到的新數(shù)值進(jìn)行相加。
(3) 如果輸入了非’‘0’‘或非’‘1’'數(shù)值或者數(shù)據(jù)位數(shù)超出邊界,則轉(zhuǎn)至報(bào)錯(cuò)提示。

include io32.inc .data count = 5 ;預(yù)留五個(gè)空間 array dword count dup(0) ;聲明數(shù)組 temp dword ? ;工具變量 .code start:mov ecx, countmov ebx, offset array ;初始化數(shù)組指針 again: call rdbd ;調(diào)用子程序,從鍵盤讀一個(gè)數(shù)據(jù)mov eax,temp ;獲得出口參數(shù)mov [ebx],eax ;存放到輸入緩沖區(qū)call dispbdadd ebx,4 ;數(shù)組指針移動(dòng)loop again exit 0rdbd proc ;二進(jìn)制輸入子程序、push eax ;出口參數(shù),共享工具變量temppush ebx push ecx rdbd1: xor ebx,ebx ;ebx用于存放二進(jìn)制結(jié)果mov ecx,32 ;限制輸入字符的個(gè)數(shù)到32個(gè) rdbd2: call readc ;輸入一個(gè)字符cmp al,'0' ;0小,轉(zhuǎn)至報(bào)錯(cuò)jb rderr cmp al,'1' ;1大,轉(zhuǎn)至報(bào)錯(cuò)ja rderrsub al,'0' ;獲得真正的數(shù)據(jù)shl ebx,1 ;前邊輸入后轉(zhuǎn)化為二進(jìn)制的數(shù)據(jù)左移一位再相加or bl,al ;bl和al相加loop rdbd2 ;循環(huán)鍵入字符mov temp,ebx ;把ebx的二進(jìn)制結(jié)果存放于temp返回call dispcrlfpop ecxpop ebxpop eaxret rderr: mov eax,offset errmsg ;顯示錯(cuò)誤信息call dispmsgjmp rdbd1 errmsg byte 0dh,0ah,'Inpput error,enter again:',0 rdbd endp ;子程序結(jié)束end start

運(yùn)行結(jié)果:

(2) 二進(jìn)制顯示子程序

編寫二進(jìn)制顯示子程序、以及驗(yàn)證子程序的主程序(教材習(xí)題5.8),并運(yùn)行正確。
代碼思路:將得到的二進(jìn)制數(shù)每次右移一位,通過(guò)判斷CF是否為1來(lái)分別處理,處理時(shí)加30H轉(zhuǎn)化為ACSII碼,后將每次得到的ACSII碼入棧,此時(shí)在原數(shù)據(jù)中每個(gè)位在棧中就是低位到高位了,輸出的時(shí)候依次從棧中彈出就是正確的順序。

include io32.inc.data array dword 10110101b,1010000101b,0001000100001000101b,1001010101010b writebuf byte 12 dup (0) ;顯示緩沖區(qū).code start:mov ecx, lengthof array ;數(shù)組長(zhǎng)度計(jì)數(shù)器mov ebx, 0 ;初始化數(shù)組指針 again: mov eax, array[ebx*4] ;逐個(gè)取出字符數(shù)組中元素call writecall dispcrlfinc ebx ;數(shù)組指針移動(dòng)loop againexit 0;子程序部分 write procpush ebx ;數(shù)組指針先入棧push ecx ;計(jì)數(shù)器再入棧push edx ;存放每個(gè)留在緩沖區(qū)的二進(jìn)制數(shù)的edx入棧mov ebx, offset writebuf ;字符內(nèi)部數(shù)組首指針,指向顯示緩沖區(qū) write1: mov ecx,32 ;每個(gè)二進(jìn)制數(shù)最多表示到32位push ecx ;結(jié)束標(biāo)志入棧mov edx,0 write2: cmp eax,0 ;移位結(jié)束標(biāo)志jz write3 ;所有有效數(shù)字位已經(jīng)右移完畢 shr eax,1 ;每次將最低位移出來(lái)到CFjnc write_0 ;若最低位為0jmp write_1 write_0: mov edx,0add edx,30h ;30h轉(zhuǎn)換為ACSII碼push edx ;該字符入棧jmp write2 write_1: mov edx,1add edx,30h ;30h轉(zhuǎn)換為ACSII碼push edx ;該字符入棧jmp write2 write3: pop edx ;對(duì)該數(shù)從高位到低位依次出棧cmp edx,ecx ;出棧結(jié)束判斷jz write4mov [ebx],dl ;將棧中彈出的值放入數(shù)組inc ebx ;數(shù)組指針移動(dòng) jmp write3 write4: mov byte ptr[ebx],0 ;給顯示內(nèi)容加上結(jié)尾標(biāo)志mov eax,offset writebuf ;打印緩沖區(qū)內(nèi)容call dispmsg pop edxpop ecxpop ebxret write endpend start

運(yùn)行結(jié)果:

2.2 主存區(qū)域數(shù)據(jù)顯示子程序

編寫逐個(gè)字節(jié)顯示主存區(qū)域數(shù)據(jù)的子程序和主程序(教材習(xí)題5.13),并運(yùn)行正確。
代碼思路:利用十六進(jìn)制字節(jié)顯示程序,將主存區(qū)域的數(shù)據(jù)(如字符數(shù)組)按字節(jié)從低位(字符數(shù)組首元素)到高位進(jìn)行輸出即可,每按字節(jié)輸出一個(gè)兩位十六進(jìn)制數(shù)數(shù)組指針移動(dòng)一位。

代碼:

include io32.inc.data array byte 'This is a test.' writebuf byte 12 dup (0) ;顯示緩沖區(qū).code start: mov eax,offset array ;將數(shù)組主存偏移地址給eaxmov ecx,lengthof array ;ecx存放字節(jié)數(shù)call dispmem ;調(diào)用子程序按字節(jié)從低地址到高地址輸出exit 0;子程序部分 dispmem procpush eax ;保護(hù)eaxpush ecx ;保護(hù)ecx mov ebx,eax ;先把整體32位傳給ebx,ebx做數(shù)組指針 again: mov eax,[ebx] ;把數(shù)組中元素傳給eaxcall disphb ;利用十六進(jìn)制字節(jié)顯示子程序mov eax, 32 ;輸出空格call dispc inc ebx ;指針移動(dòng)loop againdispmem endp ;子程序結(jié)束end start

運(yùn)行結(jié)果:

2.3 十進(jìn)制數(shù)的輸入和顯示子程序

(1) 有符號(hào)十進(jìn)制數(shù)輸入子程序

編寫有符號(hào)十進(jìn)制數(shù)的輸入子程序、以及驗(yàn)證子程序的主程序,并運(yùn)行正確。
思路:(1)首先判斷數(shù)據(jù)是否是0、負(fù)數(shù)或正數(shù),是0則直接顯示’0’退出。
(2) 若輸入有符號(hào)數(shù)為負(fù)數(shù),顯示符號(hào)’-’,求數(shù)據(jù)的絕對(duì)值。
(3) 數(shù)據(jù)除以10,如十進(jìn)制正數(shù)123,
編寫無(wú)符號(hào)十進(jìn)制數(shù)的輸入輸出子程序、以及驗(yàn)證子程序的主程序,并運(yùn)行正確。

.data count = 5 array dword count dup(0) temp dword ? readbuf byte 30 dup(0).code begin: mov ecx,count mov ebx,offset array ;ebx為數(shù)組指針 again: call read ;調(diào)用子程序,輸入一個(gè)數(shù)據(jù)mov eax,temp ;獲得出口參數(shù)mov [ebx],eax ;存放到數(shù)據(jù)緩沖區(qū)add ebx,4 ;指針移動(dòng)dec ecx ;計(jì)數(shù)器遞減jnz again ;循環(huán)判定條件exit 0 ;退出主程序read proc ;輸入有符號(hào)十進(jìn)制數(shù)的子程序push eax ;出口參數(shù):變量temp= 補(bǔ)碼表示的二進(jìn)制數(shù)值push ebx ;說(shuō)明:負(fù)數(shù)用"-"引導(dǎo)push ecx push edx read0: mov eax,offset readbufcall readmsg ;輸入一個(gè)字符串test eax,eax ;沒(méi)有輸入數(shù)據(jù),轉(zhuǎn)向錯(cuò)誤處理cmp eax,12 ;ja readerr ;輸入超過(guò)12個(gè)字符,轉(zhuǎn)向錯(cuò)誤處理mov edx, offset readbuf ;edx指向輸入緩沖區(qū)xor ebx,ebx ;ebx保存結(jié)果xor ecx,ecx ;ecx為正負(fù)標(biāo)志,0為正,-1為負(fù)mov al,[edx] ;取一個(gè)字符cmp al,'+' ;"+",繼續(xù)jz read1 cmp al,'-' ;'-',設(shè)置-1標(biāo)志jnz read2 ;轉(zhuǎn)至無(wú)符號(hào)處理區(qū)read2mov ecx,-1 ;ecx保存符號(hào)位 read1: inc edx ;取下一個(gè)字符mov al,[edx]test al,al ;是結(jié)尾0,轉(zhuǎn)向求補(bǔ)碼jz read3 read2: cmp al,'0' ;不是0~9之間的數(shù)碼,輸入錯(cuò)誤jb readerr cmp al,'9' ja readerrsub al,30h ;0~9之間的數(shù)碼,轉(zhuǎn)換為二進(jìn)制數(shù)imul ebx,10 ;原數(shù)值乘10:ebx = ebx *10jc readerr ;乘積溢出處理movzx eax,al ;零位擴(kuò)展,便與相加add ebx,eax ;原數(shù)值乘10后與新數(shù)值相加cmp ebx,80000000h ;數(shù)據(jù)超過(guò)2^31,出錯(cuò) readerr: mov eax,offset errmsg ;顯示出錯(cuò)信息call dispmsg jmp read0 ; read3: test ecx,ecx ;判斷是正數(shù)還是負(fù)數(shù)jz read4neg ebx ;是負(fù)數(shù),進(jìn)行求補(bǔ)jmp read5 read4: cmp ebx,7fffffffh ;正數(shù)超過(guò)2^31-1,出錯(cuò)ja readerr read5: mov temp,ebx ;設(shè)置出口參數(shù)pop edxpop ecxpop ebxpop eaxret errmsg byte 'error!!',13,10,0 read endpend begin

(2) 有符號(hào)十進(jìn)制數(shù)顯示子程序

編寫有符號(hào)十進(jìn)制數(shù)的顯示子程序、以及驗(yàn)證子程序的主程序,并運(yùn)行正確。

代碼思路:(1) 首先判斷給的數(shù)據(jù)是否是0,若為0則直接輸出’0’。
(2) 是負(fù)數(shù)就在數(shù)組首端放置’-’,并求數(shù)據(jù)絕對(duì)值。
(3) 數(shù)據(jù)除以10,余數(shù)為10進(jìn)制數(shù)碼,加30h轉(zhuǎn)化為ACSII碼之后放在字符數(shù)組edx中保存。
(3) 重復(fù)(3),直到商為0時(shí)結(jié)束。

程序代碼:

include io32.inc.data array dword 1234567890,-1234,0,1,-987654321,32767,-32768,5678,-5678,9000 writebuf byte 12 dup (0) ;顯示緩沖區(qū).code start: mov ecx, lengthof array ;數(shù)組長(zhǎng)度計(jì)數(shù)器 mov ebx, 0 again: mov eax, array[ebx*4] ;逐個(gè)取出字符數(shù)組中元素call write ;調(diào)用子程序,顯示一個(gè)數(shù)據(jù)call dispcrlf inc ebx ;數(shù)組指針移動(dòng)loop again exit 0;子程序部分 write procpush ebxpush ecxpush edxmov ebx, offset writebuf ;字符內(nèi)部數(shù)組首指針,指向顯示緩沖區(qū)test eax,eax ;判斷正/負(fù)數(shù)(結(jié)果不為0),0(結(jié)果為0),jnz write1 ;結(jié)果不為0,要么正,要么負(fù),轉(zhuǎn)至write1進(jìn)一步mov byte ptr[ebx],'0'inc ebx ;按字節(jié)來(lái),數(shù)組指針每次增一jmp write5 write1: jns write2 ;是正數(shù),轉(zhuǎn)至write2處理mov byte ptr[ebx],'-' ;若為負(fù)數(shù),則首先給字符數(shù)組傳入一個(gè)負(fù)號(hào)inc ebx ;字符數(shù)組指針往后移動(dòng)neg eax ;對(duì)eax進(jìn)行求補(bǔ) write2: mov ecx,10 ;除數(shù) ecx=10 push ecx write3: cmp eax,0 ;若該位數(shù)為0,則轉(zhuǎn)至write4jz write4 xor edx,edx ;edx,被除數(shù)高位清零div ecx ;除以10,取余add edx, 30h ;余數(shù)轉(zhuǎn)化為ACSII碼push edx ;將最低位數(shù)字壓棧jmp write3 write4: pop edx ;從高位到低位依次彈出棧中存放的數(shù)字cmp edx,ecx ;和除數(shù)10進(jìn)行比較,是結(jié)束標(biāo)志10,轉(zhuǎn)至打印處理je write5 mov [ebx],dl ;將余數(shù)edx的低八位dl放進(jìn)字符數(shù)組inc ebx ;數(shù)組指針移動(dòng),循環(huán)輸出存放的數(shù)jmp write4 write5: mov byte ptr[ebx],0 ;字符數(shù)組結(jié)束標(biāo)志mov eax, offset writebuf ;打印緩沖區(qū)內(nèi)容call dispmsg pop edxpop ecxpop ebxret write endpend start

運(yùn)行結(jié)果:

(3) 無(wú)符號(hào)十進(jìn)制數(shù)顯示子程序

代碼思路:(1) 首先判斷給的數(shù)據(jù)是否是0,若為0則直接輸出’0’。
(2 數(shù)據(jù)除以10,余數(shù)為10進(jìn)制數(shù)碼,加30h轉(zhuǎn)化為ACSII碼之后放在字符數(shù)組edx中保存。
(3) 重復(fù)(3),直到商為0時(shí)結(jié)束div。
(4) 持續(xù)pop出棧頂元素給edx,此時(shí)出棧順序剛好是從高位到低位。

程序代碼:

include io32.inc.data array dword 1234567890,1234,0,1,987654321,32767,32768,5678,9000 writebuf byte 12 dup (0) ;顯示緩沖區(qū).code start: mov ecx, lengthof array ;ecx為數(shù)組長(zhǎng)度計(jì)數(shù)器 mov ebx, 0 again:mov eax, array[ebx*4] ;使用共享變量傳遞參數(shù)call write ;調(diào)用子程序,顯示一個(gè)數(shù)a,需要保存ebx、ecx的值call dispcrlfinc ebx ;數(shù)組指針移動(dòng)loop againexit 0;子程序write proc push ebxpush ecxpush edxmov ebx, offset writebuf ;字符內(nèi)部數(shù)組首指針,指向顯示緩沖區(qū)test eax,eax ;若為0,則直接輸出jnz write1 ;若不為0,則進(jìn)一步處理mov byte ptr[ebx],'0' ;直接填充字符0inc ebx ;數(shù)組指針后移jmp write4 ;轉(zhuǎn)至打印緩沖區(qū) write1: mov ecx,10 ;被除數(shù)ecx= 10push ecx ;結(jié)束標(biāo)志,當(dāng)edx==ecx時(shí)結(jié)束popwrite2: cmp eax,0 ;判斷商是否為0jz write3 ;商為0,準(zhǔn)備輸出xor edx,edx ;被除數(shù)最高位清零div ecx ;商不為0,繼續(xù)作除法add edx, 30h ;余數(shù)轉(zhuǎn)化為ACSII碼push edx ;余數(shù)(0~9區(qū)間)入棧jmp write2 write3: pop edx ;從高位到低位依次pop出來(lái)cmp edx,ecx ;判斷該數(shù)是否pop完畢jz write4 ;pop完畢,轉(zhuǎn)至輸出緩沖區(qū)mov [ebx],dl ;把從棧中彈出的數(shù)位存至數(shù)組,ACSII<127,用dl即可inc ebx ;數(shù)字內(nèi)部字符數(shù)組,每次增1jmp write3 write4: mov byte ptr[ebx],0 ;數(shù)組末尾加0mov eax, offset writebuf ;打印緩沖區(qū)內(nèi)容call dispmsg pop edxpop ecxpop ebxret write endp end start

(3) 無(wú)符號(hào)十進(jìn)制數(shù)輸入子程序

include io32.inc.data count = 5 ;預(yù)留5個(gè)空間 array dword count dup(0) ;初始化數(shù)組 temp dword ? ;工具變量 readbuf byte 30 dup(0) ;輸入緩沖區(qū) .code start: mov ecx,count mov ebx,offset array ;ebx為數(shù)組指針 again: call read ;調(diào)用子程序,輸入一個(gè)數(shù)據(jù)mov eax,temp ;將返回的temp傳回eaxmov [ebx*4],eax ;存放到數(shù)據(jù)緩沖區(qū)inc ebx ;數(shù)組指針移動(dòng)loop again ;循環(huán)將所有數(shù)據(jù)全部輸出exit 0;子程序部分 read proc push ebx ;主程序中數(shù)組指針,需要保護(hù)push ecx ;主程序中計(jì)數(shù)器,需要保護(hù) read0: mov eax,offset readbuf ;輸入緩沖區(qū)首地址傳給eaxcall readmsg ;輸入一個(gè)字符串test eax,eax ;沒(méi)有輸入數(shù)據(jù),轉(zhuǎn)向錯(cuò)誤處理cmp eax,12 ;輸入若超過(guò)12個(gè)字符,則轉(zhuǎn)向錯(cuò)誤處理ja readerrmov edx, offset readbuf ;edx作為數(shù)組指針xor ebx,ebx ;ebx保存結(jié)果 read1: mov al,[edx] ;從數(shù)組中取出一個(gè)字符test al,al ;判斷是不是字符串結(jié)尾jz read2 ;是結(jié)尾,輸出該數(shù)inc edxcmp al,'0' ;若某一位小于0,輸入錯(cuò)誤jb readerrcmp al,'9'ja readerrsub al,30h ;ACSII碼轉(zhuǎn)化為0~9區(qū)間的數(shù)imul ebx,10 ;ebx = ebx*10,給權(quán)重,初始時(shí)ebx=0jc readerr ;乘積溢出處理movzx eax,al ;零位擴(kuò)展,便于前數(shù)ebx和當(dāng)前位al相加add ebx,eaxcmp ebx,0ffffffffh ;判斷數(shù)據(jù)是否超過(guò)2^32-1jbe read1 ;沒(méi)超過(guò),繼續(xù)取下一個(gè)字符 readerr: mov eax,offset errmsg ;顯示出錯(cuò)信息call dispmsg jmp read0 ;再次輸入 read2: mov temp,ebx ;設(shè)置出口參數(shù)pop ecxpop ebxret errmsg byte 'error!!',13,10,0 read endpend start

總結(jié)

以上是生活随笔為你收集整理的汇编语言程序设计实验(六)——子程序设计的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。