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

来源:互联网 发布:易企秀电脑版 mac 编辑:程序博客网 时间:2024/06/09 04:55

第十三章 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:codecode segmentstart:mov ax, 3456int 7chadd ax, axadc dx, dxmov ax, 4c00hint 21hcode endsend start

安装int 7ch的代码

assume cs:codecode segmentstart:; set es:di as target address 0000:0200hmov ax, 0mov es, axmov di, 200h; set ds:si as source address cs:sqrmov ax, csmov ds, axmov si, offset sqr; set cx as the data lengthmov cx, offset sqrend - offset sqr; set the transport directive DF = 0 cldcldrep movsb; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)mov ax, 0mov es, axmov word ptr es:[7ch * 4], 200hmov word ptr es:[7ch * 4 + 2], 0mov ax, 4c00hint 21hsqr:mul axiretsqrend:nopcode endsend start
运行结果注意观察程序跳转到了0000:0200处执行并返回

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

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

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

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

应用代码

assume cs:codedata segmentdb 'conversation', 0data endscode segmentstart:mov ax, datamov ds, axmov si, 0int 7chmov ax, 4c00hint 21hcode endsend start


安装INT 7CH的安装例程 

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

assume cs:codecode segmentstart:; set es:di as target address 0000:0200hmov ax, 0mov es, axmov di, 200h; set ds:si as source address cs:sqrmov ax, csmov ds, axmov si, offset letterc; set cx as the data lengthmov cx, offset lettercend - offset letterc; set the transport directive DF = 0 cldcldrep movsb; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)mov ax, 0mov es, axmov word ptr es:[7ch * 4], 200hmov word ptr es:[7ch * 4 + 2], 0mov ax, 4c00hint 21h;*****************************************; Sub process for the IRQ 7CH; convert to upper caseletterc:push sipush axlts:mov al, ds:[si]cmp al, 0je letterc_ok; meet the end of the string '\0'cmp al, 'a'jb nextcmp al, 'z'ja nextand al, 11011111B; conver to upper casemov ds:[si], al; apply change to datanext:inc sijmp short ltsletterc_ok:pop axpop siiretlettercend:nopcode endsend start



运行结果


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

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

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

assume cs:codecode segmentstart:mov ax, 0b800hmov es, axmov di, 160 * 12mov bx, offset s - offset se ; set the jump offset from se to smov cx, 80s:mov byte ptr es:[di], '!'add di, 2int 7chmov ax, 4c00hint 21hcode endsend start

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

1)dec cx

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


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

安装例程

assume cs:codecode segmentstart:; set es:di as target address 0000:0200hmov ax, 0mov es, axmov di, 200h; set ds:si as source address cs:sqrmov ax, csmov ds, axmov si, offset lp; set cx as the data lengthmov cx, offset lpend - offset lp; set the transport directive DF = 0 cldcldrep movsb; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)mov ax, 0mov es, axmov word ptr es:[7ch * 4], 200hmov word ptr es:[7ch * 4 + 2], 0mov ax, 4c00hint 21hlp:push bpmov bp, spdec cxjcxz lpretadd [bp + 2], bx; the bp register default point to ss:bplpret:pop bpiretlpend:nopcode endsend start

运行结果


监测点13.1


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

assume cs:codedata segmentdb 'conversation', 0data endscode segmentstart:mov ax, datamov ds, axmov si, 0mov ax, 0b800hmov es, axmov di, 12 * 160s:cmp byte ptr ds:[si], 0je okmov al, [si]mov es:[di], alinc siadd di, 2mov bx, offset s - offset ok; offset between ok to segmentint 7chok:mov ax, 4c00hint 21hcode endsend start


安装例程

assume cs:codecode segmentstart:; set es:di as target address 0000:0200hmov ax, 0mov es, axmov di, 200h; set ds:si as source address cs:sqrmov ax, csmov ds, axmov si, offset jump; set cx as the data lengthmov cx, offset jumpend - offset jump; set the transport directive DF = 0 cldcldrep movsb; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)mov ax, 0mov es, axmov word ptr es:[7ch * 4], 200hmov word ptr es:[7ch * 4 + 2], 0mov ax, 4c00hint 21hjump:push bpmov bp, spadd [bp + 2], bx; the bp register default point to ss:bp; so we modify the IP in the stackpop bpiretjumpend:nopcode endsend 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: codecode segmentstart:mov ah, 2; set sursormov bh, 0; page #0mov dh, 5; line#5mov dl, 12; col#12int 10hmov ah, 9; put charmov al, 'a'; char 'a'mov bl, 0cah;mov bh, 0mov cx, 3int 10hmov ax, 4c00hint 21hcode endsend 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: codedata segmentdb 'Welcome to masm', '$';'$' is the end symbol for int21 putchardata endscode segmentstart:mov ah, 2; set sursormov bh, 0; page #0mov dh, 5; line#5mov dl, 12; col#12int 10hmov ax, datamov ds, axmov dx, 0; ds:dx point to the stringmov ah, 9int 21hmov ax, 4c00hint 21hcode endsend start

运行结果


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

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

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

运行代码

assume cs:codedata segmentdb "Welcome to masm!", 0data endscode segmentstart:mov dh, 10mov dl, 10mov cl, 2mov ax, datamov ds, axmov si, 0int 7chmov ax, 4c00hint 21hcode endsend start


装载程序

assume cs:codecode segmentstart:; set es:di as target address 0000:0200hmov ax, 0mov es, axmov di, 200h; set ds:si as source address cs:sqrmov ax, csmov ds, axmov si, offset show_str; set cx as the data lengthmov cx, offset show_stre - offset show_str; set the transport directive DF = 0 cldcldrep movsb; set the IRQ table  1F0 = 200h(IP)    1F2 = 0(CS)mov ax, 0mov es, axmov word ptr es:[7ch * 4], 200hmov word ptr es:[7ch * 4 + 2], 0mov ax, 4c00hint 21hshow_str:push axpush bxpush cxpush dxpush bppush sipush dipush esmov ax, 0b800h; load the first address to vrammov es, axmov bp, 0; reset bp to 0mov ax, 0; reset al ahmov al, dhmov bl, 0a0hmul bl; dh x 160 = line offsetadd bp, ax; add the line offsetmov ax, 0; reset al ahmov al, dlmov bl, 2mul bl; dl x 2 = column offsetadd bp, ax; add the column offsetmov di, 0mov ah, cl; store the color infonext:mov cl, [si]mov ch, 0jcxz ok; check whether pointer to '\0'mov al, cl; store the charmov es:[bp][di], ax ;inc siadd di, 2jmp short nextok:pop espop dipop sipop bppop dxpop cxpop bxpop axiretshow_stre:nopcode endsend start

运行结果:


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


参考13.3 运行代码


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

assume cs:codecode segments1: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 s4row: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 21hinc siadd bx, 2loop okmov ax, 4c00hint 21hcode endsend start

运行结果



阅读全文
0 0
原创粉丝点击