📖 Scenario: You are learning how to write simple decision-making code using ARM assembly language. This is useful when you want your program to choose between two actions based on a condition, like checking if a number is positive or negative.
🎯 Goal: Build a small ARM assembly program that uses an if-else structure to check if a number is zero or not, and sets a register to different values accordingly.
📋 What You'll Learn
Create a register with a specific value to test
Set up a label for branching
Use a conditional branch instruction to implement if-else logic
Set a register to one value if the condition is true, another if false
💡 Why This Matters
🌍 Real World
Assembly language is used in low-level programming for embedded systems, device drivers, and performance-critical code where direct control of hardware is needed.
💼 Career
Understanding if-else in assembly helps in debugging, optimizing code, and working with hardware-level programming in fields like embedded systems engineering and systems programming.
Progress0 / 4 steps
1
DATA SETUP: Initialize register with test value
Load the value 5 into register r0 to use as the test number.
ARM Architecture
Hint
Use the MOV instruction to put the number 5 into r0.
2
CONFIGURATION: Prepare a label for branching
Create a label called else_part that will mark where the else code starts.
ARM Architecture
Hint
Labels end with a colon : and mark a place in the code.
3
CORE LOGIC: Implement if-else using conditional branch
Compare r0 with zero using CMP r0, #0. Then use the conditional branch BEQ else_part to jump to else_part if r0 is zero. If not zero, set r1 to 1. Then use B end_part to skip over the else part.
ARM Architecture
Hint
Use CMP to compare, BEQ to branch if equal, MOV to set r1, and B end_part to skip the else.
4
COMPLETION: Complete else part to set r1 to zero
At label else_part, set r1 to 0. Then create a label end_part: to complete the if-else structure.
ARM Architecture
Hint
After the label else_part, use MOV r1, #0 then add end_part:.
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
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.
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.
Final Answer:
CMP -> Option B
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
Step 1: Understand branch conditions
BEQ means 'Branch if Equal', which jumps if the zero flag is set after a CMP instruction.
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.
Final Answer:
BEQ label -> Option C
Quick Check:
Equal branch = BEQ [OK]
Hint: BEQ branches when values are equal after CMP [OK]
If R2 equals 5, BNE is not taken, so MOV R3, #1 executes, then execution continues to else_label and MOV R3, #0 runs.
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.
Final Answer:
Missing branch to skip else after if block -> Option A
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
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.
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.
Step 3: Use labels to separate blocks
Use labels to mark inner if, else, and end blocks to control flow clearly.
Final Answer:
Use CMP and BGT for R4, then CMP and BEQ for R5 with nested labels -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.