Bird
Raised Fist0
ARM Architectureknowledge~10 mins

If-else implementation in assembly in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - If-else implementation in assembly
Load values into registers
↓
Compare registers
↓
Check condition flags
↓
Execute if-block
↓
Continue execution
The CPU loads values, compares them, sets flags, then jumps to either the if-block or else-block based on the condition, before continuing.
Execution Sample
ARM Architecture
MOV R0, #5
MOV R1, #3
CMP R0, R1
BGT if_block
else_block:
  MOV R2, #0
  B end
if_block:
  MOV R2, #1
end:
This code compares R0 and R1; if R0 > R1, it sets R2 to 1, else sets R2 to 0.
Analysis Table
StepInstructionRegisters BeforeCondition FlagsBranch TakenRegisters After
1MOV R0, #5R0=_, R1=_, R2=_N=_, Z=_, C=_, V=_NoR0=5, R1=_, R2=_
2MOV R1, #3R0=5, R1=_, R2=_N=_, Z=_, C=_, V=_NoR0=5, R1=3, R2=_
3CMP R0, R1R0=5, R1=3, R2=_N=_, Z=_, C=_, V=_NoR0=5, R1=3, R2=_ (flags updated: N=0, Z=0, C=1, V=0)
4BGT if_blockR0=5, R1=3, R2=_N=0, Z=0, C=1, V=0Yes (since R0>R1)No change
5MOV R2, #1 (if_block)R0=5, R1=3, R2=_N=0, Z=0, C=1, V=0NoR0=5, R1=3, R2=1
6end label reachedR0=5, R1=3, R2=1N=0, Z=0, C=1, V=0NoRegisters unchanged
ExitExecution endsR0=5, R1=3, R2=1N=0, Z=0, C=1, V=0NoFinal state reached
💡 Branch taken to if_block because R0 (5) > R1 (3), so if-block executed and else-block skipped.
State Tracker
RegisterStartAfter Step 1After Step 2After Step 3After Step 5Final
R0_55555
R1__3333
R2____11
Key Insights - 2 Insights
Why does the program jump to the if_block instead of else_block after CMP?
Because CMP sets condition flags based on R0 - R1. Since R0=5 is greater than R1=3, the BGT (branch if greater than) condition is true, so the jump goes to if_block as shown in execution_table step 4.
What happens if the condition is false? How does the else_block execute?
If the condition was false (e.g., R0 <= R1), BGT would not branch, so execution continues to else_block instructions. This is shown by the absence of branch in step 4 and execution of else_block instructions instead.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What condition flags indicate the branch to if_block?
AN=1, Z=0, C=0, V=1
BN=0, Z=1, C=1, V=0
CN=0, Z=0, C=1, V=0
DN=1, Z=1, C=0, V=0
💡 Hint
Check the 'Condition Flags' column at step 4 in the execution_table.
At which step does R2 get its final value assigned?
AStep 5
BStep 3
CStep 2
DStep 6
💡 Hint
Look at the 'Registers After' column for R2 in the execution_table.
If R0 was 2 and R1 was 3, which branch would be taken?
ABranch to if_block
BNo branch, continue sequentially
CBranch to else_block
DProgram would crash
💡 Hint
Refer to the logic in concept_flow and how BGT depends on R0 > R1.
Concept Snapshot
If-else in ARM assembly:
- Use CMP to compare two registers.
- CMP sets condition flags (N, Z, C, V).
- Use conditional branch (e.g., BGT) to jump if condition true.
- If branch taken, execute if-block; else execute else-block.
- Continue after blocks with labels and unconditional branch if needed.
Full Transcript
This visual execution trace shows how an if-else structure is implemented in ARM assembly. First, values are loaded into registers R0 and R1. Then CMP compares R0 and R1, setting condition flags. The BGT instruction checks if R0 is greater than R1 by examining flags. If true, it branches to the if_block where R2 is set to 1. Otherwise, it executes the else_block setting R2 to 0. The variable tracker shows how registers change step-by-step. Key moments clarify why branching happens based on flags. The quiz tests understanding of flags, register changes, and branch decisions.

Practice

(1/5)
1. In ARM assembly, which instruction is primarily used to compare two values before an if-else branch?
easy
A. MOV
B. CMP
C. ADD
D. STR

Solution

  1. Step 1: Understand the purpose of CMP

    The CMP instruction compares two registers by subtracting one from the other and setting condition flags without storing the result.
  2. Step 2: Identify its role in if-else

    These condition flags are then used by branch instructions to decide which code path to take, implementing if-else logic.
  3. Final Answer:

    CMP -> Option B
  4. Quick Check:

    Compare = CMP [OK]
Hint: CMP sets flags for branching decisions in if-else [OK]
Common Mistakes:
  • Confusing CMP with MOV which moves data
  • Using ADD instead of CMP for comparison
  • Thinking STR stores comparison results
2. Which of the following is the correct syntax for a conditional branch in ARM assembly that jumps if two values are equal?
easy
A. BNE label
B. BL label
C. BEQ label
D. BGT label

Solution

  1. Step 1: Understand branch conditions

    BEQ means 'Branch if Equal', which jumps if the zero flag is set after a CMP instruction.
  2. Step 2: Match condition to syntax

    BNE means 'Branch if Not Equal', BGT means 'Branch if Greater Than', and BL is 'Branch with Link' (function call), so only BEQ matches the condition for equality.
  3. Final Answer:

    BEQ label -> Option C
  4. Quick Check:

    Equal branch = BEQ [OK]
Hint: BEQ branches when values are equal after CMP [OK]
Common Mistakes:
  • Using BNE for equality
  • Confusing BL with conditional branch
  • Using BGT for equality check
3. Given the ARM assembly snippet:
CMP R0, #10
BEQ equal_label
MOV R1, #0
B end_label
equal_label:
MOV R1, #1
end_label:

What value will R1 hold if R0 equals 10?
medium
A. 0
B. 10
C. Undefined
D. 1

Solution

  1. Step 1: Analyze CMP and BEQ

    The CMP compares R0 with 10. If equal, BEQ jumps to equal_label.
  2. Step 2: Follow the branch logic

    If R0 is 10, execution jumps to equal_label where R1 is set to 1. Otherwise, R1 is set to 0 before jumping to end_label.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    R0=10 triggers BEQ, sets R1=1 [OK]
Hint: If equal, BEQ jumps to set R1=1 [OK]
Common Mistakes:
  • Assuming R1 stays 0
  • Ignoring the branch to equal_label
  • Confusing MOV targets
4. Identify the error in this ARM assembly if-else snippet:
CMP R2, #5
BNE else_label
MOV R3, #1
else_label:
MOV R3, #0
medium
A. Missing branch to skip else after if block
B. Incorrect comparison instruction
C. Wrong register used in MOV
D. No labels defined

Solution

  1. Step 1: Understand the flow

    If R2 equals 5, BNE is not taken, so MOV R3, #1 executes, then execution continues to else_label and MOV R3, #0 runs.
  2. Step 2: Identify missing branch

    There is no branch to skip the else block after the if block, so both blocks run sequentially, overwriting R3.
  3. Final Answer:

    Missing branch to skip else after if block -> Option A
  4. Quick Check:

    Missing jump after if causes both blocks to run [OK]
Hint: Add branch after if block to skip else [OK]
Common Mistakes:
  • Assuming else block is skipped automatically
  • Using wrong branch instruction
  • Ignoring label definitions
5. How would you implement a nested if-else in ARM assembly to check if R4 is greater than 10, then if R5 equals 20, setting R6 to 1; else set R6 to 0?
hard
A. Use CMP and BGT for R4, then CMP and BEQ for R5 with nested labels
B. Use only one CMP for both conditions combined
C. Use MOV instructions without branches
D. Use STR to store comparison results

Solution

  1. Step 1: Implement outer if with CMP and BGT

    Compare R4 with 10 using CMP, then branch with BGT to inner if block if true.
  2. Step 2: Implement inner if with CMP and BEQ

    Inside the inner block, compare R5 with 20 and branch with BEQ to set R6 to 1; else set R6 to 0.
  3. Step 3: Use labels to separate blocks

    Use labels to mark inner if, else, and end blocks to control flow clearly.
  4. Final Answer:

    Use CMP and BGT for R4, then CMP and BEQ for R5 with nested labels -> Option A
  5. Quick Check:

    Nested if uses CMP + BGT then CMP + BEQ [OK]
Hint: Use nested CMP and conditional branches with labels [OK]
Common Mistakes:
  • Trying to combine conditions in one CMP
  • Skipping branches for nested logic
  • Using STR instead of branch instructions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard