ARM core: dataflow, registers and CPSR
Notes on ARM core dataflow: how instructions move data through registers, the register set and banked registers, and the Current Program Status Register (CPSR) with its flags and role in conditional execution and mode changes.
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
Two to three sentences: The ARM core's dataflow and register set are central to how an embedded program moves and transforms data at the machine level. Understanding the register file, how the Program Counter (PC) is updated, and how the Current Program Status Register (CPSR) controls conditionals and modes is essential for correctly writing, debugging and optimizing firmware, and for answering exam questions on ARM pipeline and exceptions.
Key ideas
Start from first principles and build up.
Processor register (register file): a small, fast storage inside the CPU used to hold operands and results. On classic 32-bit ARM cores these are 32-bit general-purpose registers named r0–r15 (but see details below). Registers are faster than memory and are the primary operands for most ARM instructions.
General-purpose registers: In ARM state, r0–r12 are general-purpose. r13 is typically the Stack Pointer (SP), r14 is the Link Register (LR), and r15 is the Program Counter (PC). Define each when first used.
Program Counter (PC): the register holding the address of the instruction being executed or about to be executed. In ARM state the architectural PC (when read as a register) commonly reads as the address of the current instruction plus an offset because of the pipeline — this gives programmers the ability to read a value that is some instructions ahead. (See worked examples & verify below for exact offset behaviour.)
Link Register (LR): used to store the return address on function calls (e.g., BL sets LR = return address). This avoids always using the stack for returns.
Stack Pointer (SP): points to top of stack (conventionally). Some instructions use SP directly; pushing/popping and exception handling use SP critically.
Banked registers: To make context switching on exceptions fast, some modes have their own copies (banks) of registers. For example, FIQ mode has banked r8–r14; other exception modes have banked r13 and r14. Banked registers mean the CPU switches to a different physical register when mode changes; the same register name can therefore refer to different physical storage depending on the CPU mode.
Current Program Status Register (CPSR): a 32-bit control and status register that holds condition flags and mode/interrupt status. Important fields are the four condition flags N (Negative), Z (Zero), C (Carry) and V (Overflow); the interrupt-disable bits I and F; and the T bit (Thumb state indicator). CPSR also encodes the processor mode (User, FIQ, IRQ, Supervisor, Abort, Undefined, System) in low-order bits. Bit positions to quote in exams: N=31, Z=30, C=29, V=28, Q=27; I=7, F=6, T=5; mode field in bits [4:0].
SPSR (Saved Program Status Register): Exception modes have a Saved Program Status Register (SPSR) that stores the CPSR value when an exception is taken. This allows restoring status on return from exception. Exception modes include FIQ, IRQ, Supervisor (SVC), Abort (ABT), and Undefined (UND); User and System modes do not have an SPSR.
Condition codes and conditional execution: ARM data-processing and branch instructions have a 4-bit condition field. An instruction executes only if the condition evaluates true when compared against CPSR flags. This enables compact conditional code without branching.
Dataflow through ARM core: Typical instruction flow: fetch -> decode -> read registers -> execute (ALU/memory) -> writeback -> next PC. The register file is read during decode/read registers stage and written during writeback. The PC update depends on whether a branch was taken, whether the instruction writes PC, and the pipeline behaviour.
Assumptions behind common exam derivations: assume a 3-stage pipeline (Fetch, Decode, Execute) for classic ARM when asked to explain PC offset; assume 32-bit ARM state registers; assume condition field present unless instruction specifically ignores it (e.g., some system or unconditional variants). When asked to draw a diagram, draw the register file as a rectangular block with labelled r0–r15 and CPSR to the side; draw pipeline stages as sequential boxes with arrows.
What to draw for diagrams (exactly):
- Register map: draw a rectangle divided into 16 horizontal cells; label from top r0 down to r15. To the right draw a smaller rectangle and label CPSR and below it SPSR (note: SPSR exists only in exception modes). Mark r13 as SP, r14 as LR, r15 as PC with arrows pointing right to show PC flows to fetch stage.
- Pipeline diagram: draw three boxes left-to-right labeled Fetch, Decode/Register Read, Execute/Writeback. Draw arrow from PC to Fetch. Below the Decode box, draw arrows to/from the register map to show register reads and writes.
Formulas
Each formula on its own line with symbol meanings and when it applies.
width_register = 32 bits
- width_register: bit-width of general-purpose registers in classic 32-bit ARM cores. Applies to ARMv4–ARMv7 in ARM (AArch32) state.
CPSR[31] = N, CPSR[30] = Z, CPSR[29] = C, CPSR[28] = V
- N, Z, C, V: names of the top four condition flags in CPSR; index shows bit position (31 is MSB). Apply when interpreting CPSR.
Instruction[31:28] = condition field
- Instruction[31:28]: bits 31 down to 28 in a standard 32-bit ARM instruction encoding hold the condition code that controls conditional execution.
PC_read_value = address_of_current_instruction + offset (offset depends on pipeline and state)
- PC_read_value: value observed when reading r15 as data. The offset is +8 in ARM state for a 3-stage pipeline; in Thumb state it is +4.
S flag set by instruction (format): DataProcessing opcode S bit = 1 => update CPSR flags
- Applies to data-processing instructions that include the S bit. If S=1, result affects N/Z/C/V in CPSR.
Worked examples
Example 1 — simple ADD that sets flags (numerical, step-by-step)
Given: r0 = 0x7FFFFFFF, r1 = 0x00000001. Instruction: ADDS r2, r0, r1 (ADD, set flags).
- Formula: result = r0 + r1 = 0x7FFFFFFF + 0x00000001
- Calculate: 0x7FFFFFFF + 0x00000001 = 0x80000000
- Represent in signed 32-bit: 0x7FFFFFFF = +2147483647, 0x00000001 = +1, sum = -2147483648 (overflow occurred)
- Flags:
- N = 1 (result MSB is 1)
- Z = 0 (result is not zero)
- C = 0 or 1? For unsigned carry: there is no carry out of bit 31 here (0x7FFFFFFF + 1 = 0x80000000) — no carry beyond bit31, so C = 0. (Carry is set when unsigned addition wraps past 0xFFFFFFFF.)
- V = 1 (signed overflow occurred: operands both positive, result negative) Answer: r2 = 0x80000000, CPSR flags N=1, Z=0, C=0, V=1. This is the expected exam-style answer in bold: r2 = 0x80000000; N=1 Z=0 C=0 V=1.
Example 2 — loop decrement and conditional branch (algorithm trace) Start: r3 = 3. Code (ARM pseudo): 1: SUBS r3, r3, #1 ; decrement, set flags 2: BNE 1b ; branch to label 1 if not equal (Z==0) Trace:
- Iteration 1: r3=3 -> after SUBS r3=2, Z=0 -> BNE taken -> loop
- Iteration 2: r3=2 -> after SUBS r3=1, Z=0 -> BNE taken
- Iteration 3: r3=1 -> after SUBS r3=0, Z=1 -> BNE not taken -> exit Final state: r3 = 0, CPSR Z=1. Show each step if asked in exam: list register values and whether branch executed.
Example 3 — BL (branch and link) and LR/PC behaviour
Instruction: BL function at address 0x1000 (ARM state). Assuming classic pipeline where PC when executing BL is PC = address_of_BL + 8 when read:
- Return address expected by BL is the address of the next instruction. In ARM state, LR is set to address_of_BL + 4, so here LR = 0x1004. BL then loads PC with the branch target (function).
- On return:
MOV PC, LRreturns to the saved address (0x1004 in this example). Note: In Thumb state, LR is set to the address of the next instruction with bit[0]=1 (to ensure return to Thumb state), and BX LR or POP {PC} will branch back to Thumb; in ARM state LR has bit[0]=0. If asked, draw arrows: PC->Fetch, BL sets LR <- (next instruction address) and PC <- target.
Common mistakes
- Forgetting the S bit: using an ADD that does not set flags (ADD) versus ADDS (sets flags). Exams often expect you to state whether CPSR is updated.
- Not accounting for banked registers on mode switches (especially FIQ). Students write code assuming a single physical register for r13/r14 across modes.
- Misreading instruction encoding fields (condition field position) — state exact bit ranges when asked.
For your exam
Kinds of questions produced:
- Definitions: Define CPSR, SPSR, PC, LR, SP, banked registers.
- Short notes: Explain conditional execution using CPSR flags; explain why banked registers help exception handling.
- Derivations: Derive which flags are set by an ADD/SUB operation; show bitwise calculation for C and V.
- Numericals: Given register values, simulate a few instructions (include S variants) and give final registers and CPSR.
- Programs/trace: Trace a small loop using SUBS and BNE and show iterations and final state.
- Diagrams: Draw the register file or pipeline and label how PC flows and when CPSR is read/written.
What a full-marks answer includes:
- Precise definitions with bit positions where relevant (e.g., flag bit numbers).
- A correct, step-by-step simulation of register changes and flag computations with intermediate hex or decimal values.
- Correct identification of banked registers and which modes use SPSR (state which modes and why).
- Correct diagram labelling PC, LR, SP, CPSR, and pipeline stages when asked.
Quick check
- Which CPSR flag is set when an arithmetic operation result is zero?
- What register is conventionally used as the Link Register?
- If an instruction has condition field 0000 (EQ), when will it execute? Answers: Z flag; r14 (LR); when Z == 1 (i.e., when last result was zero).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?