assemblyx86

Getting floating point exception while trying to use div in assembly


I am trying to run the following code in assembly:

  mov        %si, %ax
  mov        $15, %si
  div        %si
  mov        %eax, %esi

When I make my program, it compiles, but at runtime it gives me a floating point exception. I tried to replace the last line's parameters by %ah and %si.

Briefly, I am trying to divide %esi by 15. I only want an int, and have no need for a double.

Thank you


Solution

  • The div instruction divides the double-word parameter dx:ax by the operand. The dx:ax form means the doubleword formed by regarding ax as the low word and dx as the high word. If the quotient is too large to fit into a word, it will throw that exception.

    Reference: https://www.felixcloutier.com/x86/div

    What do you have in the dx register? Most likely dx:ax divided by 15 does not fit in a 16-bit word.