Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Data processing instructions (ADD, SUB, MOV, CMP) in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Data processing instructions (ADD, SUB, MOV, CMP)
Fetch Instruction
↓
Decode Instruction Type
↓
Is it ADD?
Yes→Perform Addition
↓
Update Flags
↓
Write Result
↓
Is it SUB?
Yes→Perform Subtraction
↓
Update Flags
↓
Write Result
↓
Is it MOV?
Yes→Move Value
↓
Update Flags
↓
Write Result
↓
Is it CMP?
Yes→Compare Values
↓
Update Flags Only
↓
No Write Back
↓
Next Instruction
The processor fetches and decodes the instruction, then performs the operation (ADD, SUB, MOV, CMP), updates condition flags, and writes results if needed.
Execution Sample
ARM Architecture
MOV R1, #5
ADD R2, R1, #3
SUB R3, R2, R1
CMP R3, #5
Moves 5 into R1, adds 3 to R1 into R2, subtracts R1 from R2 into R3, then compares R3 with 5 updating flags.
Analysis Table
StepInstructionOperationOperandsResultFlags Updated
1MOV R1, #5MoveR1 = 5R1=5Yes (NZ)
2ADD R2, R1, #3AddR2 = R1 + 3R2=8Yes (NZ, C)
3SUB R3, R2, R1SubtractR3 = R2 - R1R3=3Yes (NZ, C)
4CMP R3, #5CompareCompare R3 and 5No register changeYes (NZ, C)
5EndNo more instructions---
💡 All instructions executed; CMP updates flags but does not write result.
State Tracker
RegisterStartAfter Step 1After Step 2After Step 3After Step 4
R1Undefined5555
R2UndefinedUndefined888
R3UndefinedUndefinedUndefined33
FlagsUndefinedNZ setNZ, C setNZ, C setNZ, C set
Key Insights - 3 Insights
Why does CMP update flags but not change any register value?
CMP performs a subtraction internally to update flags but does not store the result in any register, as shown in execution_table step 4.
How does MOV differ from ADD and SUB in terms of operands?
MOV copies a value directly to a register without arithmetic, unlike ADD and SUB which perform calculations, as seen in execution_table step 1.
When are condition flags updated during these instructions?
Flags are updated after every instruction here (MOV, ADD, SUB, CMP) to reflect the result status, as indicated in the Flags column of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of R3 after SUB?
A5
B8
C3
DUndefined
💡 Hint
Check the Result column in execution_table row for step 3.
At which step does the CMP instruction update flags without changing registers?
AStep 2
BStep 4
CStep 1
DStep 3
💡 Hint
Look for CMP in the Instruction column and see Flags Updated in execution_table.
If MOV R1, #5 was changed to MOV R1, #10, what would be R2 after step 2?
A13
B15
C8
D5
💡 Hint
R2 = R1 + 3; check variable_tracker for R1 and R2 values.
Concept Snapshot
Data processing instructions perform arithmetic or data moves.
ADD and SUB calculate and store results.
MOV copies a value to a register.
CMP compares values by updating flags only.
All update condition flags (NZCV) to reflect results.
CMP does not write back a result.
Full Transcript
This visual execution trace shows how ARM data processing instructions ADD, SUB, MOV, and CMP work step-by-step. First, MOV sets register R1 to 5. Then ADD adds 3 to R1 and stores 8 in R2. SUB subtracts R1 from R2 and stores 3 in R3. CMP compares R3 with 5, updating flags but not registers. Flags are updated after each instruction to reflect the result status. CMP is unique because it only updates flags without changing register values. This helps in decision-making instructions later. The variable tracker shows how registers and flags change after each step. The quiz questions test understanding of register values and flag updates during execution.

Practice

(1/5)
1. What does the ADD instruction do in ARM architecture?
easy
A. Compares two register values without changing them
B. Subtracts one register value from another
C. Copies a value into a register
D. Adds values from two registers and stores the result in a register

Solution

  1. Step 1: Understand the purpose of ADD

    The ADD instruction performs addition of two values stored in registers.
  2. Step 2: Identify the result storage

    The result of the addition is stored back into a register.
  3. Final Answer:

    Adds values from two registers and stores the result in a register -> Option D
  4. Quick Check:

    ADD means addition operation [OK]
Hint: ADD always means adding two numbers and saving result [OK]
Common Mistakes:
  • Confusing ADD with MOV which copies values
  • Thinking ADD compares values like CMP
  • Assuming ADD subtracts values
2. Which of the following is the correct syntax to move the value 5 into register R1 in ARM assembly?
easy
A. SUB R1, #5
B. MOV R1, #5
C. ADD R1, #5
D. CMP R1, #5

Solution

  1. Step 1: Identify the instruction for copying values

    MOV is used to copy a value into a register.
  2. Step 2: Check the syntax for immediate value

    The immediate value 5 is written as #5, so MOV R1, #5 copies 5 into R1.
  3. Final Answer:

    MOV R1, #5 -> Option B
  4. Quick Check:

    MOV copies value to register [OK]
Hint: MOV copies values; # means immediate number [OK]
Common Mistakes:
  • Using ADD or SUB instead of MOV to copy values
  • Forgetting # before immediate values
  • Using CMP which only compares values
3. Given the instructions:
MOV R0, #10
SUB R0, R0, #3

What is the value in register R0 after execution?
medium
A. 7
B. 13
C. 3
D. 10

Solution

  1. Step 1: Execute MOV instruction

    MOV R0, #10 sets register R0 to 10.
  2. Step 2: Execute SUB instruction

    SUB R0, R0, #3 subtracts 3 from R0 (10 - 3 = 7) and stores result in R0.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    10 minus 3 equals 7 [OK]
Hint: SUB subtracts immediate from register value [OK]
Common Mistakes:
  • Adding instead of subtracting
  • Not updating R0 after SUB
  • Confusing immediate and register operands
4. Identify the error in the following ARM assembly code:
MOV R2, #8
CMP R2, #8
ADD R3, R2
medium
A. MOV instruction cannot use immediate values
B. CMP instruction syntax is incorrect
C. ADD instruction missing second operand
D. No error, code is correct

Solution

  1. Step 1: Check MOV and CMP instructions

    MOV R2, #8 and CMP R2, #8 are correct syntax for moving and comparing immediate values.
  2. Step 2: Analyze ADD instruction

    ADD requires three operands: destination, first source, second source. Here, ADD R3, R2 misses the third operand.
  3. Final Answer:

    ADD instruction missing second operand -> Option C
  4. Quick Check:

    ADD needs 3 operands [OK]
Hint: ADD needs destination and two sources [OK]
Common Mistakes:
  • Assuming CMP syntax is wrong
  • Thinking MOV can't use immediate values
  • Ignoring missing operand in ADD
5. You want to check if the value in register R4 is equal to 15 and then move 1 into R5 if true, else move 0. Which sequence correctly uses ARM instructions?
hard
A. CMP R4, #15
BEQ label_true
MOV R5, #0
B label_end
label_true: MOV R5, #1
label_end:
B. MOV R5, #1
CMP R4, #15
BEQ label_true
MOV R5, #0
label_true:
C. CMP R4, #15
MOV R5, #1
BEQ label_true
MOV R5, #0
label_true:
D. MOV R5, #0
CMP R4, #15
MOV R5, #1
BEQ label_true
label_true:

Solution

  1. Step 1: Compare R4 with 15

    CMP R4, #15 sets flags based on comparison.
  2. Step 2: Use BEQ to branch if equal

    BEQ label_true jumps to label_true if R4 equals 15.
  3. Step 3: Move 0 if not equal, else move 1

    If not equal, MOV R5, #0 executes; branch skips to label_end. At label_true, MOV R5, #1 sets R5 to 1.
  4. Final Answer:

    Sequence in option A correctly implements the logic -> Option A
  5. Quick Check:

    CMP + BEQ + conditional MOV sequence [OK]
Hint: Use CMP then BEQ to branch, MOV sets values [OK]
Common Mistakes:
  • Moving values before comparison
  • Not branching correctly on equality
  • Incorrect order of 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