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.

PSR instructions, constants and conditional execution

How ARM uses the Program Status Registers (PSRs), how immediate constants are encoded and produced, and how conditional execution works (ARM state and brief Thumb note) — all essential for writing and understanding correct assembly for embedded systems.

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 programs often need to read or change processor state (modes, interrupt enable, condition flags) and to load constants efficiently into registers. The Program Status Registers (PSRs) hold the condition flags and control bits used by exceptions, interrupts and conditional execution. Understanding PSR instructions, the ARM immediate-constant encodings, and conditional execution lets you write compact, fast and correct firmware for ARM-based embedded systems.

Key ideas

Define the important CPSR fields (used later):

  • N (Negative) flag — set to 1 if the result of an arithmetic/logical operation is negative (bit 31 of result). (CPSR bit 31)

  • Z (Zero) flag — set to 1 if the result is zero. (CPSR bit 30)

  • C (Carry) — for addition, set to 1 on carry-out; for subtraction, set to 1 if no borrow (clear to 0 if a borrow occurred); for logical shifts, set to the last bit shifted out when the instruction updates flags. (CPSR bit 29)

  • V (Overflow) — set when signed overflow occurs. (CPSR bit 28)

  • I, F, T bits — interrupt disable bits I and F, and T (Thumb state) are in CPSR (I=bit 7, F=bit 6, T=bit 5). Also commonly examined in A32: A (Abort mask, bit 8), E (Endianness, bit 9), Q (Saturation flag, bit 27), J (Jazelle, bit 24), GE[3:0] (bits 19–16), and IT state bits (bits 26–25 and 15–10).

  • SPSR (Saved PSR) — exists only in privileged modes; on exception entry the previous CPSR is copied to SPSR of the new mode. SPSR is used to restore state on return from exception.

  • PSR instructions — ARM provides instructions to read and write PSRs:

    • MRS (Move PSR to Register): reads CPSR or SPSR into a general-purpose register. Example: MRS R0, CPSR — after this R0 contains CPSR value.
    • MSR (Move Register/Immediate to PSR): writes to CPSR or SPSR. MSR can write full PSR or selected fields (flags, control) depending on a mask. Example: MSR CPSR_f, R1 — writes only the flags field from R1 into CPSR (affects N,Z,C,V and Q where implemented). In Thumb-2, use MSR APSR_nzcvq, R1; CPSR/SPSR forms are ARM-state only. These allow controlled changes to flags and mode. Use MSR only from privileged code when you change control fields.
  • Condition flags and conditional execution — In ARM (ARM instruction set, not Thumb), almost every data-processing or memory instruction has a 4-bit condition code field. The instruction executes only if the condition, evaluated against CPSR flags, is true; otherwise the instruction is treated as a no-op (but still consumes one pipeline slot). Typical conditions: EQ (equal, Z=1), NE (not equal, Z=0), CS/HS (carry set / unsigned higher or same, C=1), CC/LO (carry clear), MI (negative, N=1), PL (positive, N=0), VS (overflow set), VC (overflow clear), HI (unsigned higher, C=1 and Z=0), LS (unsigned lower or same), GE (signed greater/equal, N==V), LT (signed less, N!=V), GT (signed greater, Z=0 and N==V), LE (signed less or equal, Z=1 or N!=V), AL (always — executes unconditionally). Using conditional execution avoids branches and can reduce pipeline flushes.

  • Immediate constants in ARM data-processing instructions (rotated immediate) — The ARM data-processing immediate field encodes a 32-bit immediate using 12 bits in the instruction (commonly called imm12). The encoding concept: an 8-bit immediate value (imm8) and a 4-bit rotate value (rotate). The actual 32-bit immediate is produced by rotating imm8 right by an even number of bits (0, 2, 4, ..., 30): imm32 = ROR(imm8, 2 * rotate). This compact encoding lets many useful constants fit in a single instruction. If a constant cannot be encoded, programmers use other tricks (MOVW/MOVT, MVN, literal loads, or multiple instructions).

    Important helper encodings and pseudo-instructions:

    • MVN (move NOT) returns bitwise NOT of imm; sometimes MVN with a rotated imm8 produces constants that MOV cannot.
    • MOVW and MOVT (Thumb-2 / ARM assemblers provide these) load the lower 16 bits and the top 16 bits of a register respectively. Combined they can produce any 32-bit constant in two instructions.
    • Literal pool LDR: assembler can place constant in memory near code and produce LDR Rd, =const which loads from that literal; this costs memory access but is simple.
  • Thumb conditional execution (brief): In Thumb-2, the IT (If-Then) instruction allows conditional execution of up to four following instructions. Its syntax is IT{mask} cond, where mask is 0–3 letters T/E; it predicates up to four following instructions, with T meaning cond and E meaning the inverse. An IT block cannot contain branches that change control flow, and only certain instructions are allowed.

Diagram to draw for exams (describe exactly):

  • Draw a 32-bit word labelled CPSR bits 31..0. Mark bits 31(N),30(Z),29(C),28(V); mark bits 7(I),6(F),5(T); mark bits 4..0 labelled MODE. Draw an arrow from an arithmetic instruction to CPSR flags showing how result -> sets N and Z and may set C,V depending on operation. Label that SPSR exists per privileged mode and is copied from CPSR on exception entry.

Formulas

(For this programming topic, list syntax to remember) MRS <Rd>, CPSR — read CPSR into register Rd MRS <Rd>, SPSR — read SPSR (valid only in privileged mode) MSR CPSR_<field>, <Rm or #imm> — write selected CPSR fields; field can be any combination of c (Control), x (eXtension), s (Status), f (Flags). Writing control fields is privileged. <instr>{cond}{S} <Rd>, <Rn>, <operand2> — general data-processing instruction form; {cond} is the condition suffix (EQ,NE,...), S means update flags imm32 = ROR(imm8, 2 * rotate) — how ARM imm12 encodes a 32-bit immediate (rotate = 0..15) MOVW Rd, #imm16 and MOVT Rd, #imm16 — set low/high 16 bits of Rd (use together to form any 32-bit constant) LDR Rd, =const — assembler pseudo-instruction to load a constant from literal pool MVN{cond}{S} Rd, Operand2 — move bitwise NOT of operand2 into Rd IT{<mask>} <cond> — Thumb-2 If-Then; mask is 0–3 letters T/E selecting up to 4 predicated instructions: the first uses <cond>, each 'T' uses <cond>, each 'E' uses the inverse; branches and some instructions are not permitted in an IT block

Worked examples

  1. Encode an immediate constant in one instruction
  • Problem: Can 0x00FF0000 be encoded as ARM data-processing immediate? If yes, specify imm8 and rotate and show MOV.
  • Solution steps:
    1. Note encoding imm32 = ROR(imm8, 2*rotate).
    2. Try imm8 = 0xFF (00000000 00000000 00000000 11111111). Rotating right by 16 bits (2*rotate where rotate=8) gives 0x00FF0000.
    3. Therefore imm8=0xFF and rotate=8 is valid. The assembler accepts: MOV R0, #0x00FF0000 (in ARM state) and it will encode that constant in imm12. Final: R0 = 0x00FF0000 after MOV.
  1. Load any 32-bit constant in two instructions with MOVW/MOVT
  • Problem: Load 0x12345678 into R0.
  • Steps:
    1. Use MOVW to set low 16 bits: MOVW R0, #0x5678 -> R0 = 0x00005678.
    2. Use MOVT to set high 16 bits: MOVT R0, #0x1234 -> R0 = 0x12345678. Final: R0 = 0x12345678.
  1. Conditional execution and flags
  • Problem: Set R0 = 1 if R1 == 0, else R0 = 0, without branch.
  • Steps:
    1. CMP R1, #0 — sets CPSR flags: Z set if R1==0.
    2. MOVEQ R0, #1 — executes only if Z==1, sets R0=1.
    3. MOVNE R0, #0 — executes only if Z==0, sets R0=0. Final: R0 contains 1 when R1==0, otherwise 0. This uses conditional execution instead of branching.
  1. Read and write flags example (safe sequence)
  • Problem: Save flags, change them, then restore.
  • Steps:
    1. MRS R2, CPSR — R2 now contains CPSR.
    2. Modify R2 as needed (e.g., clear or set NZCV flags) with logical ops. Do not change T with MSR; use BX/BLX to switch state.
    3. MSR CPSR, R2 — write full CPSR back (privileged required to change control bits). Final: CPSR restored/modified per R2.

Common mistakes

  • Confusing MOV with MOVW/MOVT availability — MOVW/MOVT require ARMv6T2 or later (A32/T32). Older cores/assemblers (e.g., ARMv4/v5 without T2) do not support them.
  • Forgetting that conditional instructions still occupy pipeline slots — overuse does not always save cycles compared to a short branch.
  • Mistaken bit positions for CPSR extras (Q, J, E) — architecture versions differ. Verify in your ARM ARM (reference manual).
  • Incorrectly assuming IT in Thumb applies to unlimited instructions — IT covers at most four following instructions and uses a mask; misuse will assemble or run incorrectly.

For your exam

Kinds of questions you will see

  • Definitions: define CPSR, SPSR, N/Z/C/V and T/I/F bits. A full answer lists bit meanings and high-level purpose (exception state, conditional execution, interrupt masks).
  • Short notes: explain immediate constant encoding in ARM and give examples. A full answer states the imm8+rotate encoding and an example of a constant that is and is not encodable.
  • Derivations/explanations: show why conditional execution can avoid branches and a pipeline diagram (draw instruction stream with NOP when condition false). Include the exact condition tests (e.g., GE is N==V).
  • Programs/assembly: load a 32-bit constant into a register (expect MOVW/MOVT or literal LDR solution) or implement conditional moves without branching. Full marks require correct syntax and a short explanation about flags and encoding.
  • Diagrams: draw CPSR bit map with N,Z,C,V, I,F,T and mode bits and explain how SPSR is used at exception entry.

Quick check

  1. What does the Z flag indicate?
  2. How is a 32-bit immediate encoded in an ARM data-processing instruction?
  3. Which instruction reads CPSR into a register? Answers: Z indicates result zero; imm32 = ROR(imm8, 2*rotate) (imm8 + 4-bit rotate); MRS <Rd>, CPSR

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

Stuck on something here?