Thread: Fibonacci number program problem(assembly)

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    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
        add    $sp,$sp,4
        lw    $ra,($sp)    # pop $ra
        add    $sp,$sp,4
        jr    $ra    # return to OS
        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
        add     $v0,$v0,$s1# add fib(n-2)
    
    exit:
        add    $sp,$fp,0    # $sp = $fp + space_for_variables (==0)
        lw    $s1,($sp)
        add    $sp,$sp,4
        lw    $fp,($sp)    # pop $fp
        add    $sp,$sp,4
        lw    $ra,($sp)    # pop $ra
        add    $sp,$sp,4
        jr    $ra    # return to OS
        nop

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why would it output 8, when the first 6 numbers are 0, 1, 1, 2, 3, 5
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    Fibonacci numbers counting from 0, not 1.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    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. #8
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Making a program take a variable number of arguments
    By subtled in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 05:38 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM