So, I will give my textbook (Computer Organization and Design), and then ask my question:
Compiling if-then-else into conditional branches
In the next code segment, f, g, j, i and j are variables. If five fj variables correspond to five registers $ s0-s $ s4, what is the compiled MIPS code for this C if statement?
if (i == j) f = g + h; else f = g - h;
Figure 2.9 is a block diagram of what the MIPS code should do. The first expression is compared for equality, so it would seem that we need a branch if the registers are equal to instructions (beq). In the general case, the code will be more efficient if we test the opposite condition on the branch over the code that executes the subsequent part of the if (the Else label is defined below), and therefore we use the branch if the registers are not equal to the instruction (bne):
bne $ s3, $ s4, Else # go to Else if I β j
I searched for a while, but I could not find why bne would be more efficient than beq. (However, I found that bne is sometimes recommended, because it makes it easier to understand the code, since the instructions that must be executed when the condition is met are directly below the bne statement.)
So, if it is not more effective overall, it can still be more effective in this particular exercise. I thought about it, and I suggested that the jump instruction is worth the time, and therefore we would like to minimize the number of jumps needed. This means that when we expect the condition to be met, we must use bne, and when we expect the condition to fail, we must use beq.
Now, if we check if $ s3 $ s4 is equal when we have no information about the contents of these registers, it is not reasonable to assume that they are likely to be equal; on the contrary, it is more likely that they are not equal, which should lead to the use of beq instead of bne.
So to summarize: the tutorial says that bne is more efficient than beq, regardless of whether it is generally or simply unclear in this example, but in any case, I donβt understand why.