格式
int指令也是一种内中断指令,int指令的格式为int n,n是中断类型码。也就是说,使用int指令可以调用任意的中断例程,例如我们可以显示的调用0号中断例程,还记得在汇编学习笔记(10)中我们自己定义了除法溢出的例程么,我们在这里直接调用看看
assume cs:codesg codesg segment start:
mov ax,0b800h
mov es,ax
mov byte ptr es:[*+*],'h'
int codesg ends end start
成了,perfect!
既然可以随时随地的调用中断例程,那么就可以借助中断例程来完成一些系统必须的子程序,下面是书上的例子,当次搬运工
;编写7ch号中断例程,计算word型数据的平方
assume cs:codesg codesg segment start:
;ds:si指向sqr
mov ax,cs
mov ds,ax
mov si,offset sqr
;es:di指向0000:01e0
mov ax,
mov es,ax
mov di,01e0h
;安装
mov cx,offset sqrend-offset sqr
cld
rep movsb
;设置中断向量表
mov ax,
mov es,ax
mov word ptr es:[7ch*],01e0h
mov word ptr es:[7ch*+],
mov ax,
int 7ch mov ax,4c00h
int 21h
sqr:
mul ax
iret
sqrend:
nop codesg ends end start
;编写7ch号中断例程,将字符串全部转换为大写
assume cs:codesg,ds:data
data segment
db 'h',
data ends
codesg segment start:
;ds:si指向sqr
mov ax,cs
mov ds,ax
mov si,offset upper
;es:di指向0000:01e0
mov ax,
mov es,ax
mov di,01e0h
;安装
mov cx,offset upperend-offset upper
cld
rep movsb
;设置中断向量表
mov ax,
mov es,ax
mov word ptr es:[7ch*],01e0h
mov word ptr es:[7ch*+],
mov ax,data
mov ds,ax
mov si,
mov cl,
;显示原始字符串
call display
;调用中断程序
int 7ch
;大写
call upper
mov cl,
;显示改写后的字符串
call display mov ax,4c00h
int 21h
upper:
push cx
push si
change:
mov cl,[si]
mov ch,
jcxz s0
and byte ptr [si],11011111b
inc si
jmp short change
s0:
pop si
pop cx
iret
upperend:
nop display:
push ax
push es
push di
push si
push cx
mov ax,0b800h
mov es,ax
mov ax,
mul cl
add ax,
mov di,ax
mov ch,
s1:
mov cl,ds:[si]
jcxz ok
mov byte ptr es:[di],cl
add di,
inc si
jmp short s1
ok:
pop cx
pop si
pop di
pop es
pop ax
ret
codesg ends end start
中断例程实现loop指令
中断例程实现loop指令需要完成以下功能:
1.(cx)=(cx)-1
2.如果(cx)!=0,循环执行,否则向下继续执行
为了完成上述的两种功能,显然中断例程必须获取cs和ip的值。因调用中断例程是现将cs、ip入栈,所以只需将ip的值减去循环体的长度就可以了
assume cs:code code segment start:
mov ax,cs
mov ds,ax
mov si,offset looper
mov ax,01e0h
mov es,ax
mov di,
mov cx,offset l-offset looper
cld
rep movsb
mov ax,
mov es,ax
mov word ptr es:[7ch*],01e0h
mov word ptr es:[7ch*+],
mov ax,0b800h
mov es,ax
mov di,*
mov bx,offset s-offset send
mov cx,
s:
mov byte ptr es:[di],'!'
add di,
int 7ch
send:
nop
mov ax,4c00h
int 21h looper:
push bp
mov bp,sp
dec cx
jcxz loopend
add [bp+],bx
loopend:
pop bp
iret
l:
nop
code ends end start
BIOS提供的中断例程
BIOS提供了很多中断例程以供调用,下面举个显示字符串的例子,int 10h例程中的9号子程序
assume cs:code code segment start:
mov ah,
mov bh,
mov dh,
mov dl,
int 10h mov ah,
mov al,'a'
mov bl,11001010b
mov bh,
mov cx,
int 10h
mov ax,4c00h
int 21h
code ends end start
端口的读写
端口的读写只有两种指令和寄存器可以使用
- in 读入指令,in al 读入8位数据;in ax 读入16位数据
- out 写入指令,out al 对端口写入8位数据;out ax写入16位数据
CMOS RAM芯片
CMOS芯片包含两个端口70h和71h,70h是地址端口,71h是数据端口,从CMOS RAM中读取2号单元内容程序如下
assume cs:code code segment start:
mov al,
out 70h,al ;表示要读取2号单元
in al,71h;读取2号单元内容 mov ax,4c00h
int 21h
code ends end start
写入的是这样
assume cs:code code segment start:
mov al,
out 70h,al ;表示要写入2号单元
mov al,
out 71h,al;写入0 mov ax,4c00h
int 21h
code ends end start
shl和shr指令
shl是逻辑左移指令,shr是逻辑右移指令。左移的最低位和右移的最高位都用0补充,并将移出的最后一位写入CF中,不论左移还是右移,都是从最低位开始的。
mov al,10010000
shl al,1
shr al,1
左移后(al)=00100000 CF=1,右移后(al)=00010000 CF=0.左移一位相当于x=x*2,右移一位相当于x=x/2.
显示时间
assume cs:code,ds:data data segment
db 'yy/mm/dd hh:mm:ss','$'
index db ,,,,,
data ends code segment start:
mov ax,data
mov ds,ax
mov si,
mov di,
mov cx,
s:
push cx
;获取需要读取的端口地址
mov al,index[si]
out 70h,al
in al,71h mov ah,al
mov cl,
;取得十位
shr ah,cl
;个位
and al,00001111b
;转为字符
add ah,30h
add al,30h mov ds:[di],ah
mov ds:[di+],al inc si
add di, pop cx
loop s
;光标显示位置
mov ah,
mov bh,
mov dh,
mov dl,
int 10h
;显示ds:dx指向的字符串
mov ax,data
mov ds,ax
mov dx,
mov ah,
int 21h mov ax,4c00h
int 21h
code ends end start