In intel instruction, idiv(integer divsion) means signed division.
I got the result of idiv
, but I don't quite understand the result.
- Example
0xffff0000 idiv 0xffff1100
- My wrong prediction
As long as I know, quotient
should be 0, and remainder
should be 0xffff0000 and because...
0xffff0000 / 0xffff1100 = 0
0xffff0000 % 0xffff1100 = 0xffff0000
- However, the result was...
Before idiv
eax 0xffff0000 # dividend
esi 0xffff1100 # divisor
edx 0x0
After idiv
eax 0xfffeedcc # quotient
edx 0x7400 29696 # remainder
- Question.
The result was value I couldn't expected.
Could someone explain about signed division(idiv
)?
- Appended.
Here's More information about idiv
.idiv
uses the eax register as a source register.
As a result of execution, quotient is stored at eax, and remainder is stored at edx.
idiv
divides edx:eax by the explicit source operand. See Intel's instruction manual entry.
Since edx is 0, edx:eax is a positive number. You are dividing 4294901760 by -61184, giving -70196 with a remainder of 29696.
Remember that both dividend (EDX:EAX) and divisor (ESI in your case) are interpreted as 2's complement signed numbers, so any bit-pattern with the high bit set is negative.
00000000ffff0000 = 4294901760
ffff1100 = -61184
fffeedcc = -70196
7400 = 29696
You should sign extend eax into edx using cdq
before using idiv, instead of zero-extending by zeroing EDX.
However, that still won't give the results you were expecting, because -65536 divided by -61184 equals 1 with a remainder of -4352.
(A negative dividend and positive divisor would give a negative remainder: X86 IDIV sign of remainder depends on sign of dividend for 8/-3 and -8/3?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With