实验代码 lab16.Asm 如下:
assume cs:code
code segment
start:
;将int 7ch写入内存0:200h中
;ds:si指向要安装的int 7ch中断例程
mov ax,cs
mov ds,ax
mov si,offset int7ch
;es:di指向复制目标地址0:200h
mov ax,0
mov es,ax
mov di,200h
;将ds:si复制到es:di中
mov cx,offset int7chend - offset int7ch
cld ;设置df=0,si di将inc
rep movsb
;修改中断向量表中的值,使int 7ch指向0:200h
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中断例程
int7ch:
jmp short int7chstart
table dw offset clear - offset int7ch + 200h
dw offset set_fore - offset int7ch + 200h
dw offset set_back - offset int7ch + 200h
dw offset scroll - offset int7ch + 200h
int7chstart:
push bx
push cx
push si
push di
push ds
push es
;判别&查询子程序地址
cmp ah,3
ja int7chret ;ah设置超过3,表明请求出错,程序直接返回
;用于1、2号功能,判断set_fore set_back中al设置是否正确
cmp ah,1
je judge_color
cmp ah,2
je judge_color
jmp find_add ;非1、2号功能,跳去查找地址
judge_color:
cmp al,7
ja int7chret ;超过7的为输入错误,直接返回
jmp find_add ;输入正确,跳去查找地址
find_add:
push ax
mov ax,20h
mov ds,ax ;设置ds指向int 7ch中断例程
pop ax
mov bl,ah
mov bh,0
add bx,bx ;所查找为位置bx=2*bx,add bx,bx是bx=2bx的快速求法
call word ptr cs:[bx+202h] ;在table中寻址
;结束后中断返回iret
int7chret:
pop es
pop ds
pop di
pop si
pop cx
pop bx
iret
;0号功能
;作用:清屏
clear:
mov bx,0b800h
mov es,bx
mov bx,0 ;设置字符从0开始
mov cx,2000
clears:
mov byte ptr es:[bx],' ' ;将从0开始的偶数显存全部设置为空格符
add bx,2
loop clears
ret ;子程序返回
;1号功能
;作用:设置前景色
set_fore:
mov bx,0b800h
mov es,bx
mov bx,1 ;设置前景色/背景色从1开始
mov cx,2000
set_fores:
and byte ptr es:[bx],11111000b ;只将前景色位清0
or es:[bx],al ;只设置前景色位
add bx,2
loop set_fores
ret ;子程序返回
;2号功能
;作用:设置背景
set_back:
mov bx,0b800h
mov es,bx
mov bx,1 ;设置前景色/背景色从1开始
mov cx,2000
set_backs:
and byte ptr es:[bx],10001111b ;只将背景色位清0
or es:[bx],al ;只设置背景色位
add bx,2
loop set_backs
ret ;子程序返回
;3号功能
;作用:向上滚动一行
;实现方法:以此将n+1行的内容复制到n行处,最后一行留空
scroll:
mov si,0b800h
mov es,si
mov ds,si
mov si,160 ;ds:si指向下一行
mov di,0 ;es:di指向上一行
cld
mov cx,24 ;只复制24次,因为最后一行留空
scrolls:
push cx
mov cx,160 ;每行160个字轮流复制
rep movsb
pop cx
loop scrolls
;最后一行留空,利用循环完成
mov cx,80
mov si,0
scrolls0:
mov byte ptr es:[160*24+si],' '
add si,2
loop scrolls0
ret ;子程序返回
int7chend:
nop
code ends
end start
测试程序如下:
tlab16-0.Asm:
assume cs:code
code segment
;测试清屏clear功能
start:
mov ah,0
int 7ch
;程序返回
mov ax,4c00h
int 21h
code ends
end start
tlab16-1.Asm:
assume cs:code
code segment
;测试set_fore功能
start:
mov ah,1
mov al,7
int 7ch
;程序返回
mov ax,4c00h
int 21h
code ends
end start
tlab16-2.Asm:
assume cs:code
code segment
;测试set_back功能
start:
mov ah,2
mov al,7
int 7ch
;程序返回
mov ax,4c00h
int 21h
code ends
end start
tlab16-3.Asm:
assume cs:code
code segment
;测试scroll功能
start:
mov ah,3
int 7ch
;程序返回
mov ax,4c00h
int 21h
code ends
end start