实验13 编写、应用中断
int指令的最終功能和call指令類似,都是調用一段程序
兩者的根本區別表現在服務時間和服務對象上不一樣。(本段敘述參考原文:http://blog.csdn.net/jxq0816/article/details/50121563)
1)調用子程序發生的時間是已知的和固定的,即在主程序的調用指令(CALL)執行時發生主程序調用子程序過程,調用指令所在位置是已知的和固定的;而中斷過程發生的時間一般是隨機的,CPU在執行某一主程序時受到中斷源提出的中斷申請,就發生中斷過程,而中斷申請一般由硬件電路產生,申請時間是隨機的。也可以說,調用子程序是程序設計者事先安排好的,而執行中斷服務程序是由系統工作環境隨機決定的。
2)子程序完全為主程序服務,兩者屬于主從關系。主程序需要子程序時就去調用子程序,并把調用結果帶回主程序繼續執行。而中斷服務程序與主程序二者一般是無關的,兩者是平行關系。
3)主程序調用子程序的過程完全屬于軟件處理過程,不需要專門的硬件電路,而中斷處理系統是一個軟、硬件結合的系統,需要專門的硬件電路才能完成中斷處理的過程。
4)子程序嵌套可以實現若干級,嵌套的最多級數受計算機內存開辟的堆棧大小限制;而中斷嵌套級數主要由中斷優先級來決定,一般優先級不會很大。
從宏觀上看,雖然程序中斷方式克服了程序查詢方式中CPU"踏步"現象,實現了CPU與IO并行工作,提高了CPU的資源利用率,但從微觀操作分析,CPU在處理中斷程序時,仍需暫停原程序的正常運行,尤其是當高速I/O設備或輔助存儲器頻繁地、成批地與主存交換信息時,需不斷打斷CPU執行現行程序,而執行中斷服務程序。
在完成這個實驗時,可參看下面相關的附注實驗。
(1)
安裝程序:
assume cs:codesg codesg segment start: mov ax, cs mov ds, ax mov si, offset show_str ;設置ds:si指向源地址 mov ax, 0 mov es, ax mov di, 200h ;設置es:si指目的地址 mov cx, offset show_strend - offset show_str ;設置cx傳輸長度 cld rep movsb mov ax, 0 ;設置中斷向量表 mov es, ax mov word ptr es:[7ch*4], 200h mov word ptr es:[7ch*4+2], 0 mov ax, 4c00h int 21h ;-------show_str--------------- show_str: push si push di push cx push bx mov bl, cl mov ax, 0b800h mov es, ax mov di, 0 mov al, 160 mul dh mov di, ax mov al, 2 ;es:di point to display memory mul dl add di, ax s: xor cx, cx mov cl, ds:[si] jcxz ok mov es:[di+0], cl mov es:[di+1], bl add si, 1 add di, 2 jmp s ok: pop bx pop cx pop di pop si iret show_strend:nopcodesg ends end start應用程序:
assume cs:codedata segmentdb 'welcome to masm!', 0 data endscode segment start: mov dh, 10mov dl, 10mov cl, 2mov ax, datamov ds, axmov si, 0int 7chmov ax, 4c00hint 21hcode ends end start實驗結果:
體會:show_str既可以用call子程序方式來調用,也可以用中斷的方式來調用,這兩者有各自的特點和用途,本篇開頭已經略述。
(2)
附三中已經有類似實現,這里略去。
(3)下面的程序,分別在屏幕的第2,4,6,8行顯示4句英文詩,補全程序。
代碼如下:
assume cs:code ;不寫start,默認從cs:0開始讀取指令code segment s1: db 'Good,better,best,','$' s2: db 'Never let it rest,','$' s3: db 'Till good is better,','$' s4: db 'And better,best.','$' s: dw offset s1,offset s2,offset s3,offset s4 row: db 2,4,6,8start: mov ax, csmov ds, axmov bx, offset smov si, offset rowmov cx, 4ok: mov bh, 0mov dh, ds:[si]mov dl, 0mov ah, 2int 10hmov dx, ds:[bx]mov ah, 9int 21hadd bx, 2inc siloop okmov ax, 4c00hint 21hcode ends end start實驗結果:
實驗體會:
這個才是在現實中解決問題的方式--》用別人已經造好的輪子。世界要靠大家的合作才能更好的發展,自己直接用別人的輪子即可;但是首先你好了解別人的輪子是怎么造的,完全不理會原理而只會使用,有可能被組裝出來的“汽車”非常的難用。總之,研究其理,用其產品。
附一:編寫,安裝中斷7ch中斷例程,實現word型數據求平方(課本253頁)
1,安裝程序代碼:
assume cs:codesg codesg segment start: mov ax, cs mov ds, ax mov si, offset sqr ;設置ds:si指向源地址 mov ax, 0 mov es, ax mov di, 200h ;設置es:si指目的地址 mov cx, offset sqrend - offset sqr ;設置cx傳輸長度 cld rep movsb mov ax, 0 ;設置中斷向量表 mov es, ax mov word ptr es:[7ch*4], 200h mov word ptr es:[7ch*4+2], 0 mov ax, 4c00h int 21h ;sqr------------------------ sqr: mul axiret sqrend: nopcodesg ends end start2,應用代碼 assume cs:codesgcodesg segment start: mov ax, 3456int 7chadd ax, axadc dx, dxmov ax, 4c00hint 21hcodesg ends end start
3,實驗結果
附二:編寫,安裝中斷7ch中斷例程,實現字符串字母全變成大寫(課本254頁)
1,安裝程序代碼:
assume cs:codesg codesg segment start: mov ax, cs mov ds, ax mov si, offset capital ;設置ds:si指向源地址 mov ax, 0 mov es, ax mov di, 200h ;設置es:si指目的地址 mov cx, offset capitalend - offset capital ;設置cx傳輸長度 cld rep movsb mov ax, 0 ;設置中斷向量表 mov es, ax mov word ptr es:[7ch*4], 200h mov word ptr es:[7ch*4+2], 0 mov ax, 4c00h int 21h ;capital------------------------ capital:push cxpush si change: mov cl, [si]mov ch, 0jcxz okand byte ptr [si], 11011111binc sijmp short changeok: pop sipop cxiretcapitalend:nop codesg ends end start2,應用程序 assume cs:codesgdata segmentdb 'conversation', 0 data endscodesg segment start: mov ax, datamov ds, axmov si, 0int 7chmov ax, 4c00hint 21hcodesg ends end start 3,實驗結果
附三:編寫,安裝中斷7ch中斷例程,實現loop指令功能(課本256頁)
1,安裝程序
2,應用程序
assume cs:codesg codesg segment start: mov ax, 0b800hmov es, axmov di, 160*12mov bx, offset s - offset semov cx, 8 s: mov byte ptr es:[di], '!'add di, 2int 7ch se: nopmov ax, 4c00h int 21h codesg ends end start3,實驗結果
附四:調用BIOS中斷例程
int10中斷例程是bios提供的中斷例程,其中包含了多個和屏幕輸出相關的子程序,確定子程序要通過ah來傳遞內部子程序的編號,代碼如下:
assume cs:codesg codesg segment start: mov ah, 9mov al, 'a'mov bl, 11001010bmov bh, 0mov cx, 3int 10hmov ax, 4c00h int 21h codesg ends end start實驗結果:
附五:調用dos中斷例程,在屏幕的第5行12列顯示字符串“Welcome to masm!”
mov ax, 4c00h
int 21h
int 21h中斷例程是dos提供的中斷例程,其中包含了dos提供給程序員在編程時調用的子程序。int 21h中斷例程的4ch號子程序是返回功能。
(ah)=9表示調用第21h號中斷例程的第9號子程序,功能為在光標位置顯示字符串,可以提供要顯示的字符串的地址作為參數。
代碼如下:
assume cs:codesg data segmentdb 'Welcome to masm!', '$' data endscodesg segment start: mov ah, 2 ;置光標mov bh, 0 ;第0頁mov dh, 5 ;dh行mov dl, 12 ;dl列int 10h ;BIOS中斷mov ax, datamov ds, axmov dx, 0 ;point to first address of datamov ah, 9int 21hmov ax, 4c00h int 21h codesg ends end start實驗結果:
從實驗四,五可以看出,遇到打印相關的需求,直接調用BIOS,操作系統的相關中中斷例程即可,無需自己再造輪子。
總結
以上是生活随笔為你收集整理的实验13 编写、应用中断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关卡 动画 蓝图 运行_UE4入门之路(
- 下一篇: Go语言实战-golang操作redis