# Thread: Fibonacci number program problem(assembly)

1. ## Fibonacci number program problem(assembly)

anyone can help me to fix my program problem? I can't output the correct number, what's the problem on my code? Help me, thanks.

Code:
```## Program to computes the value of Fibonacci number.

.text
.globl  __start

__start:

sub    \$sp,\$sp,4    # push return address
sw    \$ra,(\$sp)
sub    \$sp,\$sp,4    # push caller's frame pointer
sw    \$fp,(\$sp)
sub    \$fp,\$sp,8    # \$fp = \$sp - space_for_variables
move    \$sp,\$fp

li    \$v0,4    # call to print string
la    \$a0,pr1
syscall

li    \$v0,5    # call to read integer
syscall
sw    \$v0,0(\$fp)    # save in \$v0
nop

lw    \$a0,0(\$fp)    # load variable into \$a0
nop
jal    fib
nop

sw    \$v0,4(\$fp)    # b = fib(a)
nop

li    \$v0,4    # call to print string
la    \$a0,pr2
syscall

lw    \$a0,4(\$fp)
li    \$v0,1    # call to print integer
syscall

add    \$sp,\$fp,8    # \$sp = \$fp + space_for_variables

lw    \$fp,(\$sp)    # pop \$fp
lw    \$ra,(\$sp)    # pop \$ra
nop

.data
pr1:    .asciiz "Enter an interger:"
pr2:    .asciiz "\nThe Fibonacci number is:"

# int Fib( int n )
# {
#   if( n <= 0 )
#      return 0
#   else if( n == 1 )
#       return 1
#   else
#     x = Fib( n-1 )
#     y = Fib( n-2 )
#     return x + y
#   endif
# }

.text
.globl  fib

fib:
sub    \$sp,\$sp,4    # push return address
sw    \$ra,(\$sp)
sub    \$sp,\$sp,4    # push caller's frame pointer
sw    \$fp,(\$sp)
sub    \$sp,\$sp,4    # push register \$s1
sw    \$s1,(\$sp)
sub    \$fp,\$sp,0    # \$fp = \$sp - space_for_variables (==0)
move    \$sp,\$fp

move    \$s1,\$a0    # save argument in \$s1
li    \$t1,0    # get a==0
bgt    \$s1,\$t1,lo1    # if (n <= 0)
nop
li    \$v0,0    # return 0
j    exit
nop

lo1:
move    \$s1,\$a0    # save argument in \$s1
li    \$t1,1    # get a==1
beq    \$s1,\$t1,lo2    # if (n == 1)
nop
bgt    \$s1,\$t1,lo3    # if (n <=1)
nop

lo2:    li    \$v0,1    # return 1
j    exit
nop
lo3:
sub     \$a0,\$s1,1    # param = n-1
jal     fib    # compute fib(n-1)
nop
move    \$v0,\$s1    # x = Fib( n-1 )

sub    \$a0,\$s1,2    # set param to n-2
jal     fib    # and make recursive call
nop

exit:
add    \$sp,\$fp,0    # \$sp = \$fp + space_for_variables (==0)
lw    \$s1,(\$sp)
lw    \$fp,(\$sp)    # pop \$fp
lw    \$ra,(\$sp)    # pop \$ra
nop```

2. > what's the problem on my code?
Well it isn't C++
And you didn't state which Processor or OS you're using.
And you didn't describe the symptoms in anything better than "it doesn't work".

Moved.

3. This is MIPS assembly language program.
Suppose Fibonacci numbers counting from 0, the resule is 0, 1, 1, 2, 3, 5, 8, 13, 21
but my program can not output the correct answer, which it's i input 6, my program cannot output the result is 8.

4. Why would it output 8, when the first 6 numbers are 0, 1, 1, 2, 3, 5

5. Fibonacci numbers counting from 0, not 1.

6. I can't figure out how you got this far, yet can't figure out how to turn say a 'lt' into an 'le' (for example) to make a loop run for one more iteration.

I mean, if it produces the right sequence, but the wrong length of sequence, then that ought to be the easiest thing to diagnose and fix.

7. this program is ask user input a number, such as 4, and then do the Fibonacci numbers, output the result, suppose is 3. But my program increase the result for 3 each time, so my result is 6. I can't figure out where the problem is.

8. Why don't you try writing the program in C++ so that half of us can understand what you're writing, and then if you really need to, port it to assembly.