Ok ill explain just a little because there isn't more to know actually.
I have this code with a few calculations over EAX.
EAX contains a signed number, and i need to do a division on this numver. Now i discovered if i do it with IDIV i get a weird result when EAX contains a negative number (same in windows calculator), so what i do is i check if EAX is negative, if so i change EAX to positive use IDIV and then make it negative again.
if EAX contains a positive number i just do IDIV, but i think there should be a better way to do this, but since this is my first contact with signed numbers i cant find it.

(my code is below for the people who are interested what i tryed to explain)


cmp eax, 0
jge Q1
neg eax
idiv sdword ptr ds:[ebx+8]
neg eax
jmp Q2
idiv sdword ptr ds:[ebx+8]
...rest of code...
Posted on 2004-11-29 13:37:45 by Scorpie
Remember to sign extend EAX first using the CDQ instruction.
Posted on 2004-11-29 14:04:32 by Sephiroth3
thanks ill look it up :)

edit: why does my post say Smile and doesnt it show a smilie? (disable smilies checkbutton is unchecked)

more edit: sorry but maybe i dont understand the command correctly but as i see it it extends a 16 / 32 bit value to a 32 / 64 bit signed value?
this is not what i want, i already have a signed DWORD which i need to divide, my only problem is the idiv doesnt work correctly on a negative number.
Posted on 2004-11-29 14:05:32 by Scorpie
It is exactly what you want because of the definition of IDIV. The dividend is the double-length number contained in EDX (high part) and EAX (low part). CDQ is how you get the equivalent signed 64-bit dividend needed by IDIV.

idiv divisor32bit
Posted on 2004-11-29 23:21:19 by tenkey
When you do a division by a 32-bit value, the value to be divided MUST be a 64-bit number, the High Order 32 bits being in EDX and the Low Order 32 bits in EAX.

If the value to be divided is only a 32-bit one, you must sign extend it into EDX:EAX for a signed division. That is the purpose of the CDQ instruction.

(For an unsigned division, you must zero the EDX register such as with: xor edx,edx).

Posted on 2004-11-29 23:28:35 by Raymond
ok thanks guys this helps me a lot :D (saves me a lot of unnessecary code), btw i noticed windows calculator has the same 'bug' in it it doesnt devide negatieve hex numbers correctly (if you go from negative decimal to hex then devide and back to decimal the awnser is not correct)
Posted on 2004-11-30 03:52:55 by Scorpie