汇编8086监听键盘中断 [英] Assembly 8086 listening keyboard interrupt

查看:261
本文介绍了汇编8086监听键盘中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与此完全相同的问题:在绘制时监听键盘

I have exactly the same question as this: Listen to keyboard while drawing

但是第一个答案(被接受的答案)仅听键盘一次.因此,我该如何修改我的代码,以便可以监听键盘中断多一遍.

But the first answer (accepted one) listens keyboard for exactly once. So how can I modify my code so that I can listen keyboard interrupt for more then once.

这是我的代码:

.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寄存器中没有任何重要意义!

In your LISTEN_KEYBOARD procedure you check the keyboard but you fail to interpret the ZeroFlag before comparing the scancode in the AH register. If the ZF is set then there is nothing of any importance in the AH register!

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.

Below the label GO you've written MOV ZF,1. I don't see any variable ZF in your program. If you want to set the ZF then use mov ah,40h sahf.

最后一次解决不止一次聆听键盘的问题,您需要真正移除刚刚使用的琴键.

Finally to solve the issue of listening to the keyboard more than once you need to actually remove the key that you've just used.

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

这篇关于汇编8086监听键盘中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆