Skip to content

Instantly share code, notes, and snippets.

@alexsunday
Created December 4, 2019 01:12
Show Gist options
  • Save alexsunday/f54762168017c9e787ce11d088977499 to your computer and use it in GitHub Desktop.
Save alexsunday/f54762168017c9e787ce11d088977499 to your computer and use it in GitHub Desktop.
assume cs:code, ds:data, ss:stack
; 编写 0 号中断的处理,使得除法溢出发生时,屏幕中间显示字符串 divide error
data segment
db 128 dup (0)
data ends
stack segment stack
db 128 dup (0)
stack ends
code segment
start:
; 栈
mov ax, stack
mov ss, ax
mov sp, 128
; 数据
mov ax, cs
mov ds, ax
mov si, offset except0
; 目标
mov ax, 0
mov es, ax
mov di, 200H
; 复制代码到目标
mov cx, offset except0_end - offset except0
cld
rep movsb
; 修改中断向量表
mov word ptr es:[0], 200H
mov word ptr es:[2], 0
mov ax, 4C00H
int 21H
except0:
jmp short except0_code
show_label:
db 'Divide ERROR!',0
except0_code:
push ax
push cx
push dx
push ds
push si
mov dh, 10
mov dl, 10
mov cl, 00000010B
mov ax, 0
mov ds, ax
mov si, 200H + offset show_label - offset except0
call show_str
pop si
pop ds
pop dx
pop cx
pop ax
; 退出错误的程序,返回DOS
mov ax, 4C00H
int 21H
iret
show_str:
; 显示字符串,show_str 在指定位置,使用指定颜色,显示0结尾的字符串
; dh 行号,dl 列号,cl 颜色 ds:si 指向字符串首地址
; 0 行 第0列 偏移为 0
; 1 行 第0列 偏移为 160 第 N 行 m 列,偏移为 160 * n + 2 * m
push ax
push cx
push dx
push si
push di
mov ax, 0B800H
mov es, ax
; 计算首地址 di
mov al, dh
mov ah, 160
mul ah
push ax
mov al, dl
mov ah, 2
mul ah
pop di
add di, ax
mov ah, cl
mov cx, 0
show_byte:
mov al, ds:[si]
mov es:[di], ax
mov cl, al
jcxz show_end
inc si
add di, 2
jmp show_byte
show_end:
pop di
pop si
pop dx
pop cx
pop ax
ret
except0_end:
nop
code ends
end start
@alexsunday
Copy link
Author

图片

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment