Page 1 of 1

Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 3:46 am
by osdf_1
I set up my IDT and ISRs with C and ASM. When I call:

Code: Select all

__asm__ __volatile__("int $0x0");
the exception message is displayed and everything seems to work perfectly. But, should the exception be thrown if I put:

Code: Select all

int x = 5 / 0;
in my main function? Because it isn't.

Thanks

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 3:56 am
by c0x
You should check the disassembled output of your binary because if I'm right, GCC will "optimize" this stuff out..

To really trigger a division by zero exception you should do the division with variables instead of constants :)

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 3:57 am
by octa
is there a triple fault !!!!!

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 3:59 am
by AJ
Hi,

If you do not use the variable x, this line will be optimised out of your code. Memory writes, however, cannot be optimised out, so:

Code: Select all

int x[10];
x[0] = 5/ 0;
..or similar should give you the exception still.

Cheers,
Adam

[edit]too slow :) [/edit]

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 4:08 am
by osdf_1
Well it didn't give me an exception.. How do I check the disassembled output?

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 4:13 am
by JamesM
AJ wrote:Hi,

If you do not use the variable x, this line will be optimised out of your code. Memory writes, however, cannot be optimised out, so:

Code: Select all

int x[10];
x[0] = 5/ 0;
..or similar should give you the exception still.

Cheers,
Adam

[edit]too slow :) [/edit]
However, it can optimise out writes to stack-local variables. 'x' is never used except in the write, so both lines can be optimised out.

A global variable however, could be referenced from anywhere and so the compiler should never optimise away a write:

Code: Select all

int x;
int myfunc()
{
  x = 3 / 0;
}
This works for my kernel, for testing purposes!

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 4:16 am
by AJ
:oops: Whoops - sorry. That's what comes of trying to write an example (even if it's that simple) off the top of your head without testing [-X . Apologies to OP.

Cheers,
Adam

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 4:17 am
by Korona
AJ wrote:If you do not use the variable x, this line will be optimised out of your code. Memory writes, however, cannot be optimised out, so:

Code: Select all

int x[10];
x[0] = 5/ 0;
Actually, Memory writes can be optimized if you don't use the volatile keyword:

Code: Select all

volatile int x[10];
x[0] = 1;
x[1] = 0;
x[2] = x[0] / x[1];

Re: Exception not thrown when dividing by zero

Posted: Mon Jul 28, 2008 4:27 am
by osdf_1
Wow, finally I got it to work using Korona's example, thanks to everyone.

Re: Exception not thrown when dividing by zero

Posted: Thu Jul 31, 2008 8:09 am
by xlq
Regardless of what context you use (5 / 1) in, (5 / 1) is still a constant and can still be calculated at compile time. I'm surprised that (5 / 0) didn't produce a compiler error, though.

Re: Exception not thrown when dividing by zero

Posted: Thu Jul 31, 2008 8:19 am
by AJ
Hi,

I've just checked my cross-compiled GCC and it just gives a warning (unless, of course, -Werror is specified, which it always should be!).

Cheers,
Adam