Snailwalker Snailwalker - 2 months ago 14
C Question

Are these two MIPS translations from C equivalent?

Are these following MIPS translations from C equivalent? if not, Please explain in detail. My professor said Version one is correct. if i wrote the instruction like version two, I would not get the mark even if they produce same output. she did not explain why. Thank you in advance !

C Code

if (i==j) {
f = g + h ;
}
else
f = g -h ;


MIPS version one :

bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit:...


MIPS version two:

beq $s3, $s4, Else
sub $s0, $s1, $s2
j Exit
Else: add $s0, $s1, $s2
Exit:...

Answer

You go to else statement when i is not equal to j so use bne (branch not equal). What you're second MIPS describes is the code:

if (i!=j) {
     f = g - h ;
}
else 
     f = g + h ;

So it is equivalent but your second MIPS code it doesn't describe the first if-else code it describes something equivalent but it is not right translation.

Comments