0
0
ARM Architectureknowledge~5 mins

Conditional branch with flags in ARM Architecture

Choose your learning style9 modes available
Introduction

Conditional branches let the program choose what to do next based on results from earlier steps. Flags are special markers that tell if something like a number is zero or negative.

When you want the program to repeat a task only if a condition is true.
When you want to skip some instructions if a calculation result is zero.
When you want to do one thing if a number is positive and another if it is negative.
When you want to stop a loop once a counter reaches zero.
When you want to compare two values and jump to different code based on the comparison.
Core Concept
ARM Architecture
B{condition} label

B stands for branch, which means jump to another part of the program.

The {condition} is a two-letter code that checks flags like zero (Z), negative (N), carry (C), or overflow (V).

Key Points
Branch to label if the zero flag (Z) is set, meaning the last result was zero.
ARM Architecture
BEQ label
Branch to label if the zero flag (Z) is clear, meaning the last result was not zero.
ARM Architecture
BNE label
Branch to label if the last result was greater than zero (signed comparison).
ARM Architecture
BGT label
Branch to label if the last result was less than zero (signed comparison).
ARM Architecture
BLT label
Detailed Explanation

This program counts down from 5 to 0. It subtracts 1 from register R0 each time. The SUBS instruction updates the flags. If R0 becomes zero, the BEQ END instruction jumps to the label END, stopping the loop. Otherwise, it jumps back to LOOP to continue counting down.

ARM Architecture
MOV R0, #5
LOOP:
SUBS R0, R0, #1
BEQ END
B LOOP
END:
OutputSuccess
Important Notes

Flags are set by instructions that end with S, like SUBS or ADDS.

Common condition codes include EQ (equal), NE (not equal), GT (greater than), LT (less than), GE (greater or equal), and LE (less or equal).

Using conditional branches helps make your program decision-based and efficient.

Summary

Conditional branches use flags to decide where the program goes next.

Flags like zero (Z) and negative (N) come from previous instructions that update them.

Branch instructions with conditions let you create loops and decisions in your code.