Created
December 27, 2013 23:31
-
-
Save kylelk/8154079 to your computer and use it in GitHub Desktop.
Compute Greatest Common Divisor using Euclid's Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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