problem in interrupt handler

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

problem in interrupt handler

Post 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
- Thanks
Vaibhav jain
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: problem in interrupt handler

Post 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)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: problem in interrupt handler

Post by bluemoon »

pop ax may get you in trouble, are you sure you want 16bit pop? are you in real mode?
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: problem in interrupt handler

Post 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?
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: problem in interrupt handler

Post 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).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
vjain20
Member
Member
Posts: 73
Joined: Wed Apr 04, 2012 9:12 pm

Re: problem in interrupt handler

Post 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.
- Thanks
Vaibhav jain
Post Reply