Search code examples
exceptionassemblyx86-64divisioninteger-division

Simple x86-64 division not working


For a simple test case for my compiler project, I'm trying to divide 88 by 11, but when I call idivq my program throws a Floating Point Exception. Here is the relevant section of generated code where the exception occurs:

# push 88
movq $88,%r10

# push 11
movq $11,%r13

# \
movq  %r10,%rax
idivq %r13

I have looked up examples of how to use div, and I thought I was following the same format, so I don't understand why I am getting an exception.


Solution

  • idiv concatenates rdx and rax before performing the division (that is, it is actually 128-bit division). If you want to do single-word division, put a zero in rdx. What you're getting is not an FP exception, but an integer overflow exception: there's something left over in rdx which is making the quotient too big to fit in the destination register.