Recursive fibonacci Assembly

Active3 hr before
Viewed126 times

3 Answers


Returning from the 1st recursive call you put the result from EAX in this memory dword. Returning from the 2nd recursive call you add to EAX the contents of this memory dword. Doing so, the EDX register will no longer be clobbered.,Today I wrote a recursive fibonacci in assembly and it doesn't work. I compiled it to object file with NASM and than made it elf with gcc. When I enter 1 or 2 the function works perfectly, but when I enter 3, 4, 5, 6, or more the function doesn't work. I think there is problem where the function calls itself. ,If you don't balance the stack directly after the 1st recursive call, you can just decrement the dword that is already there and make your second recursive call., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

mov eax, [ebp - 4]
load more v

C Code

int fib(int n) {
   if (n < 2) return 1;
   else return fib(n - 1) + fib(n - 2);
load more v

Following are different methods to get the nth Fibonacci number. ,Method 1 (Use recursion) A simple method that is a direct recursive implementation mathematical recurrence relation given above.,The Fibonacci numbers are the numbers in the following integer sequence.0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..,We can observe that this implementation does a lot of repeated work (see the following recursion tree). So this is a bad implementation for nth Fibonacci number. 

load more v

Other "assembly-recursive" queries related to "Recursive fibonacci Assembly"