.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!

Write out the program in pseudo-code first and work it through using that logic. It will likely show you where your mistake is. Sorry, but I haven't written x86 assembler since about 1992...

That said, it is obvious from your results that your logic as to where to stop recursing is faulty. You are only stopping if n == 1 or n == 2. In all cases, you can only do two levels of recursion in order to get the 4-level sum.

shouldnt it be

n => 0 return 1
n => 1 return 1
n => 2 return 2
n => 3 return 4
n => 4 return 8
n => 5 return 15
n => 6 return 29

??

Also with this series you are adding the previous 4 numbers to get the current nth place so you wouldnt just be checkng if n == 1 or n == 2 correct?

This article has been dead for over six months. Start a new discussion instead.