.code
tetrabonacci PROC
push ebp ;save previous pointer
mov ebp,esp ;save current pointer
sub esp, 4 ;make room for local variable ptr
mov eax,[ebp+8] ; get n
;if ((n==1)|| (n==2) return 1 else do recursion
cmp eax, 2 ;n>2?
ja recursion ; yes: continue to recursion
mov eax, 1 ; no: return 1
jmp Quit
cmp eax,1 ; n > 1?
ja recursion ; yes: continue to recursion
mov eax,1 ; no: return 1
jmp Quit
recursion:
dec eax
push eax ; Factorial(n-1)
call tetrabonacci ;tetrabonacci (n-1)
mov [ebp-4], eax ;store first result
call tetrabonacci ;Tetrabonacci (n-2)
mov [ebp-4], eax ;store second result
add esp, 4 ;clean stack
add eax, [ebp-4] ; add stored result (n-1) to (n-2) in eax
jmp Quit
Quit: mov esp, ebp ; restore esp pointer
pop ebp ; restore ebp
ret ; return EAX
tetrabonacci ENDP
I need help with my program to calculate the nth term in a tetrabonacci sequence. It is very similar to the fibonacci sequence except instead of adding the previous two terms together you add the previous four terms together. I have a working code with a main function written in c++ that calls the tetrabonacci function and displays the result. My issue is that halfway through the sequence I do not get te right tetrabonacci numbers. Here is what i should get:
n=1 -> 1
n=2 -> 1
n=3 -> 2
n=4 -> 4
n=5 -> 8
n=6 -> 15
n=7 -> 29
n=8 -> 56
n=9 -> 108
this is what i get:
n=1 -> 1
n=2 -> 1
n=3 -> 2
n=4 -> 4
n=5 -> 8
n=6 -> 16 this is where my problem starts at n=6
n=7 -> 32
n=8 -> 64
n=9 -> 128
I am not sure what is wrong I think something is not right in the recursion method i wrote in my assembly code. Any help would be much appreciated!