retagged by
15,445 views
49 votes
49 votes

Delayed branching can help in the handling of control hazards

The following code is to run on a pipelined processor with one branch delay slot:

I1: ADD $R2 \leftarrow R7 + R8$

I2: Sub $R4 \leftarrow R5 – R6$

I3: ADD $R1 \leftarrow R2 + R3$

I4: STORE Memory $[R4] \leftarrow R1$

     BRANCH to Label if $R1 == 0$

Which of the instructions I1, I2, I3 or I4 can legitimately occupy the delay slot without any program modification?

  1. I1
  2. I2
  3. I3
  4. I4
retagged by

7 Answers

4 votes
4 votes
Answer is (D) I4. The STORE instruction can be moved below the conditional branch instruction. Whether the branch is taken or not, STORE will be executed as the next instruction after conditional branch instruction, due to delayed branching.

Here, I3 is not the answer because the branch conditional variable R1 is dependent on it. Same for I1. Similarly, I4 has a dependency on I2 and hence I2 must be executed before I4.
3 votes
3 votes

Delay  Branching in simple terms

imagine you're following a set of instructions in a recipe. Sometimes, the recipe might give you a choice like "If the vegetables are fresh, do this step. If not, skip to the next step." Now, let's relate this to a computer's way of working..

In a computer, there's something called a pipeline that helps it process tasks faster. It's like an assembly line in a factory, where different steps are done one after another to make something. But sometimes, the computer encounters a step where it's not sure what to do because it depends on something else, like our recipe example.

To make things efficient, the computer can sometimes find steps that are safe to do no matter what the uncertain part is. It's like finding an instruction in the recipe that you can always follow regardless of whether the veggies are fresh or not.

Now, when the computer comes across a point where it's not sure because of a "choice" (like our fresh veggies decision), it starts working on the step that comes right after that uncertain point. This is like you starting to do the next recipe step without waiting to see if the veggies are fresh or not.

And here's the cool part: the computer doesn't need to worry about the choice anymore. Whether the veggies are fresh or not, it already started the next step which is safe to do anyway. So, it keeps working smoothly without having to stop and think too much about the uncertain choice, just like you keep going with the recipe without worrying about the veggies.

This way, the computer can keep its pipeline busy and be more efficient, just like you can keep making progress in your recipe by doing the steps that don't depend on the uncertain part.

if it is helpfull upvote it !

edited by
0 votes
0 votes

In the I2 instruction, register R4 takes the value as R5-R6. Instruction I4 stores the R1 register value in the address stored in R4(which we calculate from I2). If the flow is I1 > I3 > I4 > branch > I2, when executing I4 we won’t have the address so error. The only possible flow is I1 > I2 > I3 > branch > I4,
 

This can be pictorially represented as a 5 stage pipeline. Here, I am assuming there is also a follow-up instruction I5 for generality

I1 I1 I1 I1 I1 I1          
  I2 I2 I2 I2 I2 I2        
    I3 I3 I3 I3 I3 I3      
      B B B B B B    
        I4 I4 I4 I4 I4 I4  
          I5 I5 I5 I5 I5 I5

 

I1 I1 I1 I1 I1 I1            
  I2 I2 I2 I2 I2 I2          
    I3 I3 I3 I3 I3 I3        
      I4 I4 I4 I4 I4 I4      
        B B B B B B    
            NOP I5 I5 I5 I5 I5

Comparing both the images, in the first image we observe that by placing I4 in the slot after the branch instruction(depicted as B) we are utilizing what would otherwise be filled with NOP(as in the second image). Clearly, one extra cycle is required in the second image as branch decision is taken usually after the 3rd stage.

Answer:

Related questions

55 votes
55 votes
2 answers
6