我读了一本关于汇编的书。它给出了键盘 I/O 驱动程序的示例:
section .data
ESC_KEY EQU 1BH ; ASCII code for ESC key
KB_DATA EQU 60H ; 8255 port PA
section .text
global _start
_start:
key_up_loop:
;Loops until a key is pressed i.e., until PA7 = 0.
; PA7 = 1 if a key is up.
in AL, KB_DATA ; read keyboard status & scan code
test AL, 80H ; PA7 = 0?
jnz key_up_loop ; if not, loop back
and AL,7FH ; isolate the scan code
..Translate scan code to ASCII code in AL..
cmp AL,0 ; ASCII code of 0 => uninterested key
je key_down_loop
cmp AL,ESC_KEY ; ESC key---terminate program
je done
display_ch:
; char is now in AL
..Print character AL to screen..
key_down_loop:
in AL,KB_DATA
test AL, 80H ; PA7 = 1?
jz key_down_loop ; if not, loop back
mov AX,0C00H ; clear keyboard buffer
int 21H ; (System interrupt)
jmp key_up_loop
Done:
mov AX,0C00H ; clear keyboard buffer
int 21H
..Exit Program..
我没有理解指令:test AL, 80H
,目的是什么以及它如何通过这种方式检查 PA7=0?
编辑:我也很高兴解释key_down_loop
部分。为什么我们需要它?我们可以进行下一个更改:
cmp AL,0 ; ASCII code of 0 => uninterested key
je key_up_loop
然后key_down_loop
的所有部分都是无用的。我错过了什么?
谢谢。
test
指令对操作数执行逻辑and
,并根据结果设置标志。操作数不会更改。test
指令是and
cmp
指示它sub
的内容。
出于某种奇怪的原因,"让我为你谷歌一下"不再允许在堆栈溢出上(也许是为了冒犯?(,所以直接链接:http://www.google.de/search?q=test+x86+instruction
PA7 显然在某处是位 7。我把它留给你找出如何转化为80H
.