Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Compare and branch patterns in ARM Architecture - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare register R0 with zero and branch if equal.

ARM Architecture
CMP R0, [1]
BEQ label_equal
Drag options to blanks, or click blank then click option'
A#1
B#0
CR1
D#-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a register instead of an immediate value for zero comparison.
2fill in blank
medium

Complete the code to branch to label_greater if R2 is greater than R3.

ARM Architecture
CMP R2, R3
B[1] label_greater
Drag options to blanks, or click blank then click option'
AGT
BEQ
CLT
DNE
Attempts:
3 left
💡 Hint
Common Mistakes
Using branch conditions for equality or less than instead of greater than.
3fill in blank
hard

Fix the error in the code to branch if R4 is less than or equal to R5.

ARM Architecture
CMP R4, R5
B[1] label_less_equal
Drag options to blanks, or click blank then click option'
AGE
BLT
CLE
DGT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LT instead of LE causes the branch to miss the equal case.
4fill in blank
hard

Fill both blanks to compare R6 with R7 and branch if not equal.

ARM Architecture
CMP [1], [2]
BNE label_not_equal
Drag options to blanks, or click blank then click option'
AR6
BR7
C#0
DR0
Attempts:
3 left
💡 Hint
Common Mistakes
Using an immediate value instead of the correct registers for comparison.
5fill in blank
hard

Fill all three blanks to compare R8 with immediate 5 and branch if greater or equal.

ARM Architecture
CMP [1], [2]
B[3] label_ge
Drag options to blanks, or click blank then click option'
AR8
B#5
CGE
DLT
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong immediate value or branch condition.

Practice

(1/5)
1. What is the main purpose of the CMP instruction in ARM assembly?
easy
A. To jump to a different part of the program unconditionally
B. To compare two values and set condition flags
C. To add two numbers and store the result
D. To load a value from memory into a register

Solution

  1. Step 1: Understand the role of CMP

    The CMP instruction compares two values by subtracting one from the other without storing the result, but it sets flags based on the outcome.
  2. Step 2: Identify what CMP affects

    These flags are then used by conditional branch instructions to decide program flow.
  3. Final Answer:

    To compare two values and set condition flags -> Option B
  4. Quick Check:

    CMP sets flags for branching decisions [OK]
Hint: CMP sets flags after comparing values for branching [OK]
Common Mistakes:
  • Thinking CMP jumps directly
  • Confusing CMP with arithmetic instructions
  • Assuming CMP stores the subtraction result
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. BGT label
B. BNE label
C. BEQ label
D. BLT label

Solution

  1. Step 1: Recall condition codes for branching

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

    BNE means 'Branch if Not Equal', BGT is 'Branch if Greater Than', and BLT is 'Branch if Less Than'. Only BEQ matches the condition for equality.
  3. Final Answer:

    BEQ label -> Option C
  4. Quick Check:

    BEQ jumps when values are equal [OK]
Hint: BEQ means branch if equal (zero flag set) [OK]
Common Mistakes:
  • Using BNE instead of BEQ for equality
  • Confusing greater than and equal conditions
  • Forgetting to use CMP before branch
3. Given the following ARM assembly code snippet, what will be the value in register r2 after execution?
CMP r0, r1
BEQ equal_label
MOV r2, #0
B end_label
equal_label:
MOV r2, #1
end_label:

Assume r0 = 5 and r1 = 5.
medium
A. Undefined
B. 0
C. 5
D. 1

Solution

  1. Step 1: Analyze the CMP and BEQ instructions

    CMP compares r0 and r1. Since both are 5, they are equal, so the zero flag is set. BEQ will branch to equal_label.
  2. Step 2: Follow the branch and MOV instructions

    The code jumps to equal_label and executes MOV r2, #1, setting r2 to 1. The instructions after BEQ are skipped.
  3. Final Answer:

    1 -> Option D
  4. Quick Check:

    Equal values cause BEQ to set r2 to 1 [OK]
Hint: If CMP finds equal, BEQ jumps to set r2=1 [OK]
Common Mistakes:
  • Ignoring the branch and setting r2 to 0
  • Confusing BEQ with BNE
  • Assuming MOV after BEQ always runs
4. Identify the error in this ARM assembly snippet:
CMP r3, r4
BGT greater_label
MOV r5, #10
greater_label:
MOV r5, #20

What is the problem?
medium
A. Missing unconditional branch to skip MOV after BGT
B. CMP instruction is incorrect
C. MOV instructions use wrong registers
D. BGT should be BGE for correct branching

Solution

  1. Step 1: Understand the flow of conditional branch

    BGT branches to greater_label if r3 > r4. If not, execution continues to MOV r5, #10.
  2. Step 2: Identify missing jump to skip unwanted instruction

    After executing MOV r5, #10, the code falls through to greater_label and executes MOV r5, #20, overwriting the previous value. To avoid this, an unconditional branch is needed after MOV r5, #10.
  3. Final Answer:

    Missing unconditional branch to skip MOV after BGT -> Option A
  4. Quick Check:

    Need unconditional branch to prevent fall-through [OK]
Hint: Add unconditional branch after conditional MOV to avoid fall-through [OK]
Common Mistakes:
  • Thinking CMP is wrong
  • Assuming MOV instructions are incorrect
  • Confusing BGT with BGE
5. You want to write ARM assembly code that sets r0 to 1 if r1 is less than r2, and to 0 otherwise. Which sequence correctly implements this using compare and branch patterns?
hard
A. CMP r1, r2 BLT set_one MOV r0, #0 B end set_one: MOV r0, #1 end:
B. CMP r1, r2 BGT set_one MOV r0, #0 B end set_one: MOV r0, #1 end:
C. CMP r2, r1 BLT set_one MOV r0, #0 B end set_one: MOV r0, #1 end:
D. CMP r1, r2 BEQ set_one MOV r0, #0 B end set_one: MOV r0, #1 end:

Solution

  1. Step 1: Understand the comparison needed

    We want to check if r1 < r2. The CMP instruction compares r1 and r2, setting flags accordingly.
  2. Step 2: Use the correct branch condition

    BLT (Branch if Less Than) jumps if r1 < r2. If true, branch to set_one to set r0 to 1. Otherwise, set r0 to 0.
  3. Step 3: Confirm the flow

    If BLT is taken, r0 = 1; else r0 = 0. The unconditional branch after MOV r0, #0 skips the set_one label to avoid overwriting.
  4. Final Answer:

    CMP r1, r2 BLT set_one MOV r0, #0 B end set_one: MOV r0, #1 end: -> Option A
  5. Quick Check:

    Use CMP + BLT for less than check [OK]
Hint: Use CMP then BLT to branch if less, else set zero [OK]
Common Mistakes:
  • Swapping operands in CMP
  • Using BGT instead of BLT
  • Forgetting unconditional branch after MOV 0
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