int21h的02、 09功能模拟
在写这个程序的过程中学中遇到了,很多的错误。
1、入栈出栈的次数要一致
比如说写下面的程序的时候:
;原始值恢复,返回主程序
pop dx
pop bx
pop di
pop dx 一开始没有加,导致在最后一次的循环的过程中,直接jz,少执行一次pop ax,最终堆栈进出的次数不一致,导致ret 的恢复的ip地址错了,
最后在屏幕上显示了,一堆乱码。
2、0a,0d 在ascii码表里说是回车换行.
最后我发现程序有个问题就是,因为我是直接操作的显存。直接忽视了、回车与换行,在
执行了下面的程序后:
mov dl,0ah
call int21h02
mov dl,0dh
call int21h02
本应该另起一行再显示的。但是当我执行完:
mov dl,'l'
call int21h02
被l代替了a,输出的最终结果为 lbcdefgh
程序最终也只能覆盖显示了。嗯,这个bug留在以后调试吧。
int21h的02、 09功能模拟程序
;系统的字符串输出功能 assume cs:code,ds:data
data segment buffer db 'abcdefgh$' data ends
code segment start: mov ax,data mov ds,ax
lea dx,buffer call int21h09
mov dl,0ah call int21h02 mov dl,0dh call int21h02
mov dl,'l' call int21h02
mov ax,4c00h int 21h
;将偏移地址以dx为首的字符串输出在屏幕上,摸拟系统的9号功能 int21h09:
;bx ,di中的原始值保存起来 push di push bx
xor di,di again: ;将dx指向的字符送到dl中 push dx ;dl中将会把字符存到dl中,故先把dx中的值保存一下。 mov bx,dx mov dl,[bx] cmp dl,'$' ;字符串是否为结束标志 jz outAgain call int21h02 ;不是结束标志,则将dl中的字符显示出来 pop dx inc dx add di,2 jmp again
outAgain: ;原始值恢复,返回主程序 pop dx pop bx pop di ret
;显示单个字符,将dl中的字符显示出来,模拟系统的2号功能 int21h02: mov bx,0b800h mov es,bx mov es:[di],dl ret
code ends end start