Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is the solution to the problem. Please correct any mistakes that I have ma

ID: 3732378 • Letter: B

Question

Below is the solution to the problem. Please correct any mistakes that I have made and provide an EXPLANATION. Only answer this question if you know how to do it! Thanks.

Question:

Translate the following C code to MIPS assembly code.

int service (int n) {

     if (n <= 10)

          return n;

     else

          return n + service(n+1) + service (n+2);

     }

Solution:

/IStae K Spac e tors items 1 2. Service: - /store ira as the verun Vatu e Povorne ,r (n tru. // .tore ress en to Value of n n lo eise move ss0, ao al Service add $so,ts 0,30 I add veturn ll arynement (ntr)or vecusive addi tao, too, I Sevvice (nti) Caut al servce add tso, s so, $ vo lladd return vAlue for service (ne

Explanation / Answer

#int service (int n){

# if (n<=10){

# return n;

# else

# return n + service(n+1) + service(n+2);

# }

service:

bgt $a0, 10, recurse

move $v0, $a0

jr $ra

recurse:

sub $sp, $sp, 12 # stack space for 3 items

sw $ra, 0($sp) # store return address

sw $a0, 4($sp) # store input parameter

addi $a0, $a0, 1 # $a0 = n+1

jal service # service(n+1)

sw $v0, 8($sp) # store result of service(n+1) on stack

lw $a0, 4($sp) # $a0 = n

addi $a0, $a0, 2 # $a0 = n+1

jal service # service(n+2)

lw $t0, 8($sp) # $t0 = service(n+1)

add $v0, $v0, $t0 # $v0 = service(n+1) + service(n+2)

lw $a0, 4($sp) # $a0 = n

add $v0, $v0, $a0 # $v0 = n + service(n+1) + service(n+2)

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

addi $sp, $sp, 12 # remove stack space

jr $ra # return (result is at $v0)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote