CHAPTER 8Control Flow

Instructions are executed sequentially. But what happens when a program uses a condition statement or calls a subroutine? The first section of this chapter discusses how branch instructions alter the flow of execution. As you will learn in the second part of this chapter, these instructions are primarily used to invoke functions and subroutines, which will be discussed in more detail.

Branch Instructions

Branch instructions change the flow of execution by updating the program counter to the target address specified in the branch instruction. In assembly, this target address can be specified either as a label or as a register holding an address. At the binary level, labels are encoded into instructions as an immediate offset that is added to the PC when the instruction is executed. Together, these branch instructions can be used to encode conditional logic, loops, and calls to subroutines.

Table 8.1 contains an overview of branch instructions that branch to a specified label.

Table 8.1: Immediate Branches

STATE INSTRUCTION SYNTAX
AArch64 Unconditional branch B <label>
Conditional branch B.<cond> <label>
Branch with link BL <label>
Compare and branch if nonzero CBNZ Wt|Xt, <label>
Compare and branch if zero CBZ Wt|Xt, <label>
Test bit and branch nonzero TBNZ Wt|Xt, #imm, <label>
Test bit and branch zero TBZ Wt|Xt, #imm, <label>
AArch32 Unconditional branch B <label>
Branch unconditionally B<cond> <label>
Branch ...

Get Blue Fox now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.