Created
January 3, 2020 16:35
-
-
Save ak-seyam/47bb121feb6aa6e24456cfcc32df5eaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#fibonacci generator by abdullah khaled A-Siam | |
# April 27,2019 3:40am | |
.data | |
msg: .asciiz "x= " | |
resu: .ascii "fib: " | |
.text | |
.globl main | |
main: | |
# print (x=) | |
li $v0,4 | |
la $a0, msg | |
syscall | |
#get value from the user | |
li $v0 ,5 | |
syscall | |
move $a0, $v0 | |
jal fib | |
move $t2 , $v0 | |
li $v0 ,4 | |
la $a0, resu | |
syscall | |
li $v0 ,1 | |
addi $a0, $t2 , 0 | |
syscall | |
li $v0 ,10 | |
syscall | |
#fibonacci function | |
fib: | |
#set a space for a single snapshot in the stack | |
subiu $sp , $sp , 12 #using 3 vars | |
sw $a0 , 8($sp) #the seed number N | |
sw $s0 , 4($sp) #used to add fib(N-2) | |
sw $ra , 0($sp) #pc returining reg (pc+4) | |
bgt $a0 , $0 , test1 #if(n<=0) | |
addi $v0 , $0 , 0 #{ return 0; | |
j endFunction # ...popping outta stack... } | |
test1: #else if(n==1){ | |
addi $t0 , $0 , 1 # | |
bne $a0 , $t0 , generate # | |
add $v0 , $0 , 1 #return1; | |
j endFunction # ...popping outta stack... } | |
generate: #else{ | |
subi $a0 , $a0 ,1 # x = n-1; | |
jal fib # y = fib(x); | |
addi $s0 , $v0 , 0 # z = y + 0; | |
subi $a0, $a0 , 1 # a = x -1; | |
jal fib # b = fib(a) | |
add $v0 , $s0 $v0 #return z + b;} | |
# ...popping outta stack... | |
endFunction: lw $a0 ,8($sp) | |
lw $s0 ,4($sp) | |
lw $ra ,0($sp) | |
addiu $sp , $sp ,12 | |
jr $ra |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment