If-else lets a program choose between two paths based on a condition. In assembly, it helps control what the processor does next.
If-else implementation in assembly in ARM Architecture
CMP Rn, Rm BEQ label_if_equal BNE label_if_not_equal ; code for if part B label_end label_if_not_equal: ; code for else part label_end:
CMP compares two registers by subtracting but does not store the result.
BEQ branches if the compared values are equal; BNE branches if not equal.
is_zero, else it runs the code after BEQ.CMP R0, #0 BEQ is_zero ; code if not zero B end is_zero: ; code if zero end:
greater, else continues with the next code.CMP R1, R2 BGT greater ; code if R1 <= R2 B end greater: ; code if R1 > R2 end:
This program compares the value in register R0 with zero. If R0 is greater than zero, it sets R1 to 1 (meaning positive). Otherwise, it sets R1 to 0 (not positive).
; Check if R0 is positive or not CMP R0, #0 BGT positive ; else part MOV R1, #0 ; R1 = 0 means not positive B end positive: MOV R1, #1 ; R1 = 1 means positive end:
Assembly uses labels and branch instructions to implement if-else logic.
Conditions like equal, not equal, greater than, less than are checked using flags set by CMP.
Always ensure branch labels are unique and placed correctly to avoid errors.
If-else in assembly uses CMP to compare values and branch instructions like BEQ, BNE, BGT to jump to code blocks.
Labels mark the start of if or else code sections.
This lets the processor choose which instructions to run based on conditions.
Practice
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 BQuick Check:
Compare = CMP [OK]
- Confusing CMP with MOV which moves data
- Using ADD instead of CMP for comparison
- Thinking STR stores comparison results
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 CQuick Check:
Equal branch = BEQ [OK]
- Using BNE for equality
- Confusing BL with conditional branch
- Using BGT for equality check
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?
Solution
Step 1: Analyze CMP and BEQ
The CMP compares R0 with 10. If equal, BEQ jumps to equal_label.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.Final Answer:
1 -> Option DQuick Check:
R0=10 triggers BEQ, sets R1=1 [OK]
- Assuming R1 stays 0
- Ignoring the branch to equal_label
- Confusing MOV targets
CMP R2, #5 BNE else_label MOV R3, #1 else_label: MOV R3, #0
Solution
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.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 AQuick Check:
Missing jump after if causes both blocks to run [OK]
- Assuming else block is skipped automatically
- Using wrong branch instruction
- Ignoring label definitions
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 AQuick Check:
Nested if uses CMP + BGT then CMP + BEQ [OK]
- Trying to combine conditions in one CMP
- Skipping branches for nested logic
- Using STR instead of branch instructions
Ready to prove it?
Pass the test and this pattern moves unknown
