executing a branch
A branch instruction decides whether to keep going in a straight line or jump to a different part of the program — it is how loops and if-statements become machine actions. Take 'beq x1, x2, L' meaning 'if x1 equals x2, continue execution at label L; otherwise just go to the next instruction'. Tracing a branch reveals how the datapath both tests a condition and chooses the next instruction's address.
Two computations happen in parallel. First, the comparison: the register file reads x1 and x2, the ALU subtracts them, and a 'zero' result means they were equal (this is the condition test). Second, the target address: the branch offset is taken from the instruction, sign-extended, and added to the current PC to compute the branch target (PC + offset). Then a mux at the very front of the datapath chooses the next PC: if the branch is taken (the ALU's zero flag is set AND the Branch control signal is 1), the next PC is the branch target; otherwise the next PC is the ordinary PC + 4.
The honest subtlety: a branch does not write any register and does not touch data memory (RegWrite, MemRead, MemWrite are all 0). Its only effect is on the program counter — it steers what runs next. That is why, in the pipelined chapter that follows, branches cause control hazards: the machine has often already started fetching the fall-through instruction before it knows whether the branch is taken. In the single-cycle model there is no such problem, because the next PC is settled within the same cycle.
For 'beq x1, x2, L' with x1 = 5, x2 = 5: the ALU computes 5 - 5 = 0, so the zero flag is set; Branch is 1, so the next-PC mux selects PC + offset (the address of L). Had x1 been 7, the difference would be non-zero and the next PC would simply be PC + 4.
A branch tests a condition (ALU) and a mux chooses the next PC: the target if taken, else PC + 4.
A branch's whole job is to set the next PC; it writes no register. 'Taken' just means the condition held and the PC jumps to the target — it is not a separate kind of write-back.