I am currently writing a compiler which produces x86-32 code.
However, I have a problem when trying to implement division.
idivl %ecx, %ebx
This code gives me the following error:
Error: operand type mismatch for `idiv'
Does anyone know why? The above lines are the only time idiv
appears in my code.
According to my reference, IDIV
divides the 64-bit integer EDX:EAX
by the provided register's value.
The idiv instruction divides the contents of the 64 bit integer EDX:EAX (constructed by viewing EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value. The quotient result of the division is stored into EAX, while the remainder is placed in EDX.
Syntax
idiv <reg32> idiv <mem>
Examples
idiv ebx ; divide the contents of EDX:EAX by the contents of EBX. Place the quotient in EAX and the remainder in EDX. idiv DWORD PTR [var] ; divide the contents of EDX:EAS by the 32-bit value stored at memory location var. Place the quotient in EAX
and the remainder in EDX.
The idivl
instruction behaves exactly like idiv
, except idivl
is a signed division.
See http://www.cs.virginia.edu/~evans/cs216/guides/x86.html
Another great ref: http://ref.x86asm.net/
idivl only takes one argument. see here. it divides the contents of EDX:EAX by the contents of the argument