int Add(int x, int y)
{
__asm
{
mov eax,x;
mov ebx,y;
add eax,ebx;
mov x,eax;
}
return x;
}
int Sub(int x, int y)
{
__asm
{
mov eax,x;
mov ebx,y;
sub eax,ebx;
mov x,eax;
}
return x;
}
int __fastcall Div(int x, int y)
{
__asm
{
mov ebx,y;
mov ecx,x;
div ecx;
mov x,ebx;
}
return x;
}
int Mul(int x, int y)
{
__asm
{
mov eax,x;
mov ebx,y;
mul ebx;
mov x,eax;
}
return x;
}
These are some function i made to use in my inline calculator tutorial and the div function is not working :( I tried adding __fastcall as you can see but nothing works :(
any ideas whats wrong?
the div function divides eax not ebx
hmm aparently it divides ecx. I had problems when using eax to for some reaosn. At the start i gave it two parameters :p so i know that can't be right.
Paul,
Actually bother to look up the DIV mnemonic in the Intel literature, as Darrel has told you it is a register specific mnemonic and if you try and use the wrong register it will not work.
Paul,
Perhaps the problem you were having was a divide overflow:
http://www.masm32.com/board/index.php?topic=3057.msg23484#msg23484
The problem i am having with the code i have posted above is that it returns the second number entered. Not sure if that is any help.
DIV divides a 64-bit number (always in EDX:EAX) by a 32-bit number.
int __fastcall Div(int x, int y)
{
__asm
{
mov eax,x;
mov ecx,y;
sub edx,edx;
div ecx;
mov x,eax;
}
return x;
}
Just remember that the whole part ends up in EAX and the remainder in EDX - so 3/2 will actually return 1 (not 1.5 since it is integer division)
Thanks :) Ill try that when i get home :U