5
votes

when I have the the operation

IDIV ecx

in assembly, then i have read that the that the value in edx:eax is divided by the operand ecx. I also know that the quotient is stored in eax and the remainder in edx.

so but what exactly is the value in edx:eax ??

Can someone explain it to me?

edit: i also did not understand the reason of "cdq" operation which come always before the IDIV operation although I have read some pages about that.

2
you should probably explain that you're talking about x86 ASM. "assembler" is a generic term, and not specific to any cpu/instruction set. - Marc B
Plus, your answer is already documented: en.wikipedia.org/wiki/X86_instruction_listings#Added_with_80386 - Marc B
R = X/Y. The numerator X of the division is in edx:eax and the denominator Y is in ECX. If you want to divide two numbers you have to supply those two numbers not just one of them. So you supply the numerator in edx:eax and denominator in ecx and do the division and get a result. - old_timer

2 Answers

5
votes

i have read that the that the value in edx:eax is divided by the operand ... but what exactly is the value in edx:eax ?

EDX:EAX in this context means the 64-bit value formed by the registers EDX and EAX, where EDX is interpreted as containing the most significant bits, and EAX the least significant bits.

CDQ converts the doubleword in EAX into a quadword in EDX:EAX by sign-extending EAX into EDX (i.e. each bit of EDX is filled with the most significant bit of EAX). For example, if EAX contained 0x7FFFFFFF you'd get 0 in EDX, since the most significant bit of EAX is clear. But if you had EAX = 0x80000000 you'd get EDX = 0xFFFFFFFF since the most significant bit of EAX is set.
The point of CDQ is to set up EDX prior to a division by a 32-bit operand, since the dividend is EDX:EAX.

1
votes

What is it? It's the value you put there because you want to divide it...

The CDQ instruction takes a 32-bit value in EAX and converts it into a 64-bit value in EDX:EAX (by copying the sign bit of EAX into every bit of EDX). You use it if (as is usual) the value you want to divide is 32-bit to begin with.