汇编8086监听键盘中断

Assembly 8086 listening keyboard interrupt

我和这个问题完全一样:Listen to keyboard while drawing

但是第一个答案(已接受的答案)只听键盘一次。那么我该如何修改我的代码,以便我可以多次监听键盘中断。

这是我的代码:

.model small
draw_row Macro x
    Local l1
; draws a line in row x from col 10 to col 300
    MOV AH, 0CH
    MOV AL, 4
    MOV CX, 0
    MOV DX, x
L1: INT 10h
    INC CX
    CMP CX, 320
    JL L1
    EndM
.stack 100h
.data
height1 dw 51 
width1 dw 65
 v1 dw ?
 v2 dw ?
CUR_POSX_USER  DW 61
CUR_POSY_USER  DW 75
VAR DB ?
POSX DW 100
POSY DW 141


CAR_X DW  '0','0','0','0','0','0'
CAR_Y DW   '0','0','0','0','0','0'
CAR_TIMEM DW   '0','0','0','0','0','0'
CAR_TIMES DW   '0','0','0','0','0','0' 
RANDOM DW 257
TDELAY DW ?

.code

drawcar proc

    PUSH AX
    MOV V1, CX
    MOV AX,WIDTH1
    ADD V1,AX
    MOV V2, DX
    MOV AX,HEIGHT1
    ADD V2,AX
    POP AX
    MOV AH, 0CH

    MOV BX,V1
L1: 
    INC CX

    PUSH DX 
       l2:INT 10H
          INC DX
          CMP DX,V2
          JL L2
    POP DX
    CMP CX, V1        
    JL L1
    ;MOV AH,0
    ;int 16H

    ret



    RET 
drawcar endp
main proc
MOV AX,@DATA
MOV DS,AX
call drawscreen

INFINITE:

         MOV AL,1
         MOV CX,CUR_POSX_USER
         MOV DX,CUR_POSY_USER
         CALL DRAWCAR 
         CALL DRAW1
         CALL LISTEN_KEYBOARD



         JMP INFINITE
main endp

DELAY PROC
PUSH AX
PUSH CX
PUSH DX
PUSH BX
PUSH DI


  MOV DI, TDELAY
  MOV AH, 0
  INT 1Ah
  MOV BX, DX
  GO1:
  MOV AH, 0
  INT 1Ah
  SUB DX, BX
  CMP DI, DX
  JA GO1
       POP DI
       POP BX
       POP DX
       POP CX
       POP AX 


     RET  



DELAY ENDP


LISTEN_KEYBOARD PROC

   PUSH AX
   MOV AH,1
   INT 16H
   CMP AH,48H
   JE UP
   CMP AH,50H
   JE DOWN
   JMP GO
   UP:
      CMP CUR_POSY_USER,66
      JL GO
      CALL REMOVECAR
      SUB CUR_POSY_USER,66
      PUSH CX
      PUSH DX
      MOV CX,CUR_POSX_USER
      MOV DX,CUR_POSY_USER
      MOV AL,1
      CALL DRAWCAR
      POP DX
      POP CX
      JMP GO
   DOWN:
       CMP CUR_POSY_USER,133
       JG GO
       CALL REMOVECAR
       ADD CUR_POSY_USER,66
       PUSH CX
       PUSH DX
       MOV CX,CUR_POSX_USER
       MOV DX,CUR_POSY_USER
       MOV AL,1
       CALL DRAWCAR
       POP DX
       POP CX
       JMP GO 
    GO:
      MOV ZF,1
      POP AX
      RET   



LISTEN_KEYBOARD ENDP
REMOVECAR PROC
PUSH CX
PUSH DX

MOV AH,0CH
MOV DX,CUR_POSY_USER
MOV CX,CUR_POSX_USER
CALL DRAWCAR

POP DX
POP CX
RET




REMOVECAR ENDP


drawscreen proc
mov al,13H
mov ah,0h
int 10h
mov ax,10
draw_row 66
draw_row 133

ret
drawscreen endp

DRAW1 PROC


MOV CX,POSX
MOV DX,POSY
MOV AL,0
CALL DRAWCAR         

SUB POSX,5
MOV CX,POSX
MOV DX,POSY 
MOV AL,15
CALL DRAWCAR
MOV TDELAY,5
CALL DELAY


RET    
DRAW1 ENDP

end main

在您的 LISTEN_KEYBOARD 程序中,您检查了键盘,但在 比较 AH 寄存器中的扫描码之前未能解释 ZeroFlag 。如果设置了 ZF,则 AH 寄存器中没有任何重要内容!

PUSH AX
MOV AH,1
INT 16H
jz GO               Add this line.
CMP AH,48H
JE UP
CMP AH,50H
JE DOWN
JMP GO

在您写下的标签 GO 下方 MOV ZF,1。我在你的程序中没有看到任何变量 ZF。如果你想设置 ZF 然后使用 mov ah,40h sahf.

终于解决了多次听到键盘的问题,你需要实际删除你刚刚使用的键。

PUSH AX
MOV AH,1
INT 16H
jz GO
mov ah,0            Add these lines.
int 16h             Add these lines.
CMP AH,48H
JE UP
CMP AH,50H
JE DOWN
JMP GO