Question

I keep getting 'Program received signal SIGFPE, Arithmetic exception' when dividing in x86 Assembly. It's confusing because the answer should be smaller than a 64 bit answer if I divide by 10....

mov $0x82b40000, $eax
mov $0x21c3677c, $edx
mov $10000000, %ebx
div %ebx
Was it helpful?

Solution 2

The arithmetic exception is "Integer overflow", from the #DE hardware divide exception! Which is normal or expected, because your result is bigger than 32-bit number.

Remember: the quotient of 64 bit / 32-bit division is only a 32 bit register (EAX). The EDX output is the remainder, not high half of the quotient. The operand-size of div %ebx is 32-bit; only the dividend is 64-bit.

Intel's datasheet has a useful table:

enter image description here

OTHER TIPS

You'll need to do this division similar to long hand division by hand. Put the dividend in another pair of registers or memory. Then clear edx and load eax with the high order dividend. Then divide edx:eax by the 32 bit divisor, and store eax (quotient) back into high order dividend. Next load eax with low order dividend (leaving edx alone), and divide by the 32 bit divisor again. Store eax back into low order dividend. After this, high and low order dividend = dividend/divisor (a 64 bit quotient), and edx = dividend % divisor (32 bit remainder).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top