I'm trying to write an assembly code version of Fibonacci which gives the nth Fibonacci number and returns it.
For some reason it is having trouble storing the return value of the Fibonacci numbers and adding them.
I want it to print the nth Fibonacci number.
I have made some modifications to my code, now it is still incorrect, but it's closer. Now it tells me that the 11th fibonacci number is 48. Still incorrect, but we're getting somewhere right?
.text
.globl _fibonacci
_fibonacci:
pushl %ebp
movl %esp,%ebp
push %esi
movl 8(%ebp),%esi
cmp $1,%esi
jle BASE
sub $1,%esi
push %esi
call _fibonacci
add $4,%esp
movl %eax,%edx
sub $1,%esi
push %esi
call _fibonacci
add $4,%esp
add %eax,%edx
movl %edx,%eax
DONE:
pop %esi
pop %ebp
ret
BASE:
mov $1,%eax
jmp DONE
I'm calling this assembly code using C:
#include <stdio.h>
int fibonacci(int n);
int main(void){
int n=111;
int x=fibonacci(n);
printf("The %d fibonaacci number is %d\n",n,x);
}