MIPS bne beq Organization and design of computers

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.

+4
source share
2 answers

Efficiency is not related to directly comparing machine code for bne versus beq. The text describes how to optimize all performance by coding to shorten the most common code path.

If you assume that the values ​​are likely to be unequal, then when using bne you only need to process one command, if you use beq, you must make an additional failover.

The shortest way is to skip the comparison, fail it and not jump.

from http://www.cs.gmu.edu/~setia/cs365-S02/class3.pdf :

Unusual case for branches

beq $ 18, $ 19, L1

  • else

  • Jmp

replaced by

bne $ 18, $ 19, L2

  • successful processing

  • end

L2:

Making a normal case fast is one command for most branches

After re-reading your question, I think the essence of this assumption:

"Now, if we check if $ s3 $ s4 is equal when we don’t have information regarding the contents of these registers, it’s not reasonable to assume that they are likely to be equal; on the contrary, it is more likely that they are not equal, should result in using beq instead of bne. "

This seems like a confusion, we need to find some evidence or reasons to determine which probability is more likely to register equal or unequal.

In this case, we study if-then-else. I affirm that we expect the if-test to pass, this is the psychology described by twalberg. Registers are unlikely to contain random values, since they contain data expected by the programmer - the result of previous operations.

+1
source

Another reason for this is that simple industry forecasters usually assume that forward branches are not accepted and that branches are accepted back. This assumption gives better performance for simple loops.

0
source

All Articles