HOME

TheInfoList




A branch is an instruction in a
computer program In imperative programming, a computer program is a sequence of instructions in a programming language that a computer can execute or interpret. In declarative programming, a ''computer program'' is a Set (mathematics), set of instructions. A comp ...
that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behavior of executing instructions in order. Branch (or ''branching'', ''branched'') 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 computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of Algor ...
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'', meaning that the instruction contains the target address, or it specifies where the target address is to be found (e.g., a register or memory location), or it specifies the difference between the current and target addresses.A Survey of Techniques for Dynamic Branch Prediction
, S. Mittal, CPE 2018


Implementation

Mechanically, a branch instruction can change the
program counter The program counter (PC), commonly called the instruction pointer (IP) in Intel Intel Corporation is an American multinational corporation A multinational company (MNC) is a corporate A corporation is an organization—usually a gr ...
(PC) of a
CPU A central processing unit (CPU), also called a central processor, main processor or just processor, is the electronic circuit File:PExdcr01CJC.jpg, 200px, A circuit built on a printed circuit board (PCB). An electronic circuit is composed of ...

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. When a branch is ''taken'', the CPU's program counter is set to the
argument In logic Logic is an interdisciplinary field which studies truth and reasoning Reason is the capacity of consciously making sense of things, applying logic Logic (from Ancient Greek, Greek: grc, wikt:λογική, λογική, la ...
of the jump instruction. So, the next instruction becomes the instruction at that address in
memory Memory is the faculty of the brain A brain is an organ Organ may refer to: Biology * Organ (anatomy) An organ is a group of Tissue (biology), tissues with similar functions. Plant life and animal life rely on many organs that co-exis ...
. Therefore, the flow of control changes. When a branch is ''not taken'', the CPU's program counter is unchanged. Therefore, the next instruction executed is the instruction after the branch instruction. Therefore, the flow of control is unchanged. The term ''branch'' can be used when referring to programs in high level languages as well as the programs written in
machine code In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, ge ...
or
assembly language In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, gene ...
. In
high-level programming language In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , , ...
s, branches usually take the form of conditional statements of various forms that encapsulate the instruction sequence that will be executed if the conditions are satisfied. Unconditional branch instructions such as
GOTO GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement Statement or statements may refer to: Common uses *Statement (computer science), the smallest standalone element of an imperative programmin ...
are used to unconditionally "jump" to (begin execution of) a different instruction sequence. Machine level branch instructions are sometimes called ''jump'' instructions. Machine level jump instructions typically have ''unconditional'' and ''conditional'' forms where the latter may be ''taken'' or ''not taken'' depending on some condition. Usually there are distinct forms for one-way jumps, often called ''jump'' and subroutine invocations known as ''call'' which automatically save the originating address as a return address on the stack, allowing a single subroutine to be invoked from multiple locations in code. In CPUs with flag registers, an earlier instruction sets a condition in the flag register. The earlier instruction may be arithmetic, or a logic instruction. It is often close to the branch, though not necessarily the instruction ''immediately'' before the branch. The stored condition is then used in a branch such as ''jump if overflow-flag set''. This temporary information is often stored in a flag register but may also be located elsewhere. A flag register design is simple in slower, simple computers. In fast computers a flag register can place a bottleneck on speed, because instructions that could otherwise operate in parallel (in several
execution unit Capital punishment, also known as the death penalty, is the state-sanctioned killing of a person as punishment for a crime. The sentence ordering that someone is punished with the death penalty is called a death sentence, and the act of ca ...
s) need to set the flag bits in a particular sequence. There are also machines (or particular instructions) where the condition may be checked by the jump instruction itself, such as ''branch