Thread: Help using inline assembly for keyboard input

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    Question Help using inline assembly for keyboard input

    Hi, I am trying to get some assembly code to run in C. I am using mingw32 gcc compiler in a code blocks environment if that matters, and i want to get this code to store the keyboard's input to var kin.

    assembly --- in al, 64h
    From what I understand that line is suppodsed to read keyboard input and store it in the AL register.

    When I use this C code it compiles fine no errors or warning but when i execute it, it gives me a segfault and i cannot figure out how to fix this. I've tried looking at various sites for inline assembly but none of them cover using ports for i/o if some one has any websites i could view or code that would be very much appreciated.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void){
        while(1){
        char kin;
        asm(
            "movw  $0x64, %%dx;"
            "inb %%dx, %%al;"
            "movb %%al, %0;"
            :"=r"(kin)
            :
            :"%al", "%dx");
        printf("%c\n", kin);
        }
    }
    ( note i've also tried using the i/o address of 60h and still same result. Also i have tried making everything relavent to a byte, word or dword with regards to the registers the kin variable and the suffix letter after the asm instruction)

    I am not a big fan of assembly but i figure getting to know a little would be helpful and learning to use it in a C environment will keep me from having to dive into full Assembly language usage and using MASM NASM etc.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yeah, it might, IF you were using a 16-bit DOS compiler, capable of generating the original DOS format executable files.

    But you're not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by Salem View Post
    Yeah, it might, IF you were using a 16-bit DOS compiler, capable of generating the original DOS format executable files.

    But you're not.
    A friend of mine said that using the "in" operation is compatible with 32 bit asm, as he has assembled using masm and was able to run in windows, and shouldn't have anything to do with 16 bit dos. Which is why I'm using it and not a dos int.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Running under protected mode you don't have access to the IO bus, it won't work. Why not just use one of the million well defined api calls to get keyboard input, or if you REALLY wanna practice some old assembly download dosbox and have at it, although in and out still probably wont work.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by valaris View Post
    Running under protected mode you don't have access to the IO bus, it won't work. Why not just use one of the million well defined api calls to get keyboard input, or if you REALLY wanna practice some old assembly download dosbox and have at it, although in and out still probably wont work.
    If that's true though then how come when "in al, 64h" is assembled with MASM, the program receives the character pressed from the key and stores it in al why can i not do that in C even though I'm using the same regeisters and no errors appear when building? Not that I'm arguing I'm just wondering, sorry if it's a pain that i keep asking questions. >.<

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Because the OS doesn't let you meddle directly with its hardware.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Quote Originally Posted by quzah View Post
    Because the OS doesn't let you meddle directly with its hardware.


    Quzah.
    I kind of realize that but what I'm saying is why can i assemble "in al, 64h" in masm which will creat an exe right on my desktop that i can double click and run and get keyboard input but when i do the same exact lines of code in C (save the at&t syntax difference) it won't work.

    If i could use a simple asm line than my friend can assemble and read a character from the keyboard then it would be much simpler than messing around with the windows.h and windows api which i do not like one bit.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    MASM will create a 16-bit DOS executable.

    When you double-click on it, a virtual machine with a lot of magic behind the scenes creates an emulated environment which does what you expect of a 16-bit program.

    If you want access to the rest of windows goodies - high resolution graphics and sound say, then you're just going to have to suck up your dislike of windows.h.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline assembly code check
    By todd14 in forum C++ Programming
    Replies: 7
    Last Post: 09-13-2007, 03:14 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM