Skip to content

Instantly share code, notes, and snippets.

@kylelk
Created December 27, 2013 23:31
Show Gist options
  • Save kylelk/8154079 to your computer and use it in GitHub Desktop.
Save kylelk/8154079 to your computer and use it in GitHub Desktop.
Compute Greatest Common Divisor using Euclid's Algorithm
#include <stdio.h>
int gcd( int a, int b ) {
int result ;
/* Compute Greatest Common Divisor using Euclid's Algorithm */
__asm__ __volatile__ ( "movl %1, %%eax;"
"movl %2, %%ebx;"
"CONTD: cmpl $0, %%ebx;"
"je DONE;"
"xorl %%edx, %%edx;"
"idivl %%ebx;"
"movl %%ebx, %%eax;"
"movl %%edx, %%ebx;"
"jmp CONTD;"
"DONE: movl %%eax, %0;" : "=g" (result) : "g" (a), "g" (b)
);
return result ;
}
int main(int argc, const char * argv[])
{
printf("%d \n", gcd(30, 50));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment