0

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
user2499298
  • 177
  • 2
  • 5
  • 17
  • [On which platforms does integer divide by zero trigger a floating point exception?](https://stackoverflow.com/a/37266507) explains why SIGFPE for division overflow. – Peter Cordes Sep 21 '20 at 00:10

2 Answers2

3

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).

rcgldr
  • 27,407
  • 3
  • 36
  • 61
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

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
GJ.
  • 10,810
  • 2
  • 45
  • 62
  • Okay, thanks. I googled the error and the explanations were a little hard to understand; this is the first time I'm working with assembly on a real machine. – user2499298 Mar 23 '14 at 20:12