装配8086监听键盘中断

时间:2016-05-20 07:31:29

标签: assembly x86-16 keyboardinterrupt

我有完全相同的问题: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

1 个答案:

答案 0 :(得分:1)

在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