If-else implementation in assembly in ARM Architecture - Time & Space Complexity
Analyzing time complexity helps us understand how the execution time changes when running an if-else in assembly.
We want to know how the number of steps grows as the input or conditions change.
Analyze the time complexity of the following ARM assembly code implementing an if-else.
CMP R0, #0
BEQ else_label
MOV R1, #1
B end_if
else_label:
MOV R1, #0
end_if:
This code compares a value and sets another register based on the condition.
Look for repeated steps or loops.
- Primary operation: Single comparison and branch instructions.
- How many times: Each instruction runs once per execution; no loops or repeated traversals.
The number of instructions executed stays almost the same regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 6 instructions |
| 100 | 6 instructions |
| 1000 | 6 instructions |
Pattern observation: Execution steps do not increase with input size; they remain constant.
Time Complexity: O(1)
This means the time to run the if-else does not grow as the input changes; it stays constant.
[X] Wrong: "If-else in assembly takes longer when the input value is bigger."
[OK] Correct: The instructions run once regardless of input size; the condition only changes which path runs, not how many steps.
Understanding that simple conditional branches run in constant time helps you explain how decisions affect program speed clearly and confidently.
"What if the if-else was inside a loop running n times? How would the time complexity change?"