Review: instruction encoding of the branch instruction

The BRANCH instruction(s) encoding format (meaning of the bits in the instruction:

I used bra label to denote bal label (or b label) because my old notes used this notation.

How the pipeline CPU executes bra label

Clock cycle 1: The IF stage fetches the instruction into the IR(ID) register

I have include the instruction code in binary

How the pipeline CPU executes bra label

Start of clock cycle 2: the ID stage fetch all possible source operands

Notice that the branch instruction will use the current value in the PC register !!

How the pipeline CPU executes bra label

End of clock cycle 2: all source operands are fetched - but only PC and IR1=32 will be used

The label label is 32 bytes below the current instruction - that's why offset = 32

How the pipeline CPU executes bra label

Start of clock cycle 3: the EX stage operates on the correct source operands

The branch instructions will always select PC and IR1 for source operands

How the pipeline CPU executes bra label

End of clock cycle 3: the branch destination address (PC+32) is stored in ALU Output reg

Notice: 2 instructions following bra label have now been fetched !!!

How the pipeline CPU executes bra label

Start of clock cycle 4: the MEM stage is used to execute the bra label instruction:

The MEM stage sends the select signal to MUX in the IF stage to update PC to the branch address (PC+32)

How the pipeline CPU executes bra label

End of clock cycle 4: the PC is updated to the branch destination address (PC+32)

This branch destination address in assembler code would be marked by the label label

How the pipeline CPU executes bra label

Start of clock cycle 5: WB stage is idle - The IF stage fetches instruction at address label

 

How the pipeline CPU executes bra label

End of clock cycle 5: the instruction at the branch address is fetched

Notice: the (branch) instruction has been into the discarded !! (no longer needed !!!)

DEMO (using Aaron's pipelined CPU)
 

  • Execute this command on a lab machine:

       /home/cs355001/demo/pipeline/3-bra-instr   
      

    Program being executed:

         0:   10  62    // mov r1,#62
              18  1     // mov r2,#1
              26  1     // mov r3,#1
              34  1     // mov r4,#1
              42  1     // mov r5,#1
              50  1     // mov r6,#1
              58  1     // mov r7,#1
              0   0     // nop
              0   0     // nop
              0   0     // nop
              0   0     // nop
              0   0     // nop
       12:    192 44    // bra +44
              16  10    // add r2,r1,r2  (R2=R1+R2)
              24  11    // add r3,r1,r3  (R3=R1+R3) the following 3 instr's will be fetched
              32  12    // add r3,r1,r4  (R4=R1+R4)  + exec before branch take place
              40  13    // add r4,r1,r5  (R4=R1+R4)
              48  14    // add r5,r1,r6  (R4=R1+R4)
              56  15    // add r6,r1,r7  (R4=R1+R4)
      
       56:     0   1    // <---- bra target   (56 = 111000)
               0   2