The Datapath for a Branch
A branch (I-type) instruction has three operands such as
beq $t1, $t2, offset # if ($t1==$t2) then go PC+4+offset
where two registers are compared for equality and offset is for an address.
The 16-bit offset is used to compute the branch target address:
|
|
|
- The address is given by the sum of the offset field of the instruction and the address of the instruction following the branch (PC+4).
- The offset field is shifted left 2 bits so that is a word offset; this shift increases the effective range of the offset field by a factor of 4.
The comparison can be done using the ALU by subtraction of the two register operands.
If the Zero
signal out of the ALU unit is asserted, we know that the two values are equal.
The following figure shows the structure of the datapath segment that handles branches.
The datapath for a branch uses the ALU to evaluate the branch condition and a separate adder to compute the branch target as the sum of the incremented PC and the sign-extended, lower 16 bits of the instruction (the branch displacement), shifted left 2 bits.
|
|
|
Control logic is used to decide whether the incremented PC or branch target should replace the PC, based on the Zero
output of the ALU.