【问题标题】:How to read key without waiting for it, assembly 8086如何不等待就读取密钥,程序集 8086
【发布时间】:2012-10-30 16:42:59
【问题描述】:

我正在Assembly 8086 中制作太空入侵者,用于测试我正在使用DOSBox。让我给你看我的代码:

;--------------------
;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

【问题讨论】:

    标签: assembly keyboard x86-16 keystroke


    【解决方案1】:

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

    从此页面了解 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.
    

    【讨论】:

      【解决方案2】:

      check_key:

      mov     ah, 1
      
      int     16h
      
      jz      .ret 
      
      mov     cx, 0
      
      xor     cl, ch
      
      mov     ah, 0
      
      int     16h
      
      .new.key:
      

      “al”是新的key presh

      .ret:
      
       ret
      

      【讨论】:

        【解决方案3】:
        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-25
          • 1970-01-01
          • 1970-01-01
          • 2017-03-18
          • 1970-01-01
          相关资源
          最近更新 更多