2012-10-30 30 views
2

我在Assembly 8086中制作了一个空间入侵者,用于测试我正在使用DOSBox。让我告诉你我的代码:如何阅读密钥而无需等待它,程序集8086

;-------------------- 
;Update hero 
;-------------------- 
update: 
call vsync 
call update_hero ; method to read keyboard 
call set_video 
call clear_screen 
call draw_hero 
jmp update 

现在的程序update_hero是:

update_hero proc 
    mov ah, 01h 
    int 16h 
    cmp al, 97 
    je left_pressed 
    cmp al, 100 
    jne none_pressed 
    inc hero_x 
    left_pressed: 
      dec hero_x 
    none_pressed: 
    ret 
update_hero endp 

正如你所看到的,我想读“a”或“d”为运动,但,它不工作,你能帮我弄清楚为什么吗?

我想要做的是从键盘上读取而不等待它,这就是为什么我使用子功能ah, 01h

干杯。

编辑

我检查中断here,修改了代码,而现在它的工作:

update_hero proc 
    mov ah, 01h ; checks if a key is pressed 
    int 16h 
    jz end_pressed ; zero = no pressed 

    mov ah, 00h ; get the keystroke 
    int 16h 

    begin_pressed: 
     cmp al, 65 
     je left_pressed 
     cmp al, 97 
     je left_pressed 
     cmp al, 68 
     je right_pressed 
     cmp al, 100 
     je right_pressed 
     cmp al, 81 
     je quit_pressed 
     cmp al, 113 
     je quit_pressed 
     jmp end_pressed 
     left_pressed: 
      sub hero_x, 2 
      jmp end_pressed 
     right_pressed: 
      add hero_x, 2 
      jmp end_pressed 
     quit_pressed: 
      jmp exit 
    end_pressed: 

    ret 
update_hero endp 

回答

2

你只是检查是否有字符可用,但实际上并没有阅读来自缓冲区的字符。所以下次你检查时,它仍然存在。

从上BIOS功能http://webpages.charter.net/danrollins/techhelp/0230.HTM

INT 16H当前页,AH = 1

Info: Checks to see if a key is available in the keyboard buffer, and 
     if so, returns its keycode in AX. It DOES NOT remove the 
     keystroke from the buffer. 
+0

谢谢,我编辑了我的问题并添加了工作代码。 –

0

check_key:

mov  ah, 1 

int  16h 

jz  .ret 

mov  cx, 0 

xor  cl, ch 

mov  ah, 0 

int  16h 

.new.key: 

“人” 是新的密钥presh

.ret: 

ret 
0
update_hero proc 
mov ah, 01h 
int 16h 
cmp al, 97 
je left_pressed 
cmp al, 100 
jne none_pressed 
inc hero_x 

ret ; without the ret instruction "hero_x" will be decrease after increasing 

left_pressed: 
     dec hero_x 
none_pressed: 
ret 
update_hero endp 
相关问题