What is signed division(idiv) instruction?

房东的猫 提交于 2021-01-19 07:02:21

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/54000965/what-is-signed-divisionidiv-instruction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!