The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ~Paul~ on June 21, 2007, 01:44:07 AM

Title: Div Function
Post by: ~Paul~ on June 21, 2007, 01:44:07 AM

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?
Title: Re: Div Function
Post by: Darrel on June 21, 2007, 02:00:06 AM
the div function divides eax not ebx
Title: Re: Div Function
Post by: ~Paul~ on June 21, 2007, 02:07:25 AM
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.
Title: Re: Div Function
Post by: hutch-- on June 21, 2007, 02:16:50 AM
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.
Title: Re: Div Function
Post by: MichaelW on June 21, 2007, 02:29:08 AM
Paul,

Perhaps the problem you were having was a divide overflow:

http://www.masm32.com/board/index.php?topic=3057.msg23484#msg23484


Title: Re: Div Function
Post by: ~Paul~ on June 21, 2007, 02:35:20 AM
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.
Title: Re: Div Function
Post by: sinsi on June 21, 2007, 02:56:41 AM
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)
Title: Re: Div Function
Post by: ~Paul~ on June 21, 2007, 03:02:16 AM
Thanks :) Ill try that when i get home  :U