Reputation: 1605
Consider the following code
int a = 10;
int b = 2;
int c = 0;
int asmdivide(void);
int main(int argc, char* argv[])
{
c = a / b;
asmdivide();
return 0;
}
int asmdivide()
{
__asm
{
push dword ptr[a]
push dword ptr[b]
pop ecx
idiv ecx, dword ptr[a] //causes compile error
push ecx
pop dword ptr[c]
}
return c;
}
why does this line idiv ecx, ds:dword ptr[a] //causes compile error
Cause a compile error when the compiler generates this instruction idiv eax,dword ptr ds:[1308004h]
from this line `int b = 2;
The error outputted is
error C2414: illegal number of operands
Please see the screenshot below for assembly output
I am using Visual Studio 2013. Any explanation would be valued
Upvotes: 0
Views: 482
Reputation: 58487
It might seem like your code and the disassembly are the same, but that's just due to how the debugger chooses to display the disassembly.
The dividend for IDIV
is implicit, and is always EAX
when dividing by a 32-bit value (actually EDX:EAX
). To quote Intel's manual:
F7 /7 IDIV r/m32 M Valid Valid Signed divide EDX:EAX by r/m32, with result
stored in EAX ←Quotient, EDX ←Remainder.
So your attempt to specify a different register as the dividend will fail.
You can only specify the divisor for IDIV
, as in:
cdq ; sign-extend eax into edx:eax
idiv dword ptr [a] ; divide edx:eax by [a]
Upvotes: 5