Page 1 of 1

Tutorial 15 error

Posted: Thu Oct 01, 2009 3:17 am
by hahnski07
In Tutorial 15 you listed the default interrupt handler code as this:

Code: Select all

void int_handler_5 () {

	_asm add esp, 12
	_asm pushad

	// do whatever...

	_asm popad
	_asm iretd
}
The only way I could get it to work was for me to remove the add esp, 12 line.

Explanations?

Re: Tutorial 15 error

Posted: Thu Oct 01, 2009 3:38 am
by Mike
Hello,

Its usually recommended not changing the stack like the series does. A better workaround is either using an assembly language file for the IRQ handler and linking it in, or using __declspec (naked) (MSVC++ specific) which allows the function to be treated inline and you do not need to touch the stack

Code: Select all

__declspec (naked) void int_handler_5 () {

   // do whatever...

   _asm iretd
}
The reason for the crash is do to the _asm add esp, 12 changing the position of esp to a different value then what your current compiler configuration is set to output. add esp, 12 reverses the data pushed on the stack by the eprologue code that MSVC++ adds to the function. If it pushes a different amount of values then 12 bytes, then the instruction will, basically, make your stack (return eip) invalid causing a crash.

Using _declspec (naked) or assembly language eliminates the need to touch the stack.

Re: Tutorial 15 error

Posted: Fri Oct 02, 2009 2:50 am
by hahnski07
Well that makes sense because my entire kernel is written in assembly.