Branch (computer science)

The program counter maintains the memory address of the next machine instruction to be fetched and executed.

The second type of machine level branch is the call instruction which is used to implement subroutines.

Like jump instructions, calls may or may not modify the PC according to condition codes, however, additionally a return address is saved in a secure place in memory (usually in a memory resident data structure called a stack).

This description pertains to ordinary practice; however, the machine programmer has considerable powers to manipulate the return address on the stack, and so redirect program execution in any number of different ways.

Depending on the processor, jump and call instructions may alter the contents of the PC register in different ways.

At the machine level, loops are implemented as ordinary conditional jumps that redirect execution to repeating code.

Depending on the computer architecture, the assembly language mnemonic for a jump instruction is typically some shortened form of the word jump or the word branch, often along with other informative letters (or an extra parameter) representing the condition.

This table lists the machine level branch or jump instructions found in several well-known architectures: * x86, the PDP-11, VAX, and some others, set the carry-flag to signal borrow and clear the carry-flag to signal no borrow.

So conditional branches can cause "stalls" in which the pipeline has to be restarted on a different part of the program.

The optimization would arrange that the fastest branch direction (taken or not) would always be the most frequently taken control flow path.

To run any software, hardware branch predictors moved the statistics into the electronics.

Then the processor's logic gambles on the guess by beginning to execute the expected instruction flow.

Better branch predictors are developed and validated statistically by running them in simulation on a variety of test programs.

Good predictors usually count the outcomes of previous executions of a branch.

[1][2] In fact, branch-free code is a must for cryptography due to timing attacks.