Skip to content

Instantly share code, notes, and snippets.

@KelviNosse
Last active August 29, 2016 19:08
Show Gist options
  • Save KelviNosse/fe23b3f0e7a40ea1d04c to your computer and use it in GitHub Desktop.
Save KelviNosse/fe23b3f0e7a40ea1d04c to your computer and use it in GitHub Desktop.
Fibonacci MIPS32 | Example | Step by Step
#Fibonacci MIPS32
#Author: Kelvin Nose: :^)
#Date: 10/30/15
#int count = 0;
#int n = 0;
#int first = 0;
#int second = 1;
#int next = 0;
#int c;
# for ( c = 0 ; c < n ; c++ )
# {
# if ( c <= 1 )
# next = c;
# else
# {
# next = first + second;
# first = second;
# second = next;
# }
# printf("%d\n",next);
# }
.data
# Default variable values
count: .word 0
n: .word 7
first: .word 0
second: .word 1
next: .word 0
c: .word 0
.text
main:
lw $t0, c #set c contents to $t0
lw $t1, n #set n (number of fibonacci terms) to $t1
lw $t2, next #set next contents to $t2
lw $t3, first #set first contents to $t3
lw $t4, second #set second contents to $t4
j loop #jumps to loop to start for(c=0 ; c<n; c++)
#Default label for if(c<= 1) cases
default:
sw $t0, next($t2) #stores content of c in next // next = c
addi $t0, $t0, 1 #increments c by 1 // c++
j loop # jumps to loop to continue until c<n
#for loop content
loop:
ble $t0, 1, default #evaluate if(c<=1) if true, then go to default label
add $t2, $t3, $t4 #adding and storing values of first and second //next = first+second
add $t3, $zero, $t4 #stores contents of second into first // first = second
add $t4, $zero, $t2 #stores contents of next into second // second = next
addi $t0, $t0, 1 #increments c by 1 // c++
blt $t0, $t1, loop #go to loop if c < n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment