Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

ARM data processing instructions

Clear, exam-focused notes on ARM 32-bit data-processing instructions: encoding, operand2 (immediate and shifts), condition flags, common opcodes and examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

ARM data-processing instructions implement the basic arithmetic and logical operations that firmware uses for decision making, arithmetic, bit manipulation, and setting condition flags. An engineer uses them whenever writing assembly for control, writing compiler back-ends, or debugging low-level boot and driver code (see related topics: ARM core: dataflow, registers and CPSR; ARM pipeline; and ARM load–store and software interrupt instructions).

Key ideas

Start with definitions and the single-instruction model used by classic 32-bit ARM (ARM state):

  • Data-processing instruction: an ARM instruction that computes a binary operation (arithmetic or logical) using a first operand in a register (Rn) and a second operand (Operand2) which is either an immediate value or a register (optionally shifted). It can write the result to a destination register (Rd) and optionally update condition flags in the Current Program Status Register (CPSR).

  • CPSR flags: four single-bit flags used for conditional execution and branch decisions: N (negative) = result[31], Z (zero) = (result == 0), C (carry) = carry-out from the operation or shifter (explained later), V (overflow) = two's-complement signed overflow indicator.

  • Instruction 32-bit layout (draw this bit-field diagram and label each field; show most-significant bit on left): a 32-bit box labelled bits 31..0 separated into fields:

    • bits 31–28: Cond (condition code)
    • bits 27–26: 00 (data-processing class identifier)
    • bit 25: I (immediate flag: 1 = Operand2 is immediate, 0 = register-shift form)
    • bits 24–21: Opcode (4 bits)
    • bit 20: S (set condition codes if 1)
    • bits 19–16: Rn (first operand register)
    • bits 15–12: Rd (destination register)
    • bits 11–0: Operand2 (immediate-rotate or register+shift fields)
  • Operand2 forms:

    • If I = 1: Operand2 encodes an 8-bit immediate (Imm8) and a 4-bit rotate (Rotate_imm). The 32-bit immediate value is produced as: rotate-right(Imm8, Rotate_imm * 2). This allows many 32-bit constants that are an 8-bit value rotated by an even number of bits.
    • If I = 0: Operand2 is a register (Rm) optionally shifted. Shift types: LSL (logical shift left), LSR (logical shift right), ASR (arithmetic shift right), ROR (rotate right). The shift amount may be an immediate (5 bits) or specified in a register (low byte of Rs). For ROR with immediate shift amount 0 (immediate-shift encoding), ARM uses the special RRX (rotate right extended) that shifts one bit from CPSR.C into bit31. For the register-specified shift encoding, a shift amount of 0 performs no shift and leaves the shifter carry-out unchanged (old CPSR.C).
  • Opcode list (common opcodes and their mnemonic meaning). For exam answers, memorize mapping from opcode field to operation; draw a 4-bit table. Important opcodes (mnemonic → effect):

    • AND, EOR, SUB, RSB, ADD, ADC, SBC, RSC, TST, TEQ, CMP, CMN, ORR, MOV, BIC, MVN.
    • Note: TST/TEQ/CMP/CMN are the test/compare variants that update flags and do not write Rd (Rd field is ignored). Opcode field encodings (bits 24–21): 0000=AND, 0001=EOR, 0010=SUB, 0011=RSB, 0100=ADD, 0101=ADC, 0110=SBC, 0111=RSC, 1000=TST, 1001=TEQ, 1010=CMP, 1011=CMN, 1100=ORR, 1101=MOV, 1110=BIC, 1111=MVN.
  • S bit semantics: When S = 1, the instruction updates CPSR.N and CPSR.Z based on the result; CPSR.C is updated either from the ALU carry-out (for add/sub) or from the shifter carry-out (for logical operations); CPSR.V is updated to indicate signed overflow for add/sub.

  • Carry and overflow definitions (exact statements students should use):

    • Carry-out (C): For addition (ADD/ADC), C is the carry-out from bit 31 of the unsigned addition; ADC also adds the incoming CPSR.C as a carry-in. For subtraction/compare (SUB, RSB, CMP), C = NOT borrow (i.e., C = 1 means no borrow). For SBC/RSC, C = NOT borrow from the subtraction that includes the carry-in. For logical instructions (AND, ORR, EOR, BIC, MOV, MVN), C comes from the shifter stage: it is the bit shifted out by Operand2.
    • Overflow (V): For two's-complement arithmetic, overflow occurs when the signed result does not fit in 32 bits. For addition, an overflow occurs if the two operands have the same sign and the result has a different sign. Algebraic form often used:
      • For addition: V = (a31 & b31 & ~res31) | (~a31 & ~b31 & res31)
      • For subtraction (a - b): V = (a31 & ~b31 & ~res31) | (~a31 & b31 & res31)
    • N: set to result bit 31. Z: set if result == 0.
  • Special behaviours to remember:

    • The top 4 bits (Cond) conditionally execute the whole instruction. If the condition fails, the instruction does nothing (no flags change).
    • Using PC (R15) as Rd writes a new PC; when a data-processing instruction writes PC, execution branches — if S is set and processor is in a privileged mode that allows it, CPSR may be changed from SPSR (mode specific) — details are architecture-dependent. When PC (R15) is used as an operand (Rn or Rm), in ARM state it reads as the address of the current instruction plus 8.
    • Shifts by zero: LSL #0 means no shift and the shifter carry-out is unchanged; LSR #0 encodes an LSR by 32 (result = 0, shifter carry-out = bit31 of Rm); ASR #0 encodes an ASR by 32 (result = all copies of bit31 of Rm, shifter carry-out = bit31 of Rm); ROR #0 (immediate form) means RRX (rotate right extended). For register-specified shifts with a shift amount of 0, no shift occurs and the shifter carry-out is unchanged.

Formulas

Instruction encoding: cond(31-28) | 00(27-26) | I(25) | opcode(24-21) | S(20) | Rn(19-16) | Rd(15-12) | Operand2(11-0) — bit positions in the 32-bit word.

imm32 = ROR(Imm8, Rotate_imm * 2) — when I = 1. Imm8 is an 8-bit immediate; Rotate_imm is 4 bits (0..15); ROR means rotate-right.

ADD{cond}{S} Rd, Rn, Operand2 — general syntax pattern. Symbols: cond = optional condition code, S = optional set-flags suffix.

N = result[31] (1 if MSB of result is 1)

Z = 1 if result == 0 else 0

C (for add/sub) = carry-out of bit 31 (unsigned carry) ; C (for logical ops) = carry-out from shifter.

V (addition) = (a31 & b31 & ~r31) | (~a31 & ~b31 & r31) — signed overflow for addition. Units: dimensionless (bits/flags). Apply when S = 1 and operation is arithmetic.

Worked examples

Example 1 — basic add with shift and flags Problem: Given registers R1 = 0x70000000, R2 = 0x20000000. Execute: ADDS R0, R1, R2, LSL #2 and compute R0, N, Z, C, V. Steps:

  1. Compute shifted Operand2: R2 LSL #2 = 0x20000000 << 2 = 0x80000000.
  2. Add: R0 = 0x70000000 + 0x80000000 = 0xF0000000 (no 33rd-bit carry).
  3. N = bit31 of R0 = 1 (since 0xF... has MSB 1).
  4. Z = (R0 == 0) ? 1 : 0 (result is non-zero, so Z = 0)
  5. C = carry-out of addition = 0 (no unsigned carry beyond 32 bits)
  6. V: check signed operands: R1 signed positive (msb=0), shifted R2 signed negative (msb=1), operands had different signs so signed overflow cannot occur ⇒ V = 0. Answer (bold form for exam): R0 = 0xF0000000, N = 1, Z = 0, C = 0, V = 0.

Example 2 — overflow case and flags Problem: R1 = 0x7FFFFFFF, R2 = #1. Execute ADDS R0, R1, R2. Steps:

  1. Add: 0x7FFFFFFF + 0x1 = 0x80000000.
  2. R0 = 0x80000000.
  3. N = 1 (MSB = 1).
  4. Z = 0 (result non-zero).
  5. C = 0 (no unsigned carry-out: sum fits within 32 bits exactly without carry beyond bit31).
  6. V: both operands were positive (msb 0) and result has msb 1 => signed overflow. Using formula for addition gives V = 1. Answer: R0 = 0x80000000, N = 1, Z = 0, C = 0, V = 1.

Example 3 — immediate encoding and rotate Problem: How does the instruction MOV R0, #0xFF000000 encode Imm8 and Rotate_imm? What is Imm8 and Rotate_imm, and how ARM computes the 32-bit immediate? Steps:

  1. Observe 0xFF000000 equals rotating 0x000000FF right by 8 bits.
  2. Imm8 = 0xFF. Rotate amount = 8 bits. Rotate_imm field = rotate / 2 = 4 (because Rotate_imm encodes multiples of 2).
  3. Compute: imm32 = ROR(0x000000FF, 8) = 0xFF000000. Answer: Imm8 = 0xFF, Rotate_imm = 4; imm32 computed with imm32 = ROR(Imm8, Rotate_imm*2) yields 0xFF000000.

Common mistakes

  • Forgetting that TST/TEQ/CMP/CMN update flags and ignore Rd — they do not store a result.
  • Misunderstanding shift-by-zero edge cases (LSR #0, ASR #0, ROR #0 → RRX). These are architecture-subtle; do not assume #0 always means 'no shift'.
  • Mistaking the bit positions in the instruction encoding; draw the 32-bit layout every time in exam diagrams.
  • Writing PC (R15) as Rd and not considering that it changes flow (branch) and may load CPSR from SPSR if S is set in privileged state.

For your exam

Question types you will see:

  • Definitions: describe Operand2, I bit, S bit, and CPSR flags.
  • Short notes: explain immediate encoding (rotate) and list common opcodes.
  • Derivations: show how to compute imm32 from Imm8 and Rotate_imm; derive V formula for addition from bitwise carry behaviour.
  • Numericals: compute results and flags for given register values and instructions (carry, overflow, sign, zero).
  • Programs: small assembly snippets showing use of CMP/TST and conditional execution.
  • Diagrams: draw the 32-bit instruction format and label each field and its bit positions.

What a full-marks answer includes:

  • Correct instruction encoding diagram with bit indexes.
  • Exact rule for imm32 = ROR(Imm8, Rotate_imm * 2) and explanation of rotate_imm encoding.
  • Correct list of opcode mnemonics and which ones update flags implicitly (TST/CMP etc.).
  • Correct calculation of N, Z, C, V with reasoning (show intermediate binary/hex where needed).
  • Mention subtle cases: shift-by-zero semantics and PC-writing behaviour if relevant.

Quick check

  1. What field (bits) indicates whether Operand2 is an immediate or register-shift form?
  2. Write the general syntax for an add that sets flags.
  3. How is a 32-bit immediate formed when I = 1 in a data-processing instruction? Answers: 1) Bit 25 (I). 2) ADDS{<cond>} <Rd>, <Rn>, <Operand2> (S suffix present). 3) imm32 = ROR(Imm8, Rotate_imm * 2) where Imm8 is 8 bits and Rotate_imm encodes rotate/2.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?