flat assembler
Message board for the users of flat assembler.

Index > DOS > Very very simple keyboard input?

Author
Thread Post new topic Reply to topic
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 06 Apr 2005, 08:33
Can someone give me an example of simple, but very fast way to get a pressed button. I've searched, but found only complex ways of doing it, some ways were too slow and some didn't work...

_________________
Better to rule in hell, than to be a slave in heaven...
Post 06 Apr 2005, 08:33
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 06 Apr 2005, 09:59
Code:
mov ah, 8 ;non echoed input
int 21h

; al now equals the character pressed
    

use mov ah, 1 if you want the input character to be echoed
Post 06 Apr 2005, 09:59
View user's profile Send private message Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 06 Apr 2005, 13:01
Thanks. But I need it to be accessed directly (so the program can continue working if no button was pressed) I don't know if there's a simple way of doing that... Confused And I need it to be as fast as it can be.

_________________
Better to rule in hell, than to be a slave in heaven...
Post 06 Apr 2005, 13:01
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1166
Location: Overflow
Matrix 06 Apr 2005, 16:52
Hello
please go here

there is no simpler way to read keyboard directly,

if you're willing to use interrupts, it will be very slow.
speed - method
fast - directly via keyboard port
slow - via bios keyboard handling interrupt
very slow - via dos keyboard handling
Post 06 Apr 2005, 16:52
View user's profile Send private message Visit poster's website Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 06 Apr 2005, 17:00
That's what I was trying to get to work... It didn't work. Maybe I should try again...
Post 06 Apr 2005, 17:00
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 09 Apr 2005, 17:00
Why not do this:
Code:
        xor   ax,ax        in    AL,60h         test  al,10000000b        jnz   NoKey; Yes a key has been pressed, do some thing hereNoKey:;Carry on with the loop here, as no key pressed    


let me know if it for a game.
Post 09 Apr 2005, 17:00
View user's profile Send private message Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 10 Apr 2005, 10:29
I don't know if you can call it a game, but it needs to be fast. It has significant speed decrease when using your code... I only need it to exit when a key was pressed. Or there's no faster way than this one?

_________________
Better to rule in hell, than to be a slave in heaven...
Post 10 Apr 2005, 10:29
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 10 Apr 2005, 11:35
You could set up an interrupt handler for the keyboard IRQ, and handle keys on interrupt rather than keep polling...
Post 10 Apr 2005, 11:35
View user's profile Send private message Visit poster's website Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 10 Apr 2005, 13:57
Maybe you could give me an example, or lead me to some guide? I don't know much about assembly programming yet... Confused

_________________
Better to rule in hell, than to be a slave in heaven...
Post 10 Apr 2005, 13:57
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 10 Apr 2005, 17:22
This will replace Dos keyboard interrupt, for yours (call this bit, at start of program)
Code:
MOV     AL,09h                    ; Get INT 09h addressMOV     AH,35hINT     21hMOV     [oldint9],BX              ; Save it for laterMOV     [oldint9+2],ESMOV     AL,09h                    ; Set new INT 09hMOV     DX,Newint9        ; DS:DX = new interruptMOV     AH,25hINT     21h         

This is you new interrupt so do what needs to be done on key press here;
Code:
Newint9: pusha   ;Do some thing here       mov     al, 0x20out     0x20, al        ; If you do something like, jump exit do it here, after the above code.     popaIRET    

This needs calling at end of program
Code:
ReStoreInt9:MOV     AL,09h                    ; Restore original INT 09hMOV     DX,[oldint9]MOV     DS,[oldint9+2]            ; Move old INT 09H pointer to DS:DXMOV     AH,25hINT     21hRET    


Note: Most of this code is from a TASM Game, i wrote, so may need converting to FASM.
You will need to set up var for saving "old INT 09H " eg:
oldint9 dd 0
Post 10 Apr 2005, 17:22
View user's profile Send private message Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 10 Apr 2005, 17:45
Thank you!! Very Happy
Where is the button code stored in the interrupt? And could you explain me what does mov al, 0x20 out 0x20, al do?

_________________
Better to rule in hell, than to be a slave in heaven...
Post 10 Apr 2005, 17:45
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 10 Apr 2005, 18:20
When you press a key a interrupt is generated, which jump from your program to some code to deal with the IRQ, at the end of a IRQ you do the above code, so it now its the end.

As for the button code, i left it out as you said you only need to exit on keypress so soon as a key is press it jump to that code, you could try something like this ?
Code:
Newint9: call     Newint9ajmp    exitret    

Code:
Newint9a:pusha   ;Do some thing here       mov     al, 0x20out     0x20, al        ; If you do something like, jump exit do it here, after the above code.     popaIRET    


I do not know if it will work as its a long time since using dos. at the exit lable you need to call ReStoreInt9 before exiting.
Post 10 Apr 2005, 18:20
View user's profile Send private message Reply with quote
Giedrius



Joined: 13 Feb 2005
Posts: 40
Location: Lithuania
Giedrius 10 Apr 2005, 18:34
I've done everything that I wanted. Thanks. I only asked for some explanation about it.

_________________
Better to rule in hell, than to be a slave in heaven...
Post 10 Apr 2005, 18:34
View user's profile Send private message Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach 11 Apr 2005, 06:32
Code:
mov     al, 0x20
out     0x20, al    

=
on my forum, Dex4u wrote:
There are two types of interrupts a software and a hardware, when you write a "int 21h" you get a software int and when a key is pressed you get a hardware int.

A interrupt causes the procsseeor to suspend the current operation and act on the reason for the interruption, 8259 masks all further interrupts from that device until is receives an end of interrupt signal from the interrupt service routine( that is the above code). Once the interrupt has been handled, it returns to the interrupted program.


And how to actually get the scancodes? Do like this:
Code:
          xor     eax, eax           ; clear eax
          in      al, 0x60             ; mov scan-code into al

          test    al, 0x80            ; check if it was a press or a release
          jnz     .key_up

                                         ; check diffrent keys here:
          cmp     al, 42             ; this checks for a press on the left shift
          jnz     .check_next      ; nope, that wasn't pressed, check next

      .key_up:
    
Post 11 Apr 2005, 06:32
View user's profile Send private message Reply with quote
Dilshod



Joined: 23 Feb 2005
Posts: 23
Location: Uzbekistan, Tashkent
Dilshod 12 Apr 2005, 09:44
Try this:
Code:
    mov   ah,1
    int      16h
    jz       .NoKeyPressed
    mov    ah,0
    int       16h
    ;...
    ;... KeyCode in AX, AL-ASCII, AH-Scan
    ;...
.NoKeyPressed:
    
Post 12 Apr 2005, 09:44
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 12 Apr 2005, 11:37
Post 12 Apr 2005, 11:37
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.