0

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:... 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
gqli
  • 985
  • 3
  • 11
  • 34

2 Answers2

2

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.

coder
  • 12,832
  • 5
  • 39
  • 53
  • Thank you . i think i got it . They might not be equivalent in some cases . If the inputs led to execute f = g + h way more frequent than f = g - h. The second version would probably slow down the process since they had to jump to else instead executing f = g + h directly after the comparison. – gqli Oct 02 '16 at 10:32
  • Yes right, the thing is that even these are equivalent and do the same thing they are not translation of same c code but equivalent and you want to translate c to MIPS...glad that helped! – coder Oct 02 '16 at 10:40
2

I would usually give full marks for both of them, unless the assignment was to specifically follow a specific template for translating if-constructs.

There are differences because of the branching structure, but the C code does not force a compiler to choose the first translation specifically (not even necessarily either of these), so I would argue that you are not forced to choose it either.

harold
  • 61,398
  • 6
  • 86
  • 164
  • I agree with you but the question wasn't exactly how c compiler would handle this if-else statement, it was how to write in MIPS the above if-else in c. Though I agree that both ways have the same result only the first one is exact translation of c code, the second MIPS code describes the second c program that I wrote (if of course I didn't make any mistake) and his is independent from what choice does C compiler prefers to do...anyway of course I think both MIPS are valid and do the same thing... – coder Oct 02 '16 at 11:15
  • That's really interesting! In what case the complier choose the second translation? Can you give an example . Thank you . – gqli Oct 02 '16 at 11:21
  • 1
    @coder it was *from* C though, right? If it was from assembly *to* C I would agree, not because the other order would be wrong exactly but in order to stay as close as possible to the original. But from C to assembly, if the compiler has a choice then we do too, right? – harold Oct 02 '16 at 11:39
  • @Snailwalker the most reliable case is if you're building using profile guided optimization and the profile says that the second case is more likely, otherwise it's not exactly random what the compiler will choose but it would have no reason to specifically prefer the first version. – harold Oct 02 '16 at 11:42
  • @harold Great input ! Thank you ! – gqli Oct 02 '16 at 11:56
  • IMO the main problem with the second version (in terms of matching the C) is that the label on the `add` instruction is `Else:`, opposite of the C. – Peter Cordes Mar 12 '22 at 14:23
  • 1
    @gqli: With PGO, if one is significantly less likely, it would put it somewhere at the end of the function (out of line), so that case has two taken branches. (One of them unconditional). And the more likely case would be the fast path, with a not-taken `bne` or `beq` and no `j`. You'd get the same result with C++20 [[likely]] or [[unlikely]] hints (or older macros using GNU C `__builtin_expect`), as in this x86-64 example: [How do the likely/unlikely macros in the Linux kernel work and what is their benefit?](https://stackoverflow.com/a/31133787) – Peter Cordes Mar 12 '22 at 14:30
  • @PeterCordes Thanks ! This was an assignment I had when I was a undergraduate. hahaha – gqli Mar 13 '22 at 05:55