Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDIV operation in assembly (understanding)

Tags:

x86

assembly

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.

like image 828
user3097712 Avatar asked Aug 25 '14 15:08

user3097712


2 Answers

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.

like image 115
Michael Avatar answered Nov 14 '22 23:11

Michael


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.

like image 31
hobbs Avatar answered Nov 14 '22 21:11

hobbs