# Answer to Question #47584 in Assembler for Danielle A

Question #47584

have to calculate the nth term in a tetrabonacci sequence using recursion. in x86 MASM assembly. It is similar to Fibonacci but instead of adding the previous two terms you add the previous four terms. I have written workable code where I get the first 5 n terms tetrabonacci sequence correct. But when n=6 my recursion method seems to not work. Can someone help me with where my code went wrong? .586

.model flat,C

.code

tetrabonacci PROC

push ebp ;save previous pointer

mov ebp,esp ;save current pointer

sub esp, 12 ;make room for local variable ptr

mov eax,[ebp+8] ; get n

;if ((n==1)|| (n==2) ||(n == 3)||(n===4)) return predetermined value, else do recursion on previous 4 terms

cmp eax,4 ; n<=4?

ja recursion ; n > 4 do recursion

jb exception1 ; n< 4 jump to exception1

mov eax,4 ;n=4 move 4 into eax

jmp Quit ;jump to end

exception1:

cmp

.model flat,C

.code

tetrabonacci PROC

push ebp ;save previous pointer

mov ebp,esp ;save current pointer

sub esp, 12 ;make room for local variable ptr

mov eax,[ebp+8] ; get n

;if ((n==1)|| (n==2) ||(n == 3)||(n===4)) return predetermined value, else do recursion on previous 4 terms

cmp eax,4 ; n<=4?

ja recursion ; n > 4 do recursion

jb exception1 ; n< 4 jump to exception1

mov eax,4 ;n=4 move 4 into eax

jmp Quit ;jump to end

exception1:

cmp

Expert's answer

## Comments

## Leave a comment