Skip to main content
\(\newcommand{\doubler}[1]{2#1} \newcommand{\binary}{\mathtt} \newcommand{\hex}{\mathtt} \newcommand{\octal}{\mathtt} \newcommand{\prog}{\mathtt} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \)

Section11.4Branching

A very common operation in computer programs is transferring execution from one place to another. You saw an example of this in Section 9.2 with the bx lr instruction. This is an unconditional branch. Conditional branches are taken or not on the basis of the settings of the condition flags. You will learn about using conditional branches in Chapter 12.

The bx instruction introduced in Section 9.2 uses the contents of a register for the target address. There is also a form that uses a label in the program.

B

Branches to a labeled location in the program.

B{<c>}    <label>

  • <c> can be used to specify a condition under which this instruction will be executed, Table 9.2.1.

  • <label> is a labeled address in the program.

The address corresponding to the label is moved to the pc, thus causing program execution to branch to that location.

An example of a conditional branch is:

bne     else

The ‘ne’ condition means “not equal.” Looking on Table 9.2.1, this occurs when the Z condition flag is zero. That is, the result of the last instruction to set the condition flags was not zero.

Unlike the bx instruction, the b instruction does not allow the program to change instruction sets between ARM and Thumb.

Another very common type of branch is to call a function, in which case the branch instruction must include a means to provide the return address to the called function. You have already seen the branch that is used to call a function Section 10.1). For example, in the program in Listing 11.2.3, a call to printf is made with the bl instruction:

bl      printf

Control is transferred to the address labeled “printf,” which is the beginning of the printf function. The return address, the address of the instruction immediately following the bl instruction, is placed in the link register—lr or r14.