Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fib:
- addi $sp,$sp,-12 #create stack for 2 words
- slti $t0,$a0,1 #n<1
- beq $t0,1,ret #we return value
- beq $a0,1,ret #we ret value as n==1
- #store the ra
- sw $ra,8($sp)
- sw $a0,0($sp) #first element will be the argument passed to fib, that is n
- addi $a0,$a0,-1 #compute n-1 and call fib
- jal fib
- #restore back value
- lw $a0,0($sp)
- #now store the result
- sw $v0,4($sp)
- #now compute n-2 and call fib
- addi $a0,$a0,-2
- jal fib
- #now return fib(n-1)+fib(n-2)
- lw $t0,4($sp) #restore fib(n-1) value
- add $v0,$v0,$t0
- lw $ra,8($sp)
- #now just pop last 3 elements of stack
- addi $sp, $sp, 12
- jr $ra #jump to return addr
- ret:
- add $v0,$a0,$zero #just return n as it was <=1
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement