News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Div Function

Started by ~Paul~, June 21, 2007, 01:44:07 AM

Previous topic - Next topic

~Paul~


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?

Darrel

the div function divides eax not ebx

~Paul~

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.

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

eschew obfuscation

~Paul~

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.

sinsi

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)
Light travels faster than sound, that's why some people seem bright until you hear them.

~Paul~

Thanks :) Ill try that when i get home  :U