How to Implement IDIV Instruction in GCC Assembler

Hi anyone,

I have problems when implementing the IDIV Instruction in gcc assembler, the program has compiled ok, but when executing function that imparts the asm codes which contained the IDIV instruction the error box immediately appeared announcing that it should terminates the program quickly.

The following is the complete listing of the program that I just been working on it:

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

signed long divider(signed long num, signed long divisor);

int main(int argc, char* argv[])
signed long dn0,dn1,dn2;

printf("Enter any positive real number: \n");
cin >> dn1;

printf("Enter any positive divisor number: \n");
cin >> dn2;

dn0 = divider(dn1,dn2);

cout << "The Resulted Quotient Number is: " << dn0 << endl;


signed long divider(signed long num, signed long divisor)
signed long nQuotient,nRemainder;

asm (
"xorl %%edx,%%edx;"
"xorl %%eax,%%eax;"
"movl %1, %%eax;"
"movl %2, %%ebx;"
"idivl %%ebx;"
:"=a" (nQuotient),"=d"(nRemainder)
:"a"(num), "b"(divisor)
return nQuotient;

I compiled it with DevCpp, and I had tried on CodeBlocks, in CodeBlocks I made used the int64_t type but still it could not helps more..

Can anybody there please help?
signed long divider(signed long num, signed long divisor)
   register signed long a asm ("eax") = num;
   register signed long b asm ("ebx") = divisor;
   register signed long nQuotient asm ("eax");
   asm ("xorl  %edx, %edx;"
        "idivl %ebx;");
   return nQuotient;

After about an hour of tinkering I found some code that worked.
I had tried your suggestion but still not succeed,why the result printed out continues to be zero ( 0 )? what compiler did you used? thanks for your kind attention.
Make sure num > divisor and num != 0 when calling the function.
If num < divisor or num == 0, there shall be zeros returned.
Last edited on
Sure i knew, but still don't works.
I wonder what kind of processor do you have?
I had Intel Pentium 4 - 3.0Ghz, stucked on my asus mobo ( LGA Socket ). Didn't it enough to makes it ok??
Topic archived. No new replies allowed.