Why branching controls program execution in ARM Architecture - Performance Analysis
Branching changes which instructions run next in a program. Understanding its time cost helps us see how programs behave as they make decisions.
We ask: How does branching affect the number of steps a program takes?
Analyze the time complexity of the following code snippet.
CMP R0, #0
BEQ label_zero
ADD R1, R1, #1
B end
label_zero:
SUB R1, R1, #1
end:
MOV R2, R1
This code compares a value and branches to different instructions based on the result, controlling which operations run.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single conditional branch deciding which instruction to run next.
- How many times: This snippet runs once per execution; no loops or repeated branches here.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 1 | About 5 instructions |
| 10 | About 50 instructions if repeated 10 times |
| 100 | About 500 instructions if repeated 100 times |
Pattern observation: Each run executes a fixed number of steps; branching only changes which steps run, not how many.
Time Complexity: O(1)
This means the number of steps stays the same no matter the input; branching just picks the path.
[X] Wrong: "Branching always makes the program slower because it adds extra steps."
[OK] Correct: Branching only chooses which instructions run; it does not add extra repeated work by itself.
Understanding how branching affects program steps helps you explain decision-making in code clearly and confidently.
"What if the branch was inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of branching
Branching instructions allow the program to jump to different parts of the code instead of running instructions one after another.Step 2: Identify the effect on execution order
This jump changes the normal sequential flow, enabling decisions and loops.Final Answer:
To change the order in which instructions are executed -> Option AQuick Check:
Branching controls execution flow [OK]
- Confusing branching with arithmetic operations
- Thinking branching stores or loads data
- Assuming branching only repeats instructions
Solution
Step 1: Recall ARM branch instruction syntax
In ARM assembly, the unconditional branch uses the mnemonicBfollowed by the label to jump to.Step 2: Compare options
Only B label uses the correct ARM mnemonic; options A, C, and D are invalid in ARM assembly.Final Answer:
B label -> Option BQuick Check:
Unconditional branch = B label [OK]
- Using non-ARM mnemonics like JMP or GOTO
- Writing full words like BRANCH instead of B
- Confusing conditional and unconditional branch syntax
BNE skip MOV R0, #1 skip: MOV R1, #2
What happens if the zero flag is set before executing this code?
Solution
Step 1: Understand BNE behavior with zero flag set
BNE means 'Branch if Not Equal', which branches if zero flag is clear. Since zero flag is set, branch is NOT taken.Step 2: Trace instruction execution
Because branch is not taken,MOV R0, #1executes, thenMOV R1, #2executes.Final Answer:
R0 is set to 1, then R1 is set to 2 -> Option DQuick Check:
Zero flag set means no branch, both MOV run [OK]
- Assuming branch always happens regardless of flags
- Confusing zero flag set with clear
- Thinking R0 is unchanged when branch not taken
BNE label MOV R0, #5 label: MOV R1, #10
Assuming the zero flag is set, what is wrong?
Solution
Step 1: Analyze branch condition and zero flag
BNE branches if zero flag is clear. Since zero flag is set, branch will not happen.Step 2: Identify correct branch for zero flag set
To branch when zero flag is set, BEQ (Branch if Equal) should be used instead of BNE.Final Answer:
BNE should be BEQ to branch when zero flag is set -> Option AQuick Check:
Branch condition must match zero flag [OK]
- Thinking BNE branches when zero flag is set
- Believing MOV cannot follow branch
- Confusing label syntax with error
Solution
Step 1: Understand looping with branching
To repeat instructions multiple times, a loop is needed that checks a counter and branches back if not done.Step 2: Identify correct branch type for looping
A conditional branch that tests the counter and loops until it reaches zero is the correct method.Final Answer:
Use a conditional branch that loops until a counter reaches zero -> Option CQuick Check:
Looping requires conditional branch with counter check [OK]
- Using unconditional branch without loop control
- Skipping instructions instead of looping
- Assuming branch executes multiple times automatically
Ready to prove it?
Pass the test and this pattern moves unknown
