title: How to get keyboard input with x86 bare metal assembly?
body: <p>I'm in the process of trying to hack together the first bits of a kernel. I currently have the entire kernel compiled down as C code, and I've managed to get it displaying text in the console window and all of that fine goodness. Now, I want to start accepting keyboard input so I can actually make some use of the thing and get going on process management.</p>
  
  <p>I'm using DJGPP to compile, and loading with GRUB. I'm also using a small bit of assembly which basically jumps directly into my compiled C code and I'm happy from there.</p>
  
  <p>All the research I've done seems to point to an ISR at $0x16 to read in the next character from the keyboard buffer. From what I can tell, this is supposed to store the ASCII value in ah, and the keycode in al, or something to that effect. I'm attempting to code this using the following routine in inline assembly:</p>
  
  <pre><code>char getc(void) 
  {
      int output = 0;
  
      //CRAZY VOODOO CODE
      asm("xor %%ah, %%ah\n\t"
          "int $0x16"
          : "=a" (output)
          : "a" (output)
          : 
  
          );
  
      return (char)output;
  }
  </code></pre>
  
  <p>When this code is called, the core immediately crashes. (I'm running it on VirtualBox, I didn't feel the need to try something this basic on real hardware.)</p>
  
  <p>Now I have actually a couple of questions. No one has been able to tell me if (since my code was launched from GRUB) I'm running in real mode or protected mode at the moment. I haven't made the jump one way or another, I was planning on running in real mode until I got a process handler set up.</p>
  
  <p>So, assuming that I'm running in real mode, what am I doing wrong, and how do I fix it? I just need a basic getc routine, preferably non-blocking, but I'll be darned if google is helping on this one at all. Once I can do that, I can do the rest from there.</p>
  
  <p>I guess what I'm asking here is, am I anywhere near the right track? How does one generally go about getting keyboard input on this level? </p>
  
  <p>EDIT: OOhh... so I'm running in protected mode. This certainly explains the crash trying to access real mode functions then.</p>
  
  <p>So then I guess I'm looking for how to access the keyboard IO from protected mode. I might be able to find that on my own, but if anyone happens to know feel free. Thanks again.</p>