實(shí)驗(yàn)16 編寫包含多個(gè)功能子程序的中斷例程
1) 思路:
- 根據(jù) “第 16 章中的代碼示例” 以及 “安裝程序” 的套路摸航,仿照著之前的例子,代碼寫出來不會(huì)太難
- 需要注意以下幾點(diǎn):
a. 在代碼段中铜邮,“標(biāo)號(hào)”或者是“數(shù)據(jù)標(biāo)號(hào)”仪召,都代表了該段內(nèi)的某一個(gè)偏移地址寨蹋。
b. 對(duì)于任何一個(gè)內(nèi)存段來說,段地址可以開始于任何16 字節(jié)對(duì)齊的地方扔茅,偏移地址則總是從 0x0000 開始遞增已旧。
c. 在編譯階段,每條指令都被計(jì)算并賦予了一個(gè)匯編地址召娜,就像它們已經(jīng)被加載到內(nèi)存中的某個(gè)段里一樣运褪。實(shí)際上,當(dāng)編譯好的程序加載到物理內(nèi)存后玖瘸,它在段內(nèi)的偏移地址和它在編譯階段的匯編地址是相同的秸讹。
d. 在安裝“中斷例程”程序時(shí),如果“中斷例程”中的直接定址表有使用到多個(gè)子程序的“標(biāo)號(hào)”店读,那么我們需要在“中斷例程”第一行指令前面嗦枢,使用 org 偏移地址 設(shè)定程序段的起始地址,否則“中斷例程”中的標(biāo)號(hào)對(duì)應(yīng)的偏移地址屯断,依然還是安裝程序在運(yùn)行時(shí)的偏移地址文虏。
溫馨提示:
- 對(duì)于 標(biāo)號(hào) 對(duì)應(yīng)的偏移地址有疑問的,請(qǐng)查看 《x86匯編語言:從實(shí)模式到保護(hù)模式》(李忠) 第 5 章的 5.5 小節(jié)殖演。
- 使用 org 偏移地址 設(shè)定程序段的起始地址(即 從“org 偏移地址”開始氧秘,后面指令的偏移地址將按照設(shè)定的起始地址(偏移地址)為起點(diǎn)開始遞增)。
2) int 7ch 中斷例程代碼:
文件名:exp16.asm
assume cs:code
code segment
start:
mov ax, cs
mov ds, ax
mov si, offset setscreen ; 設(shè)置 ds:si 指向源地址
mov ax, 0
mov es, ax
mov di, 200h ; 設(shè)置 es:di 指向目的地址
mov cx, offset setscreenend - offset setscreen ; 設(shè)置 cx 為傳輸長度
cld ; 設(shè)置傳輸方向?yàn)檎? rep movsb
; 設(shè)置中斷向量表
mov ax, 0
mov es, ax
cli
mov word ptr es:[7ch * 4], 200h
mov word ptr es:[7ch * 4 + 2], 0
sti
mov ax, 4c00h
int 21h
; 新的 int 7ch 中斷例程
org 200h ; 設(shè)置 200h 作為以下指令的起始地址趴久,后面指令的偏移地址將按照設(shè)定的起始地址(偏移地址)為起點(diǎn)開始遞增
setscreen:
jmp short set
table dw sub1, sub2, sub3, sub4
set:
push bx
cmp ah, 3 ; 判斷功能號(hào)是否大于 3
ja sret
mov bl, ah ; ah 寄存器傳遞功能號(hào)
mov bh, 0
add bx, bx ; 根據(jù) ah 中的功能號(hào)丸相,計(jì)算對(duì)應(yīng)子程序在 table 表中的偏移(table 表使用 dw 定義字型數(shù)據(jù),所以需要 功能號(hào)*2)
call word ptr table[bx] ; 調(diào)用對(duì)應(yīng)的功能子程序
sret:
pop bx
iret
sub1:
push bx
push cx
push es
mov bx, 0b800h
mov es, bx
mov bx, 0
mov cx, 2000
sub1s:
mov byte ptr es:[bx], ' '
add bx, 2
loop sub1s
pop es
pop cx
pop bx
ret ; 清屏
sub2:
push bx
push cx
push es
mov bx, 0b800h
mov es, bx
mov bx, 1
mov cx, 2000
sub2s:
and byte ptr es:[bx], 11111000b
or es:[bx], al
add bx, 2
loop sub2s
pop es
pop cx
pop bx
ret ; 設(shè)置前景色(字符顏色)
sub3:
push bx
push cx
push es
mov cl, 4
shl al, cl
mov bx, 0b800h
mov es, bx
mov bx, 1
mov cx, 2000
sub3s:
and byte ptr es:[bx], 10001111b
or es:[bx], al
add bx, 2
loop sub3s
pop es
pop cx
pop bx
ret ; 設(shè)置背景色
sub4:
push cx
push si
push di
push es
push ds
mov si, 0b800h
mov es, si
mov ds, si
mov si, 160 ; ds:si 指向第 n+1 行
mov di, 0 ; es:di 指向第 n 行
cld
mov cx, 24 ; 共復(fù)制 24 行
sub4s:
push cx
mov cx, 160
rep movsb ; 復(fù)制一整行彼棍,一行有 160 個(gè)字節(jié)
pop cx
loop sub4s
mov cx, 80
mov si, 0
sub4s1:
mov byte ptr [160*24+si], ' ' ; 最后一行清空
add si, 2
loop sub4s1
pop ds
pop es
pop di
pop si
pop cx
ret ; 向上滾動(dòng)一行
setscreenend:
nop
code ends
end start
3) 測(cè)試程序代碼:
文件名:exp16d.asm
- 清屏
assume cs:code
code segment
start:
mov ah, 0 ; 0 表示清屏灭忠,1 表示設(shè)置前景色,2 表示設(shè)置背景色座硕,3 表示向上滾動(dòng)一行
mov al, 2 ; 對(duì)于 1弛作、2 號(hào)功能,用 al 傳送顏色值华匾,(al)∈{0, 1, 2, 3, 4, 5, 6, 7}
int 7ch ; 調(diào)用新的 int 7ch 中斷例程
mov ax, 4c00h
int 21h
code ends
end
清屏 程序運(yùn)行效果1
清屏 程序運(yùn)行效果2
- 設(shè)置前景色
assume cs:code
code segment
start:
mov ah, 1 ; 0 表示清屏映琳,1 表示設(shè)置前景色,2 表示設(shè)置背景色蜘拉,3 表示向上滾動(dòng)一行
mov al, 2 ; 對(duì)于 1萨西、2 號(hào)功能,用 al 傳送顏色值旭旭,(al)∈{0, 1, 2, 3, 4, 5, 6, 7}
int 7ch ; 調(diào)用新的 int 7ch 中斷例程
mov ax, 4c00h
int 21h
code ends
end
設(shè)置前景色 程序運(yùn)行效果
- 設(shè)置背景色
assume cs:code
code segment
start:
mov ah, 2 ; 0 表示清屏谎脯,1 表示設(shè)置前景色,2 表示設(shè)置背景色您机,3 表示向上滾動(dòng)一行
mov al, 2 ; 對(duì)于 1穿肄、2 號(hào)功能年局,用 al 傳送顏色值,(al)∈{0, 1, 2, 3, 4, 5, 6, 7}
int 7ch ; 調(diào)用新的 int 7ch 中斷例程
mov ax, 4c00h
int 21h
code ends
end
設(shè)置背景色 程序運(yùn)行效果
- 向上滾動(dòng)一行
assume cs:code
code segment
start:
mov ah, 3 ; 0 表示清屏咸产,1 表示設(shè)置前景色矢否,2 表示設(shè)置背景色,3 表示向上滾動(dòng)一行
mov al, 2 ; 對(duì)于 1脑溢、2 號(hào)功能僵朗,用 al 傳送顏色值,(al)∈{0, 1, 2, 3, 4, 5, 6, 7}
int 7ch ; 調(diào)用新的 int 7ch 中斷例程
mov ax, 4c00h
int 21h
code ends
end
向上滾動(dòng)一行 程序運(yùn)行效果