Bird
Raised Fist0
ARM Architectureknowledge~15 mins

Data processing instructions (ADD, SUB, MOV, CMP) in ARM Architecture - Deep Dive

Choose your learning style10 modes available
Overview - Data processing instructions (ADD, SUB, MOV, CMP)
What is it?
Data processing instructions in ARM architecture are commands that perform basic arithmetic and logical operations on data stored in registers. These include instructions like ADD (addition), SUB (subtraction), MOV (move data), and CMP (compare values). They manipulate values directly within the CPU to help execute programs. These instructions are fundamental for calculations, data movement, and decision-making in a program.
Why it matters
Without data processing instructions, a computer's processor would not be able to perform calculations, move data between registers, or make decisions based on data comparisons. This would make it impossible to run any meaningful software, from simple calculators to complex operating systems. These instructions enable the processor to transform raw data into useful results, powering everything from apps to embedded devices.
Where it fits
Before learning data processing instructions, you should understand the basics of CPU registers and binary numbers. After mastering these instructions, you can learn about more complex ARM instructions like load/store, branch, and multiply instructions, which build on these fundamental operations.
Mental Model
Core Idea
Data processing instructions are the CPU's way of performing simple math and data moves directly inside its fast storage areas called registers.
Think of it like...
Imagine a kitchen where the chef (CPU) uses bowls (registers) to hold ingredients (data). ADD is like mixing two bowls of ingredients together, SUB is removing some amount from a bowl, MOV is transferring ingredients from one bowl to another, and CMP is tasting two bowls to see if they are the same or different.
┌───────────────┐
│   Register A  │
│     (Data)    │
└──────┬────────┘
       │
       │ ADD, SUB, MOV, CMP
       ▼
┌───────────────┐
│   Register B  │
│     (Data)    │
└───────────────┘

Operations happen between these registers inside the CPU.
Build-Up - 7 Steps
1
FoundationUnderstanding CPU Registers
🤔
Concept: Registers are small storage locations inside the CPU that hold data temporarily for processing.
Registers are like tiny, super-fast boxes inside the processor where data is kept while the CPU works on it. Unlike memory, registers can be accessed instantly, which makes operations very fast. ARM processors have multiple general-purpose registers named R0, R1, R2, and so on.
Result
You know where data lives inside the CPU and why registers are important for fast processing.
Understanding registers is key because data processing instructions operate directly on these fast storage locations, not on slower memory.
2
FoundationBasic Arithmetic Operations
🤔
Concept: ADD and SUB instructions perform addition and subtraction on values in registers.
The ADD instruction takes two register values, adds them, and stores the result in a register. SUB subtracts one register's value from another. For example, ADD R0, R1, R2 means add the values in R1 and R2, then store the result in R0.
Result
You can perform simple math inside the CPU using registers and these instructions.
Knowing how to add and subtract data inside registers is the foundation for all calculations in programs.
3
IntermediateMoving Data with MOV Instruction
🤔
Concept: MOV copies data from one register or immediate value into another register.
MOV is used to transfer data without changing it. For example, MOV R0, R1 copies the value from R1 into R0. MOV can also load a constant number directly into a register, like MOV R0, #5.
Result
You can place data where you need it inside the CPU for further operations.
Understanding MOV helps you control where data lives, which is essential before performing calculations or comparisons.
4
IntermediateComparing Values with CMP Instruction
🤔Before reading on: Do you think CMP changes the values in the registers it compares? Commit to yes or no.
Concept: CMP compares two register values by subtracting one from the other but does not store the result; instead, it updates status flags.
CMP works like SUB but only sets special flags inside the CPU to show if one value is equal to, greater than, or less than the other. These flags help the CPU decide what to do next, like jumping to a different instruction.
Result
You can check conditions without changing data, enabling decision-making in programs.
Knowing that CMP only sets flags and does not alter data is crucial for writing correct conditional code.
5
IntermediateStatus Flags and Conditional Execution
🤔Before reading on: Does the ADD instruction always update status flags by default? Commit to yes or no.
Concept: Data processing instructions can update status flags that record results like zero, carry, or negative, which control conditional actions.
Instructions like ADD, SUB, and CMP can set flags such as Zero (Z), Carry (C), Negative (N), and Overflow (V). These flags tell the CPU about the result of the operation. For example, if the result is zero, the Z flag is set. Conditional instructions use these flags to decide program flow.
Result
You understand how arithmetic results influence program decisions.
Recognizing the role of status flags connects arithmetic operations to program control flow.
6
AdvancedInstruction Encoding and Operand Flexibility
🤔Before reading on: Can MOV only move data between registers, or can it also load constants? Commit to your answer.
Concept: ARM instructions encode operations compactly and allow operands to be registers or immediate values with flexible shifts.
Each data processing instruction is encoded in 32 bits with fields for opcode, destination register, source registers, and optional immediate values. MOV can load constants directly. ADD and SUB can use shifted registers as operands, enabling complex calculations in one instruction.
Result
You see how ARM packs powerful operations into simple instructions.
Understanding instruction encoding reveals how ARM achieves efficiency and flexibility in data processing.
7
ExpertFlags Side Effects and Instruction Variants
🤔Before reading on: Do all data processing instructions update flags by default? Commit to yes or no.
Concept: Some instructions update status flags only when suffixed with 'S', affecting conditional execution and performance.
In ARM, instructions like ADD and SUB have variants: ADD and ADDS. The 'S' suffix means the instruction updates flags. Without 'S', flags remain unchanged. This subtlety affects how subsequent instructions behave and can optimize performance by avoiding unnecessary flag updates.
Result
You can write more efficient and correct ARM code by controlling flag updates.
Knowing when and how flags update prevents bugs and improves performance in real-world ARM programming.
Under the Hood
Data processing instructions operate inside the CPU's Arithmetic Logic Unit (ALU). When an instruction like ADD is executed, the ALU takes input values from specified registers, performs the arithmetic or logical operation, and writes the result back to a register. For CMP, the ALU performs a subtraction but only updates the condition flags without storing the result. The CPU's status register holds these flags, which influence conditional instructions that follow.
Why designed this way?
ARM's data processing instructions were designed for simplicity, speed, and flexibility. Using registers for all operations avoids slow memory access. The option to update flags selectively allows programmers to optimize performance by avoiding unnecessary flag changes. Encoding immediate values and shifted registers in instructions reduces the number of instructions needed, making ARM efficient for embedded and mobile devices where power and speed matter.
┌───────────────┐       ┌───────────────┐
│   Register 1  │──────▶│               │
│   (Operand)   │       │               │
└───────────────┘       │               │
                        │      ALU      │─────▶┌───────────────┐
┌───────────────┐       │               │      │   Register 3  │
│   Register 2  │──────▶│               │      │  (Destination)│
│   (Operand)   │       └───────────────┘      └───────────────┘
└───────────────┘

ALU updates status flags ───────────────▶ Status Register (Flags)
Myth Busters - 4 Common Misconceptions
Quick: Does CMP change the values in the registers it compares? Commit to yes or no.
Common Belief:CMP changes the values in the registers because it subtracts one from the other.
Tap to reveal reality
Reality:CMP does not change register values; it only updates the CPU's status flags based on the subtraction result.
Why it matters:Assuming CMP changes data can lead to bugs where the original data is unexpectedly altered, causing incorrect program behavior.
Quick: Do all ADD instructions update the status flags by default? Commit to yes or no.
Common Belief:Every ADD instruction automatically updates the status flags after execution.
Tap to reveal reality
Reality:Only ADD instructions with the 'S' suffix (ADDS) update the status flags; plain ADD does not.
Why it matters:Misunderstanding this can cause conditional code to behave incorrectly or reduce performance by unnecessary flag updates.
Quick: Can MOV only move data between registers? Commit to yes or no.
Common Belief:MOV can only copy data from one register to another.
Tap to reveal reality
Reality:MOV can also load immediate constant values directly into registers.
Why it matters:Not knowing this limits the ability to initialize registers efficiently, leading to longer or more complex code.
Quick: Does SUB always produce a negative result if the second operand is larger? Commit to yes or no.
Common Belief:SUB will always produce a negative number if the second operand is bigger than the first.
Tap to reveal reality
Reality:SUB results are unsigned in ARM registers; the CPU sets flags to indicate if a borrow occurred, but the register holds the raw binary result.
Why it matters:Assuming SUB always produces negative values can cause confusion in interpreting results and flags, leading to logic errors.
Expert Zone
1
Some data processing instructions can use shifted register operands, allowing complex calculations in a single instruction without extra steps.
2
The selective flag update via the 'S' suffix is critical for optimizing performance and controlling conditional execution flow precisely.
3
Immediate values in instructions are limited in size and format, requiring clever encoding schemes that experienced programmers must understand to write efficient code.
When NOT to use
Data processing instructions operate only on registers and immediate values; they cannot directly access memory. For operations involving memory, use load/store instructions. For multiplication or division, use dedicated multiply or divide instructions instead of multiple ADD or SUB instructions to improve efficiency.
Production Patterns
In real-world ARM programming, data processing instructions are combined with conditional execution to minimize branching, improving speed and reducing code size. Efficient use of MOV with immediate values initializes registers quickly. Advanced use includes combining shifted operands in ADD or SUB to perform address calculations or bit manipulations in one instruction.
Connections
Boolean Algebra
Data processing instructions perform arithmetic and logical operations that are based on Boolean algebra principles.
Understanding Boolean algebra helps grasp how logical operations and flag settings work at the binary level inside the CPU.
Digital Circuit Design
The ALU hardware that executes data processing instructions is designed using digital circuits implementing arithmetic and logic gates.
Knowing digital circuit design principles explains how instructions translate into electrical signals and operations inside the CPU.
Decision Making in Psychology
CMP and flag-based conditional execution in CPUs mirror how humans compare options and make decisions based on conditions.
Recognizing this parallel shows how computers mimic basic decision processes, linking computing with cognitive science.
Common Pitfalls
#1Using CMP expecting it to change register values.
Wrong approach:CMP R0, R1 MOV R2, R0 ; expecting R0 to be changed by CMP
Correct approach:CMP R0, R1 ; Use conditional branch or instructions based on flags, not expecting R0 to change
Root cause:Misunderstanding that CMP only sets flags and does not modify registers.
#2Forgetting to use the 'S' suffix when flag updates are needed.
Wrong approach:ADD R0, R1, R2 ; flags not updated BEQ label ; branch on zero flag expected but not set
Correct approach:ADDS R0, R1, R2 ; flags updated BEQ label ; branch works correctly
Root cause:Not knowing that only instructions with 'S' update status flags.
#3Trying to load a constant into a register using ADD instead of MOV.
Wrong approach:ADD R0, R0, #5 ; inefficient or incorrect for initialization
Correct approach:MOV R0, #5 ; direct load of immediate value
Root cause:Not realizing MOV can load immediate values directly.
Key Takeaways
Data processing instructions like ADD, SUB, MOV, and CMP operate directly on CPU registers to perform arithmetic, data movement, and comparisons.
CMP does not change register values but sets status flags used for conditional decisions in programs.
The 'S' suffix on instructions controls whether status flags are updated, affecting program flow and performance.
MOV can transfer data between registers or load immediate constants, making it versatile for data setup.
Understanding how these instructions interact with registers and flags is essential for efficient and correct ARM programming.

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