Exception not thrown when dividing by zero

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
osdf_1
Posts: 20
Joined: Sun Nov 25, 2007 7:55 pm

Exception not thrown when dividing by zero

Post 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
c0x
Member
Member
Posts: 26
Joined: Sun Apr 20, 2008 2:16 am

Re: Exception not thrown when dividing by zero

Post 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 :)
Last edited by c0x on Mon Jul 28, 2008 4:00 am, edited 1 time in total.
User avatar
octa
Member
Member
Posts: 50
Joined: Sat Jun 28, 2008 9:15 am

Re: Exception not thrown when dividing by zero

Post by octa »

is there a triple fault !!!!!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Exception not thrown when dividing by zero

Post 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]
osdf_1
Posts: 20
Joined: Sun Nov 25, 2007 7:55 pm

Re: Exception not thrown when dividing by zero

Post by osdf_1 »

Well it didn't give me an exception.. How do I check the disassembled output?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Exception not thrown when dividing by zero

Post 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!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Exception not thrown when dividing by zero

Post 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
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: Exception not thrown when dividing by zero

Post 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];
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].
osdf_1
Posts: 20
Joined: Sun Nov 25, 2007 7:55 pm

Re: Exception not thrown when dividing by zero

Post by osdf_1 »

Wow, finally I got it to work using Korona's example, thanks to everyone.
xlq
Member
Member
Posts: 36
Joined: Mon Dec 11, 2006 7:51 am

Re: Exception not thrown when dividing by zero

Post 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.
Marionette the flexible kernel
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Exception not thrown when dividing by zero

Post 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
Post Reply