We now examine in detail how the basic pipeline CPU execute branch instructions
Branch condition <-------------- branch offset -----------> +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 1 | 1 | B | B | B | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ B B B Branch condition --------------------------------------------------------------- 0 0 0 Branch always 0 0 1 BEQ 0 1 0 BNE 0 1 1 BLT 1 0 0 BLE 1 0 1 BGT 1 1 0 BGE 1 1 1 not used |
BRA cond <-------------- Offset -------------------> +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ ^ ^ | | +-------------------------------------------+ offset = 10 |
bra label ^ ... | ... | (32 instructions) ... | v label: |
Execution:
|
Notice that PC + BranchOffset is the branch location (= address) !!!
Clearly, because the BRANCH instruction does use any value in the "A", "B" and "D" registers, fetching (arbitrary) values into the "A", "B" and "D" registers by the branch instruction will not cause any error (harm)....
Notice that:
|
If the branch is taken, the select hardware in the MEM stage will send out a control signal to the MUX in the IF to direct the ALUo output into PC.
If the branch is not taken (= does not branch), the Select signal will pick the value PC+4
Note:
|
In this case (Branch Always), the ALUo value will be sent to the input of the PC at the start of the 4th step.
Also, at the end of the CPU cycle, the register ALUo1 is updated with PC+Offset. (But as you will see, this value will not be used and no harm will be done.)
|
/home/cs355001/demo/pipeline/3-bra-instr Executes: 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 32 // bra +32 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) 44: 0 1 // <---- bra target 0 2 |
Watch the value in the Program Counter (upper left hand corner) change to 00000000 00101100 when the branch takes effect
When that happens, the next instruction fetched is: 00000000 00000001 (= address of "label") (in ID:IR, below the PC probes)