Febriyanto Nugroho Febriyanto Nugroho - 6 months ago 13
Linux Question

Conditional eax != 0 and edx == 0?

I have a example code like this :

global _start
section .text

_start:

div eax
int 0x80


after I compile and run it, the output is :

Floating point exception (core dumped)


My question is :


  1. whether this is conditional on the
    eax != 0 and eax == 0
    ?

  2. what the purpose of
    Floating point exception (core dumped)
    ?


a3f a3f
Answer

The name is a bit misleading, as you have no floating point arithmetic in your code snippet.

In C, division by zero is undefined behavior, which means you can't portably assume a specific behavior (or more correctly, you have to expect that anything could happen).

On the x86, that anything is a hardware exception. One of the main responsibilities of an operating system is handling these hardware exceptions. Linux handles a division by zero by signalling SIGFPE to the offending process.

Most processes can't continue running after a division by zero (Because really what value should the program continue with?) and thus dump core and abort with a return value indicative of the signal that made it abort.

Your shell then interpreted the return value of your pogram and wrote Floating point exception (core dumped) on your terminal.

It doesn't have to be always like that though. Virtual machines like Java's have a handler for divison by zero that throws a java-specific ArithmeticException instead, that you can handle without aborting your program.

In your code, the division by zero was because your uninitialized eax had a zero in it.

Comments