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

f: add $v0, $a1, $a0 bnez $a2, L sub $v0, $a0, $a1 L: jr $v0 1)This code contain

ID: 3552398 • Letter: F

Question

f: add $v0, $a1, $a0

bnez $a2, L

sub $v0, $a0, $a1

L: jr $v0


1)This code contains a mistake that violates the mips calling convention. What's this mistake and how should it be fixed?

2)what's the C equivalent of this code? Assume that the function's arguments are named a,b,c,etc in the C version of the function.

3)At the point where this function is called register $a0, $a1,$a2, and $a3 have values 1, 100, 1000 and 30, respectively. What's the value returned by this function? If another function g is called from f, assume that the value returned from g is always 500.

Explanation / Answer

1) jr is called with the argument $v0. In the MIPS calling convention, the argument fot jr is always $ra ($31, which contains the return address). Thus, to fix it, we should first move the value in $v0 to$ra and then proceed.


The new code is:

f: add $v0, $a1, $a0

bnez $a2, L

sub $v0, $a0, $a1

L: add $ra,$v0,$0

jr $v0


(b) Let the output be in variable d.

(c) Value returned is 100+1=101 since $a2=1000 != 0

Since g is not called from f in thsi case, the second part of the question is invalid.