您的位置:首页 > 其它

王爽 《汇编语言》 读书笔记 十三 int指令

2017-09-17 09:52 441 查看
第十三章 int指令

int引发的属于内中断

13.1 int指令

int n  , n为中断类型码。

1)取得中断类型码

2)标志寄存器入栈,IF=0, TF=0;

3)CS,IP 入栈

4)(IP)=(n*4)  (CS) = (n*4 + 2)

中断处理程序也称为中断例程

13.2 编写供应用程序调用的中断例程

问题,编写,安装中断7ch的中断例程。

功能:求一word型数据的平方。

参数:(ax)=要计算的数据

返回值:dx, ax中存放结果的高16位和低16位。

应用举例: 求 2*3456 ^2

应用代码

assume cs:code
code segment
start:		mov ax, 3456
int 7ch
add ax, ax
adc dx, dx

mov ax, 4c00h
int 21h
code ends
end start


安装int 7ch的代码

assume cs:code
code segment
start:						; set es:di as target address 0000:0200h
mov ax, 0
mov es, ax
mov di, 200h
; set ds:si as source address cs:sqr
mov ax, cs
mov ds, ax
mov si, offset sqr
; set cx as the data length
mov cx, offset sqrend - offset sqr
; set the transport directive DF = 0 cld
cld
rep movsb

; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)
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:		mul ax
iret

sqrend:		nop
code ends
end start
运行结果注意观察程序跳转到了0000:0200处执行并返回



问题二, 编写,安装中断7ch的中断例程

功能:将一个全是字母,以0结尾的字符串,转换为大写。

参数:ds:si指向字符串首地址

举例:将data段中的字符转换为大写

应用代码

assume cs:code

data segment
db 'conversation', 0
data ends

code segment
start:		mov ax, data
mov ds, ax
mov si, 0
int 7ch

mov ax, 4c00h
int 21h
code ends
end start


安装INT 7CH的安装例程 

(cpu进入中断例程之前会自动pushf 保存标志寄存器因此中断例程可以省略pushf和popf)

assume cs:code
code segment
start:						; set es:di as target address 0000:0200h
mov ax, 0
mov es, ax
mov di, 200h
; set ds:si as source address cs:sqr
mov ax, cs
mov ds, ax
mov si, offset letterc
; set cx as the data length
mov cx, offset lettercend - offset letterc
; set the transport directive DF = 0 cld
cld
rep movsb

; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)
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

;*****************************************
; Sub process for the IRQ 7CH
; convert to upper case
letterc:	push si
push ax

lts:	mov al, ds:[si]
cmp al, 0
je letterc_ok	; meet the end of the string '\0'
cmp al, 'a'
jb next
cmp al, 'z'
ja next
and al, 11011111B	; conver to upper case
mov ds:[si], al		; apply change to data
next:	inc si
jmp short lts

letterc_ok:	pop ax
pop si
iret
lettercend:	nop
code ends
end start


运行结果



13.3 对int, iret和栈道深入理解

问题:用7ch中断例程完成loop指令的功能

应用举例:在屏幕中显示80个‘!’

assume cs:code
code segment
start:		mov ax, 0b800h
mov es, ax
mov di, 160 * 12

mov bx, offset s - offset se ; set the jump offset from se to s
mov cx, 80
s:	mov byte ptr es:[di], '!'
add di, 2
int 7ch

mov ax, 4c00h
int 21h
code ends
end start


为了模拟loop指令7ch具备以下功能

1)dec cx

2)如果CX != 0 转到标号S处执行,否则向下执行

利用在中断例程中定位到IP的值,然后将其修改。

安装例程

assume cs:code
code segment
start:						; set es:di as target address 0000:0200h
mov ax, 0
mov es, ax
mov di, 200h
; set ds:si as source address cs:sqr
mov ax, cs
mov ds, ax
mov si, offset lp
; set cx as the data length
mov cx, offset lpend - offset lp
; set the transport directive DF = 0 cld
cld
rep movsb

; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)
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

lp:		push bp
mov bp, sp
dec cx
jcxz lpret
add [bp + 2], bx	; the bp register default point to ss:bp

lpret:	pop bp
iret
lpend:	nop
code ends
end start


运行结果



监测点13.1

用7ch中断来完成jmp near ptr s 指令的功能,用bx向中断例程出纳送位移。

assume cs:code
data segment
db 'conversation', 0
data ends

code segment
start:		mov ax, data
mov ds, ax
mov si, 0
mov ax, 0b800h
mov es, ax
mov di, 12 * 160

s:	cmp byte ptr ds:[si], 0
je ok
mov al, [si]
mov es:[di], al
inc si
add di, 2
mov bx, offset s - offset ok	; offset between ok to segment
int 7ch

ok:	mov ax, 4c00h
int 21h
code ends
end start


安装例程

assume cs:code
code segment
start:						; set es:di as target address 0000:0200h
mov ax, 0
mov es, ax
mov di, 200h
; set ds:si as source address cs:sqr
mov ax, cs
mov ds, ax
mov si, offset jump
; set cx as the data length
mov cx, offset jumpend - offset jump
; set the transport directive DF = 0 cld
cld
rep movsb

; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)
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

jump:		push bp
mov bp, sp
add [bp + 2], bx	; the bp register default point to ss:bp
; so we modify the IP in the stack
pop bp
iret
jumpend:	nop
code ends
end start
运行结果



13.4 BIOS和DOS所提供的中断例程

BIOS中主要包含以下几部分内容

1)硬件系统的检测和初始化程序;

2)外部中断和内部中断的中断例程

3)用于对硬件设备进行I/O操作的中断例程

4)其他和硬件相关的中断例程

操作系统DOS也提供了中断例程,

和硬件相关的中断例程中,一般都调用了BIOS的中断例程。

13.5 BIOS和DOS中断例程的安装过程

1) 开机以后,cpu一加点。初始化CS= 0FFFFH IP=0,此处有一跳转指令cpu取执行BIOS中的硬件系统检测和初始化程序。

2)初始化程序将建立BIOS所支持的中断向量,即将BIOS提供的中断例程的入口地址登记证中断向量表中。

3)硬件系统检测和初始化完成后,调用int 19h进行操作系统的引导。从此计算机由操作系统控制

4)DOS启动以后,除了完成其他工作外,还将它所提供的中断例程装入内存,并建立相应的中断向量。

13.6 BIOS中断例程应用

int 10h 中断例程设置光标位置功能。

mov ah, 2 ; 置光标

mov bh, 0 ; 第0页

mov dh, 5 ; dh 中放行号

mov dl, 12  ;dl中放列号

int 10h

在光标位置显示字符功能

mov ah, 9  ;在光标位置显示字符

mov al, 'a' ; 字符

mov bl, 7 ; 颜色属性

mov bh, 0 ; 第0 页

mov cx, 3 ;字符重复个数

int 10h

例子 在屏幕的5行12列显示3个红底高亮闪烁绿色的'a'.

assume cs: code

code segment
start:		mov ah, 2	; set sursor
mov bh, 0	; page #0
mov dh, 5	; line#5
mov dl, 12	; col#12
int 10h

mov ah, 9	; put char
mov al, 'a'	; char 'a'
mov bl, 0cah	;
mov bh, 0
mov cx, 3
int 10h

mov ax, 4c00h
int 21h
code ends

end start


运行结果



13.7 DOS中断例程应用

int21 是dos提供的中断例程

mov ah, 4ch ;  程序返回

mov al, 0    ;  返回值

int 21h

用21号中断例程中光标位置显示字符串的功能:

;ds:dx 指向字符串

mov ah, 9

int 21h

在屏幕的5行12列显示字符串"Welcome to masm!"

assume cs: code
data segment
db 'Welcome to masm', '$'	;'$' is the end symbol for int21 putchar
data ends
code segment
start:		mov ah, 2	; set sursor
mov bh, 0	; page #0
mov dh, 5	; line#5
mov dl, 12	; col#12
int 10h

mov ax, data
mov ds, ax
mov dx, 0	; ds:dx point to the string
mov ah, 9
int 21h

mov ax, 4c00h
int 21h
code ends

end start


运行结果



实验13 编写,应用中断例程

1)编写并安装int 7ch 中断例程, 功能为显示一个用0结束的字符串,中断例程安装在0:200处

参数: (dh ) = 行号, (dl) = 列号, (cl) = 颜色, ds:si 指向字符串的首地址

运行代码

assume cs:code
data segment
db "Welcome to masm!", 0
data ends

code segment
start:	mov dh, 10
mov dl, 10
mov cl, 2
mov ax, data
mov ds, ax
mov si, 0
int 7ch

mov ax, 4c00h
int 21h
code ends
end start


装载程序

assume cs:code
code segment
start:						; set es:di as target address 0000:0200h
mov ax, 0
mov es, ax
mov di, 200h
; set ds:si as source address cs:sqr
mov ax, cs
mov ds, ax
mov si, offset show_str
; set cx as the data length
mov cx, offset show_stre - offset show_str
; set the transport directive DF = 0 cld
cld
rep movsb

; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)
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:	push ax
push bx
push cx
push dx
push bp
push si
push di
push es

mov ax, 0b800h	; load the first address to vram
mov es, ax

mov bp, 0		; reset bp to 0
mov ax, 0		; reset al ah
mov al, dh
mov bl, 0a0h
mul bl			; dh x 160 = line offset
add bp, ax		; add the line offset

mov ax, 0		; reset al ah
mov al, dl
mov bl, 2
mul bl			; dl x 2 = column offset
add bp, ax		; add the column offset

mov di, 0
mov ah, cl		; store the color info
next:	mov cl, [si]
mov ch, 0
jcxz ok			; check whether pointer to '\0'
mov al, cl		; store the char
mov es:[bp][di], ax ;
inc si
add di, 2
jmp short next

ok:	pop es
pop di
pop si
pop bp
pop dx
pop cx
pop bx
pop ax
iret
show_stre:	nop
code ends
end start


运行结果:



2)编写并安装int 7ch中断例程,功能为完成loop指令的功能

参考13.3 运行代码

3)下面代码 分别在屏幕的第2,4,6,8 行显示4句英文诗,补全程序。

assume cs:code
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, 8

start:	mov ax, cs
mov ds, ax
mov bx, offset s
mov si, offset row
mov cx, 4
ok:	mov bh, 0
mov dh,  ds:[si]
mov dl, 0
mov ah, 2
int 10h

mov dx, ds:[bx]
mov ah, 9
int 21h
inc si
add bx, 2
loop ok

mov ax, 4c00h
int 21h
code ends
end start


运行结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  汇编语言