Jump to content
  • Advertisement
Sign in to follow this  
kordova

KeyUp/KeyDown in Assembly

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently writing a 2d side scroller in (tasm) assembly. My rendering and input occur once every 1/18.2 seconds (timer sets a flag enabling them). Now, my input works with:
    mov ah,11h
    int 16h
which checks for input (bailing the loop at fail) and
    mov ah,10h
    int 16h
grabs the input at success. So, I'm looping through all of the keypresses and setting flags as necessary which should work fine. However, because I'm working with keypresses I'm not getting very good response time in certain respects. Namely, if I'm standing still and then hit the right button (d, haven't looked into the code for the arrows quite yet, though I'm sure they work with that function as it caught in a console app before), I move, stop for a quarter/half of a second or so, and then continue. I'm assuming that this is because of the typematic rate settings. Is there another function that I can use to get a keyup or keydown (so I can have an array of states to work with instead)? Oh, and I've been googling and searching Ralf Brown's Interrupt List.

Share this post


Link to post
Share on other sites
Advertisement
If you want to avoid the typematic delays you should read the scancodes from the keyboard I/O port directly, rather than going via an interrupt. They keyboard port is located at 60h. Here is a page detailing how to read from the port. Note that keyboard scan codes aren't the same as the ASCII codes that you have probably been using with int 16h (though iirc you can also read the scan codes using int 16h).

cheers
sam

Share this post


Link to post
Share on other sites
Actually, I had been directed to that only moments before your response. I haven't used anything but interrupts so far, so I'm a bit confused by it. It gives examples as to how to put data in the buffer, but I can't find an explicit example of how to read something.

Here I found this segment:
read_kbd:
in al,64h ; read status byte
and al,01h ; wait for OBF==1
jz read_kbd
in al,60h ; read scancode byte
ret

and while I'm pretty sure that the scancode is ending up in al, is this what I should use?

Share this post


Link to post
Share on other sites
Yeah, I've been comparing al to 01h, 76h and 08h (esc for the various 3 settings) and none is firing off when I hit escape.

Share this post


Link to post
Share on other sites
Actually I've never bothered checking the status flags and it's always worked okay for me. Just doing:


in al, 60h
cmp al, 1
jne mainloop


should be sufficient. Comparing to 1 (for escape) should work.. If it isn't, there's something else wrong. I've successfully used the above (or a high-level equivalent) in demo code plenty of times.

cheers
sam

p.s. Here is a little platformer (sort of like Lode Runner) that I once wrote in assembly language with a very limited toolset (MS Edit, nasm, and MS Paint). It uses scancodes and the keyboard port for movement. Also keys can be remapped (it looks up the keypress in a table to find out what it should do). And here is a tiny fire routine in assembly language that also reads from the keyboard port in order to detect escape. It might be simpler to learn from than the platformer as there is much less code (although it is all optimised for size, so can be a bit confusing :-)).

Share this post


Link to post
Share on other sites
izzo: definately cool platformer considering the tools you used :) but it seems to me that it has the typematic delay as well... when i press left/right cursor key it does nothing and then after a few milliseconds it starts moving...
but very nice of you to share the code :)
good coincidence that i wanted to start learning assembler some days ago. i'm currently reading about different cpus and memory stuff... i'm sure i can learn something from your code when i reach the part when i can actually do something with assembler...
i'm not sure if i'll ever need assembler in my life but it was always a dream to know it. and it gives me a much better feeling of what goes on under the hood of a computer so i guess its worth learning.

Share this post


Link to post
Share on other sites
You may also be interested in the vsync flag, which is located -if I remember well- at port $3DA (AND with 8). This serves a dual purpose: you get vsync enabled output, which in my opinion is crucial in any 2D DOS game, and also have a more precise timer for your game (60Hz instead of 18).
Just a tip hope you can use it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prototype
You may also be interested in the vsync flag, which is located -if I remember well- at port $3DA (AND with 8). This serves a dual purpose: you get vsync enabled output, which in my opinion is crucial in any 2D DOS game, and also have a more precise timer for your game (60Hz instead of 18).
Just a tip hope you can use it.

Awesome, thanks. (I'm actually using it when rendering, to wait for it before draw, anyway.) That never occured to me.

Share this post


Link to post
Share on other sites
Quote:
Original post by izzo
Actually I've never bothered checking the status flags and it's always worked okay for me. Just doing:


in al, 60h
cmp al, 1
jne mainloop


should be sufficient. Comparing to 1 (for escape) should work.. If it isn't, there's something else wrong. I've successfully used the above (or a high-level equivalent) in demo code plenty of times.

cheers
sam

p.s. Here is a little platformer (sort of like Lode Runner) that I once wrote in assembly language with a very limited toolset (MS Edit, nasm, and MS Paint). It uses scancodes and the keyboard port for movement. Also keys can be remapped (it looks up the keypress in a table to find out what it should do). And here is a tiny fire routine in assembly language that also reads from the keyboard port in order to detect escape. It might be simpler to learn from than the platformer as there is much less code (although it is all optimised for size, so can be a bit confusing :-)).

Wow. That should really help me out a lot. Thanks very much!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!