Page 1 of 1

problem in interrupt handler

Posted: Wed May 02, 2012 3:51 am
by vjain20
Hi,

I am trying to set up interrupt handlers and get interrupts working in my operating system.
But I am having some problem due to which bochs restarts.
Following is the code for interrupt handlers
extern test_interrupt_handler

%macro isr 1
global int%1_handler

int%1_handler:
cli
pusha
push %1
call test_interrupt_handler
pop ax
popa
sti
iret
%endmacro

isr 0
isr 1
..
isr 31

I have defined handlers for all exceptions 0 through 31. Each handler pushes the interrupt number on stack and calls the function - test_interrupt_handler which is a C function that reads the interrupt number from stack and displays it.

void test_interrupt_handler(uint16 intrnum)
{
ConsoleWriteString("\nInterrupt Number: "); //print string
ConsoleWriteAsDec(intrnum); // print in decimal format
ConsoleWriteChar('\n'); //print character
}
The problem is that when I generate any exception from main using INT instruction , the corresponding handler runs but the system restarts after that. I thought that it must be one of the exceptions but none of handler gets called after the first handler. I know that the popping value into ax is causing the problem as when I comment it the code runs fine. But I was thinking that pusha and popa instructions should restore the ax back to its original value and there shouldn't be any problem. I am unable to understand the cause of the problem.

Thanks

Re: problem in interrupt handler

Posted: Wed May 02, 2012 4:35 am
by Combuster
Do you know the difference between IRET and IRETD?

(and please remove the colours in your signature if you expect me to be able to read it)

Re: problem in interrupt handler

Posted: Wed May 02, 2012 4:47 am
by bluemoon
pop ax may get you in trouble, are you sure you want 16bit pop? are you in real mode?

Re: problem in interrupt handler

Posted: Wed May 02, 2012 6:18 am
by xenos
Looks perfectly fine for 16 bit / real mode interrupt handling to me. The "iret", "pop ax" and 16 bit parameter size in "test_interrupt_handler(uint16 intrnum)" are pretty consistent.

Two questions: Did you make sure that your assembler and compiler both generate 16 bit code? Does your compiler prepend an underscore to function names?

Re: problem in interrupt handler

Posted: Wed May 02, 2012 11:57 am
by Combuster
C code and Bochs actually restarting usually means protected mode (because both are difficult to do in real mode), which in turn makes it unlikely that 16 bits is correct.

(And either way, intnum should be an uint8_t, not 16 or 32 bits because there are only 256 interrupts).

Re: problem in interrupt handler

Posted: Wed May 02, 2012 4:23 pm
by vjain20
Thanks! I got it working. I was thinking that push would by default push 16 bit value.
I am sorry I did not mention that I am working in 32 bit mode. As far as IRET is concerned I saw tutorials
using IRET in 32 but mode so I used it too and it is working fine.