Explanation of Solution
Corresponding MIPS instruction:
The corresponding MIPS instruction for the given C code is as follows:
#create a stack
fib: addi $sp, $sp, -12
#push the value of "$ra"
sw $ra, 8($sp)
#push the value of "$s0"
sw $s0, 4($sp)
#push the value of "$a0 (N)"
sw $a0, 0($sp)
#check "n > 0" and also check "n = 1"
bgt $a0, $0, check
#add "$v0 = 0 + 0"
add $v0, $0, $0
#jump to the "return" loop
j return
#$t0 = $0 + 1
check: addi $t0, $0, 1
#if n > 1, go to loop
bne $t0, $a0, loop
#$v0 = $0 + $t0
add $v0, $0, $t0
#jump to the "return" loop
j return
#$a0 = $a0 – 1
loop:subi $a0, $a0, 1
#call the function
jal fib
#$s0 = $v0 + $0
add $s0, $v0, $0
#$a0 = $a0 – 1
sub $a0, $a0, 1
#call the function
jal fib
#$v0 = $v0 + $s0
add $v0, $v0, $s0
#pop $a0
return: lw $a0, 0($sp)
#pop $s0
lw $s0, 4($sp)
#pop $ra
lw $ra, 8($sp)
#$sp = $sp + 12
addi $sp, $sp, 12
#return the caller
jr $ra
Explanation:
- Initially the “fib” loop create a stack then push the values into the stack...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Computer Organization and Design MIPS Edition, Fifth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design)
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY