Branch Instruction in Computer Organization
A branch is an instruction in a computer program that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behaviour of executing instructions in order. Branch may also refer to the act of switching execution to a different instruction sequence as a result of executing a branch instruction. Branch instructions are used to implement control flow in program loops and conditionals (i.e., executing a particular sequence of instructions only if certain conditions are satisfied).
A branch instruction can be either an unconditional branch, which always results in branching or a conditional branch, which may or may not cause branching depending on some condition. Also, depending on how it specifies the address of the new instruction sequence (the target address).
A branch instruction is generally classified as direct, indirect or relative. It means the instruction contains the target address, specifies where the target address is to be found (e.g., a register or memory location), or specifies the difference between the current and target addresses. A branch instruction computes the target address in one of four ways:
The target address can be computed sufficiently ahead of the branch to pre-fetch instructions along the target path using the first two methods.
Using the third and fourth methods, pre-fetching instructions along the branch path is also possible provided the Link Register or Count Register is loaded sufficiently ahead of the branch instruction.
Types of Brach Instructions
There are three types of branching instructions in computer organization:
1. Jump Instructions
The jump instruction transfers the program sequence to the memory address given in the operand based on the specified flag. Jump instructions are further divided into two parts, Unconditional Jump Instructions and Conditional Jump Instructions.
2. Call Instructions
The call instruction transfers the program sequence to the memory address given in the operand. Before transferring, the address of the next instruction after CALL is pushed onto the stack. Call instructions are also two types: Unconditional Call Instructions and Conditional Call Instructions.
3. Return Instructions
The return instruction transfers the program sequence from the subroutine to the calling program. Return instructions are two types: Unconditional Jump Instructions and Conditional Jump Instructions.
Implementation of Branch Instruction
Mechanically, a branch instruction can change the program counter of a CPU. The program counter stores the memory address of the next instruction to be executed. Therefore, a branch can cause the CPU to begin fetching its instructions from a different sequence of memory cells. Machine level branch instructions are sometimes called jump instructions.
Handling of Branch Instructions
Branch instructions can handle in several ways to reduce their negative impact on the rate of execution of instructions.
1. Branch delay slot
The processor fetches the next instructions before it determines whether the current instruction is a branch instruction. When execution of current instruction is completed, and a branch is to be made, the processor must discard remaining instructions and fetch the new branched instruction at the branch target. The location following a branch instruction is called a branch delay slot. Depending on the time to execute a branch instruction, there may be more than one branch delay slot.
A technique called delayed branching can minimize the penalty incurred as a result of conditional branch instructions. The instructions in the delay slots are always fetched. Therefore, we would like to arrange for them to be fully executed whether the branch is taken or not taken. The objective is to be able to place useful instructions in these slots. If no useful instructions can be placed in the delay slots, these slots must be filled with NOP instructions.
2. Branch prediction
Branch prediction took statistics and used the result to optimize code. A programmer would compile a test version of a program and run it with test data.
The problem with software branch prediction is that it requires a complex software development process.
3. Branch-free code
Some logic can be written without branches or with fewer branches. It is often possible to use bitwise operations, conditional moves or other predication instead of branches. Branch-free code is a must for cryptography due to timing attacks.
4. Hardware branch predictors
To run any software, hardware branch predictors moved the statistics into the electronics. Branch predictors are parts of a processor that guess the outcome of a conditional branch. Then the processor's logic gambles on the guess by beginning to execute the expected instruction flow.
An example of a simple hardware branch prediction scheme is to assume that all backward branches (to a smaller program counter) are taken (because they are part of a loop), and all forward branches (to a larger program counter) are not taken (because they leave a loop).
Better branch predictors are developed and validated statistically by running them in simulation on various test programs. Good predictors usually count the outcomes of previous executions of a branch.