0% found this document useful (0 votes)
83 views2 pages

Fibonacci Mips

This document describes an algorithm to calculate Fibonacci numbers using recursion. It defines a fib function that takes an input n and: 1. Base cases return 1 for n=1 and recursively calls fib for n-1 and n-2, adding the results and returning the sum. 2. It makes space on the stack, saves registers, and recursively calls fib for n-1 and n-2, saving the results to the stack. 3. It pops the values off the stack, loads the saved registers, and returns the sum of the fib(n-1) and fib(n-2) values as the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views2 pages

Fibonacci Mips

This document describes an algorithm to calculate Fibonacci numbers using recursion. It defines a fib function that takes an input n and: 1. Base cases return 1 for n=1 and recursively calls fib for n-1 and n-2, adding the results and returning the sum. 2. It makes space on the stack, saves registers, and recursively calls fib for n-1 and n-2, saving the results to the stack. 3. It pops the values off the stack, loads the saved registers, and returns the sum of the fib(n-1) and fib(n-2) values as the result.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

fib:

slti $t0, $a0, 2 # if i < 2 (i.e i == 1)


beq $t0, $zero, cont # if i >= 2 go to cont
addi $v0, $zero, 1 # else make the return value 1

jr $ra

cont:

# OPERATION 1: MAKE SPACE IN STACK

addi $sp, $sp, -16 # make space for 3 elements in the stack
sw $ra, 0($sp) # save the return address
sw $a0, 4($sp)

# OPERATION 2: RECURSIVE CALLS

addi $a0, $a0, -1 # calculate n - 1


jal fib # calculate fib(n - 1)

sw $v0, 8($sp) # save into the stack result of fib(n - 1)


lw $a0, 4($sp) # load the value of n

addi $a0, $a0, -2 # calculate n - 2


jal fib

sw $v0, 12($sp) # save into the stack the result of fib(n - 2)

# OPERATION 3: POP FROM STACK

lw $ra, 0($sp) # load the return address


lw $t0, 8($sp) # load the value of fib(n - 1)
lw $t1, 12($sp) # load the value of fib(n - 2)
addi $sp, $sp, 16 # pop from stack

# OPERATION 4: OPEARATION

add $v0, $t0, $t1 # fib(n - 1) + fib(n - 2)

jr $ra

*******************OTRA FORMA*************************

# Compute first twelve Fibonacci numbers and put in array, then print
.data
fibs: .word 0 : 12 # "array" of 12 words to contain fib values
size: .word 12 # size of "array"
.text
la $t0, fibs # load address of array
la $t5, size # load address of size variable
lw $t5, 0($t5) # load array size
li $t2, 1 # 1 is first and second Fib. number
add.d $f0, $f2, $f4
sw $t2, 0($t0) # F[0] = 1
sw $t2, 4($t0) # F[1] = F[0] = 1
addi $t1, $t5, -2 # Counter for loop, will execute (size-2) times
loop: lw $t3, 0($t0) # Get value from array F[n]
lw $t4, 4($t0) # Get value from array F[n+1]
add $t2, $t3, $t4 # $t2 = F[n] + F[n+1]
sw $t2, 8($t0) # Store F[n+2] = F[n] + F[n+1] in array
addi $t0, $t0, 4 # increment address of Fib. number source
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, loop # repeat if not finished yet.
la $a0, fibs # first argument for print (array)
add $a1, $zero, $t5 # second argument for print (size)
jal print # call print routine.
li $v0, 10 # system call for exit
syscall # we are out of here.

######### routine to print the numbers on one line.

.data
space:.asciiz " " # space to insert between numbers
head: .asciiz "The Fibonacci numbers are:\n"
.text
print:add $t0, $zero, $a0 # starting address of array
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading
out: lw $a0, 0($t0) # load fibonacci number for syscall
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # output string
addi $t0, $t0, 4 # increment address
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat if not finished
jr $ra # return

You might also like