SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: What are branch instructions in ARM Cortex M3?
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

What are branch instructions in ARM Cortex M3?

Ryan Ryan
Last updated: October 5, 2023 9:55 am
Ryan Ryan 10 Min Read
Share
SHARE

The ARM Cortex-M3 is a 32-bit processor core licensed by ARM Holdings. It includes specific instruction sets for enhanced real-time performance, system control and monitoring, security, and power efficiency. One key set of instructions are the branch instructions, which allow the processor to conditionally change the control flow of a program.

Contents
Types of Branch InstructionsUnconditional Branch (B)Branch with Link (BL)Branch Indirect (BX)Compare and Branch (CBZ/CBNZ)If-Then (IT)Conditional Branch (B.cond)Branch Offset and Target AddressBranch PenaltiesRole of Branches in SoftwareBranch Instructions in ARM Cortex-M3Branch Addressing ModesBranch ConditionsTypical Uses of Branches1. Looping2. If-Else3. Switch/Case4. Subroutine Call and ReturnBranch Instructions and Link RegisterBranch Prediction and PipeliningBranch Instruction Summary

In simple terms, branch instructions let the processor decide between continuing to the next instruction in sequence or jumping to a different location in code. This allows for non-linear program execution, which is essential for implementing loops, if-else decisions, and other complex logic flows.

Types of Branch Instructions

The Cortex-M3 supports several types of conditional and unconditional branch instructions:

  • B – Unconditional branch
  • BL – Branch with link
  • BX – Branch indirect
  • CBZ/CBNZ – Compare and branch on zero/non-zero
  • IT – If-Then conditional execution
  • B.cond – Conditional branch

Unconditional Branch (B)

The unconditional branch or B instruction tells the processor to always jump to a specified location. For example: B target_address

This will immediately jump to the target_address, without checking any conditions. Unconditional branches are useful for redirecting code flow unconditionally, like at the end of a subroutine.

Branch with Link (BL)

BL performs an unconditional branch, while also saving the address of the next instruction to the link register (LR). For example: BL subroutine

This jumps to the subroutine label, while saving the return address in LR. The subroutine can later return using: BX LR

Which branches back to the saved return address.

Branch Indirect (BX)

BX performs an unconditional branch to an address specified in a register. For example: BX r0

This branches to the address contained in register r0. BX allows for computed branches based on a register value.

Compare and Branch (CBZ/CBNZ)

Compare and branch instructions check a register value and branch based on the comparison. CBZ branches if the register is zero, while CBNZ branches if the register is non-zero: CBZ r1, label // Branch if r1 is zero CBNZ r2, label // Branch if r2 is non-zero

This provides a shortcut instead of comparing and branching manually.

If-Then (IT)

The IT instruction sets up conditional execution for up to 4 subsequent instructions. It has the form: IT condition next_instruction1 next_instruction2 next_instruction3 next_instruction4

The condition specifies whether the next instructions should be executed based on flags like EQ, NE, etc. IT allows multiple instructions to be conditionally grouped.

Conditional Branch (B.cond)

Conditional branches check the processor flags to decide whether to branch or not. Some examples are: BEQ target // Branch if equal BNE target // Branch if not equal BGT target // Branch if greater than BLT target // Branch if less than

The conditional branches allow if-else logic to be implemented by branching based on a condition flag.

Branch Offset and Target Address

Branch instructions specify a offset rather than an absolute address. This offset is relative to the current Program Counter (PC), which contains the address of the current instruction.

To compute the target address: target_address = PC + offset

The offset is encoded in the instruction op-code, and can be positive or negative. Typical ranges are +/- 16MB for unconditional branches, and +/- 1MB for conditional branches.

Branch Penalties

Branch instructions disrupt the instruction flow and pipeline. The Cortex-M3 reduces this using branch prediction and speculative execution. But mispredicted branches can still incur a 5 cycle penalty.

Some ways to minimize branch penalties:

  • Organize code and loops to reduce branching
  • Use conditional moves instead of conditional branches when possible
  • Enable optimization during compilation to improve branch handling

Careful use of branches is required for efficient real-time code execution on the Cortex-M3.

Role of Branches in Software

Branch instructions are a key element of higher level language constructs and programming paradigms:

  • Loops – Branches are used repetitively for looping back to the start until a condition is met.
  • Subroutines – Functions and subroutines rely on branches to jump to and return from modular code blocks.
  • If/Else – Conditional branches imlement if/else decision making to selectively execute code blocks.
  • Switch/Case – Multiway branching with branch tables implement switch/case statements.
  • While – Loops using pre-test branches form the basis of while loop constructs.
  • Do-While – Post-test loops use branches to repeat code blocks until a test fails.
  • For – For loops contain branches to iterate through ranges or collections.
  • Continue/Break – These keywords generate special purpose branches within loops.

Without branches, code would always execute sequentially. Branches enable the complex logic, control flow, and decision making in modern software applications.

Branch Instructions in ARM Cortex-M3

Here is a summary of key branch instructions supported by the Cortex-M3 architecture and Thumb-2 instruction set:

InstructionDescription
B targetUnconditional branch
BL targetBranch with link
BLX targetBranch indirect with link
BX regBranch indirect via register
CBZ reg, targetBranch if reg is zero
CBNZ reg, targetBranch if reg is non-zero
IT condIf-Then condition for next instructions
B.cond targetConditional branch on status flags

Branch Addressing Modes

Branches specify the target address using different offset addressing modes:

  • Immediate – Small 10-bit offset encoded inline in instruction
  • Literal – Larger 21-bit PC-relative offset requiring literal pool entry
  • Register – Compute address from register (BX or BLX)

Branch Conditions

Conditional branches and the IT instruction use the following condition codes to test the status flags:

ConditionMeaning
EQEqual
NENot equal
CS/HSCarry set/Unsigned higher or same
CC/LOCarry clear/Unsigned lower
MIMinus/Negative
PLPlus/Positive or zero
VSOverflow
VCNo overflow
HIUnsigned higher
LSUnsigned lower or same
GESigned greater than or equal
LTSigned less than
GTSigned greater than
LESigned less than or equal
ALAlways (unconditional)

Typical Uses of Branches

Some typical examples of using branches in Cortex-M3 code:

1. Looping

loop: … // Loop body SUB R1, R1, #1 ; Decrement counter CMP R1, #0 ; Compare to zero BNE loop ; Loop if counter != 0

2. If-Else

CMP R0, R1 ; Compare registers BLT else ; Branch if less than ; if block … B end else: ; else block … end:

3. Switch/Case

; Load test value in R0 CMP R0, #1 BEQ case1 CMP R0, #2 BEQ case2 ; Default case B default case1: … B end case2: … B end default: … end:

4. Subroutine Call and Return

; Store return address BL function ; Function function: … BX LR ; Return ; Caller …

Branch Instructions and Link Register

The link register (LR) plays an important role with branch instructions in Cortex-M3:

  • BL and BLX instructions store return address in LR
  • BX LR returns using the address in LR
  • LR must be preserved across function calls
  • Some compilers use LR for other purposes like inline assembly

Guidelines for working with LR:

  • Try to avoid explicit usage of LR in code
  • Use push/pop instructions to save LR state when needed
  • Review compiler output to see LR usage
  • Initialize LR to known value on exception handlers

Branch Prediction and Pipelining

The Cortex-M3 uses a branch predictor to avoid pipeline stalls for well-behaved forward branches. The predictor has a 2-level adaptive history buffer for predicting branches.

For conditional branches, it predicts based on past history. For unconditional branches, it predicts always taken.

Miscalculated predictions stall the pipeline for 5 cycles while the fetch path is flushed and corrected. Good branch predictability is needed for efficient pipelined execution.

Branch Instruction Summary

In summary, branch instructions like B, BL, CBNZ, CBZ, IT, and B.cond provide the ability to conditionally alter program flow in the Cortex-M3 processor.

They enable non-sequential code execution, which is necessary for higher level control structures used by software.

Efficient usage of branches ensures optimal performance. Cortex-M3 branch prediction reduces penalties, but care is still needed particularly for loops and complex code.

So in the Cortex-M3, branch instructions implement the fundamental mechanism for changes in program control flow during software execution.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Latency differences between timer, GPIO, and RTOS interrupts
Next Article Demystifying Arm’s Branch and Link Instructions (BL and BLX)
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Cortex-M0 Low Power Features (Explained)

The Cortex-M0 processor from ARM is designed to provide an…

11 Min Read

What are the pipeline stages of the Cortex-M3?

The Cortex-M3 is a 32-bit RISC processor designed by ARM…

7 Min Read

ARM Processor Interview Questions and Answers

The ARM processor architecture is widely used in embedded systems…

13 Min Read

How to Set Up External SPI Memory with an ARM Cortex-M3?

Setting up external SPI memory with an ARM Cortex-M3 microcontroller…

15 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account