如何在 DOS int 16h / AH=1 检测到按键后清除键盘缓冲区中的数据?
How to clear data from keyboard buffer after DOS int 16h / AH=1 detects a keypress?
我正在用 x86 汇编程序在 DOS 下以 16 位实模式 运行 编写程序。
.model small
.stack 100h
.code
start:
mov dl, 4bh
loop1:
mov ah, 2h
int 21h
mov ah, 1h
int 16h
cmp al, 6bh
jne loop1
mov ah, 4ch
int 21h
end start
end
程序需要一直写"K",当在键盘上按下"k"时,程序应该停止。
一切正常,程序一直写入 "K",当我在键盘上按下 "k" 时,我的程序停止了,但是当我按下键盘上的另一个按钮时(什么都不会发生),当我稍后按 "k" 按钮,程序不会停止,但会一直打印 "K"。
我认为这是因为另一个按钮已填满缓冲区,我不知道如何重置它或仅从缓冲区中取出缓冲区的最后一部分,其中包含最后一个按下按钮的代码。
你是对的,键盘缓冲区不是空的,DOS功能Int 16/AH=01h KEYBOARD - CHECK FOR KEYSTROKE keeps returning the first (non-'k') key. If the check detects some keypress, you should swallow it with Int 16/AH=00h KEYBOARD - GET KEYSTROKE。
而不是
mov ah, 1h
int 16h
cmp al, 6bh
jne loop1
尝试
mov ah, 1h ; CHECK FOR KEYSTROKE
int 16h
jz loop1 ; Jump if none pressed.
mov ah, 0h ; GET KEYSTROKE
int 16h
cmp al, 6bh ; Check if it is 'k'.
jne loop1 ; If not, continue. Keybuffer is now empty.
当你 post SO 上的一些源代码时,它应该附有它是如何编译和链接的信息,也 select 适当 tags.
我正在用 x86 汇编程序在 DOS 下以 16 位实模式 运行 编写程序。
.model small
.stack 100h
.code
start:
mov dl, 4bh
loop1:
mov ah, 2h
int 21h
mov ah, 1h
int 16h
cmp al, 6bh
jne loop1
mov ah, 4ch
int 21h
end start
end
程序需要一直写"K",当在键盘上按下"k"时,程序应该停止。
一切正常,程序一直写入 "K",当我在键盘上按下 "k" 时,我的程序停止了,但是当我按下键盘上的另一个按钮时(什么都不会发生),当我稍后按 "k" 按钮,程序不会停止,但会一直打印 "K"。
我认为这是因为另一个按钮已填满缓冲区,我不知道如何重置它或仅从缓冲区中取出缓冲区的最后一部分,其中包含最后一个按下按钮的代码。
你是对的,键盘缓冲区不是空的,DOS功能Int 16/AH=01h KEYBOARD - CHECK FOR KEYSTROKE keeps returning the first (non-'k') key. If the check detects some keypress, you should swallow it with Int 16/AH=00h KEYBOARD - GET KEYSTROKE。 而不是
mov ah, 1h
int 16h
cmp al, 6bh
jne loop1
尝试
mov ah, 1h ; CHECK FOR KEYSTROKE
int 16h
jz loop1 ; Jump if none pressed.
mov ah, 0h ; GET KEYSTROKE
int 16h
cmp al, 6bh ; Check if it is 'k'.
jne loop1 ; If not, continue. Keybuffer is now empty.
当你 post SO 上的一些源代码时,它应该附有它是如何编译和链接的信息,也 select 适当 tags.