Author Topic: How do I reset the interrupt state of the keyboard in my own INT 09h code?  (Read 1490 times)

Offline ben321

  • Full Member
  • **
  • Posts: 182
I want to intercept IRQ1 (which is at INT 09h) in order to directly process keystrokes in my program. But there's a problem, it seems to not properly reset the state of the keyboard after being called once. So the keyboard buffer fills up as not all keystrokes get processed. Here's a copy of my code with comments explaining what each line of code is for. I hope someone can tell me why it's not working (I've tested it in DOS Box Debugger and it always tells me that the keyboard buffer is getting filled instead of processing keystrokes correctly).

Code: [Select]
org 0x100 ;use this as the starting address offset for the program as this is a DOS COM file
USE16 ;make sure that 16bit instructions are being used



Main:
push es ;push original es segment register for later recovery
push 0 ;push 0 on stack
pop es ;pop 0 from stack so as to set es register to 0
mov word es:[9*4],IRQ1 ;write address offset for IRQ1 routine to IVT
mov es:[9*4+2],cs ;write address segment of code segment (segment containing IRQ1 routine) to IVT
pop es ;restore original es segment register from stack
jmp $ ;wait in endless loop so program doesn't end before keyboard input has been sent


IRQ1: ;interrupt associated with keyboard activity
in al,0x60 ;read PS/2 data register to get key scan code and reset keyboard state so it's ready for next keypress
iret ;return from interrupt
« Last Edit: February 13, 2023, 07:24:30 AM by ben321 »

Offline ben321

  • Full Member
  • **
  • Posts: 182
Re: How do I reset the interrupt state of the keyboard in my own INT 09h code?
« Reply #1 on: February 13, 2023, 08:54:36 PM »
I found what I needed, but this info was VERY hard to find, as Googling for things like "resetting the state of the keyboard interrupt in x86" just got search results showing other people asking what I was asking. No really useful answers. Only one tiny hint I did find from Google (via reading an answer to someone's question) mentioned sending the EOI (end of interrupt) signal but didn't explain how to actually do that. It took a couple hours of research deep in programming tutorials to find the code I was missing. All that work just to find these 2 lines of code that I needed. That code being:
Code: [Select]
mov al,0x20
out 0x20,al
This sends the number 0x20 to I/O port 0x20 to reset the interrupt state to allow it to be used again. I don't know why Google couldn't have directly pointed me to this answer, but I guess Google isn't as smart as everybody thinks it is.

And I'm also kinda shocked, that on an ASM forum like this, for an assembler capable of writing 16bit code for DOS, that after all these hours the post has been up nobody here seemed to know the answer either (as nobody replied to my post, other than myself).
« Last Edit: February 13, 2023, 09:09:03 PM by ben321 »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
Re: How do I reset the interrupt state of the keyboard in my own INT 09h code?
« Reply #2 on: February 14, 2023, 12:31:42 PM »
Here's a good reference for legacy I/O address space (in the PDF attached). And take a look at the legacy PIC (Programmable Interrupt Controller) and KBC (KeyBoard Controller) -- you can find about them in OSDev site.

And this question is VERY... VERY... basic. You could make a better effort to find this info, that it's NOT hard to find.
« Last Edit: February 14, 2023, 12:34:11 PM by fredericopissarra »