简体   繁体   中英

Get two keys at the same time - Assembly 8086

I am building a game in assembly, with two players. So I'm using the following code to get the current key pressed:

mov ah, 01h
int 16h

This code gives me the Scan code and the Ascii code of the pressed key The problem is that two players might press 2 different keys on the same time. How do I receive two keys at the same time?

I have tried few ways to achieve that. I tried getting the input directly by using in al,060h . I also tried int 9h but still it didn't work.

If memory serves, with a touch of Googling to refresh myself on the numbers...

The BIOS doesn't offer what you want. It's managing an input stream of text characters. Which is strongly related to catching key down events but will not allow you to tell which keys are depressed now, only which keys at some point weren't depressed and then became depressed in the past.

You need to use int 21h , function 25h to install your own handler for int 9h . That way you'll get the key up and key down events directly from the hardware.

Within your handler, read port 60h to determine why the interrupt has occurred. The low seven bits are a key code. If the top bit is clear then the key has transitioned to pressed. If the top bit is set then the key has transitioned to unpressed.

You then need to out 20h to port 20h to acknowledge the interrupt.

So you might keep a table of 128 bytes, initialised as all 80h . In your handler, just store value to value&7h .

In your actual game code, if you want to know whether key n is pressed at that moment, read the nth value from your table and branch on sign. Positive = pressed, negative = unpressed.

(Addendum: you should also get the existing vector when you launch and restore it before you exit, or else you've just accidentally written the hooking part of a TSR, probably without doing the SR part)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM