Skip to content

Instantly share code, notes, and snippets.

@gabrielsaints
Forked from Silva97/fib-optimized.c
Created August 4, 2019 15:10
Show Gist options
  • Save gabrielsaints/577f194921931760239d2b2929e38ff6 to your computer and use it in GitHub Desktop.
Save gabrielsaints/577f194921931760239d2b2929e38ff6 to your computer and use it in GitHub Desktop.
#include <stdio.h>
/**
* Sequência de Fibonacci otimizada com Inline Assembly
*/
int fib(int n)
{
register int r = 0;
__asm__(
"movl %1, %%ecx\n\t"
"movl $1, %%ebx\n\t"
".lp:\n\t"
" xaddl %%ebx, %0\n\t"
" loop .lp"
: "+r" (r)
: "r" (n)
);
return r;
}
int main(void)
{
int x;
scanf("%d", &x);
printf("fib(%d) = %d\n", x, fib(x));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment