user6181605
user6181605

Reputation: 59

Real time keypress event assembly x86 TASM

I'm trying to make a Snake game in TASM, but I have a problem; I can't seem to figure out how to keep the Snake moving while waiting for a keypress, because ah = 0 and int 16h is stopping the Snake and waiting for a keypress. Can someone please help me with that?

Upvotes: 2

Views: 2625

Answers (1)

You can use int 21h, ah=0BH, to check if a key was pressed, without stopping the program, example :

game:
;CHECK IF KEY WAS PRESSED.
  mov ah, 0bh
  int 21h      ;◄■■ RETURNS AL=0 : NO KEY PRESSED, AL!=0 : KEY PRESSED.
  cmp al, 0
  je  move_snake
;PROCESS KEY.        
  mov ah, 0
  int 16h      ;◄■■ GET THE KEY.
move_snake:

  jmp game

Full example (no snake, of course) :

.model small
.stack 100h
.data
.code
  mov ax, @data
  mov ds, ax
game:
;CHECK IF KEY WAS PRESSED.
  mov ah, 0bh
  int 21h  
  cmp al, 0
  je  move_snake
;PROCESS KEY.        
  mov ah, 0
  int 16h
  mov ah, 2
  mov dl, al
  int 21h       ;◄■■ DISPLAY PRESSED KEY.
move_snake:
  mov ah, 2
  mov dl, '.'
  int 21h       ;◄■■ DISPLAY SOMETHING.
  jmp game

  mov ax, 4c00h
  int 21h

Upvotes: 3

Related Questions