Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assembler in C program 1

Status
Not open for further replies.

davidrobin

Programmer
Aug 17, 2000
50
GB
I have a C program that calls this function

int Divide(int Firstnum,int Secondnum)
{
int Result;
asm
{
mov ax,Firstnum
mov bx,Secondnum
and bx,255
div bl
mov Result,ax
}
cout<<Result;
return Result;
} // end function

if I call the function like so

Divide(10,2)

The value 5 is returned as expected.

If I call the function with

Divide(3,2)

The value returned (and displayed) is 257, I would have expected the value returned to be 1 (ignoring the remainder). Can anyone enlighten me with this problem. The data types have to be integer, my lecturer says so.

David
Visual Basic 6 Ent
 
I could not even compile your program successfully, it gave me some errors. Here I have a working one:

#include <iostream.h>

int Divide(int Firstnum,int Secondnum);


int main()
{
int Firstnum,Secondnum;
cout << &quot;Program beginning ...\n&quot;;
cout << &quot;\n Enter 1st number: &quot;;
cin >> Firstnum;

cout << &quot;\n Enter 2nd number: &quot;;
cin >> Secondnum;

Divide(Firstnum,Secondnum);

cout << &quot;\nProgram ending ...\n&quot;;
return 0;
}


int Divide(int Firstnum,int Secondnum)
{
int Result;
_asm
{
xor edx,edx
mov eax,Firstnum
mov ebx,Secondnum

div ebx
mov Result,eax
}

cout << Result;
return Result;
} // end function

P.S. Why did you need AND AX,FFh ? Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Thanks Aphrodita,

I copied the code into my program and it wouldn't run. I got errors 'invalid registers' so I took the e from each of the registers and it worked. I take it this is something to do with different processors.

BTW - I am using Borland C++ 4.5 on a P3 Compaq.

I would like to know however what the
xor edx,edx
line does. If I comment this line out I got errors, as far as I can understand it has something to do with clearing the registers initialising them to 0) I just like to know what is going on in my programs.

Thanks again for your help. There are not many that understand (or probably use) assembly these days.

David
Visual Basic 6 Ent
 
Before dividing you have to clear EDX register, because the remainder after a division goes there, if there were some value it would give an error, so you have to clear it this way, or CDQ would also work.
- CDQ copies most significant bit of EAX to all bit positions of EDX, but XOR usage is better, coz if that bit is 1, then it creates an error again.

AX - 16bit
EAX - 32bit

I do not know the reason why it did not work for you. Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
When you make Xor operation on a register or memory location with itself it is the same as set the value fo this register or memory location to Zero

X | Y | Xor
---------------
0 | 0 | 0
---|------|----
0 | 1 | 1 -| these are not our case because. I Xor
---|------|---- |-> Value with itself, So my case is just
1 | 0 | 1 -| When X=Y.
---|------|----
1 | 1 | 0
Walid Magd
Engwam@Hotmail.com
 
Going to the opriginal code, when you do a byte sized DIV, the remainder is stored in AH, the result in AL. You moved both remainder and result into your Result variable. Zero out AH before the move and that should solve your problem.
 
i cant help you but i offer a suggestion why do you use div in stdlib.h
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top