Skip to content
systemcoding.dev
Assembly

ARM Cortex-M Registers: R0-R12, SP, LR, PC and xPSR

A map of every core register on an ARM Cortex-M: R0 to R12, the two stack pointers, LR, PC, xPSR and the special registers, with what each one is for.

6 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Arm, Intel and RISC-V logos.

Every assembly instruction on a Cortex-M works on registers, the handful of 32-bit storage slots inside the processor itself. Memory is slow by comparison and has to be reached through a load or a store; a register can be read and written by the instruction that is executing right now. Learn the register set first and the instruction set stops looking like noise. This post covers the programmer's model that Cortex-M0, M3, M4, M7 and M33 share, as defined in the Armv6-M and Armv7-M architecture reference manuals, and shows how to read the status register from a running program.

The sixteen core registers

The core has sixteen registers visible to ordinary code, named r0 to r15. Thirteen of them are general purpose, and three have fixed jobs.

NameAliasRole
r0 to r7low registersGeneral purpose. Every 16-bit Thumb instruction can use them
r8 to r12high registersGeneral purpose. Only a few 16-bit instructions reach them; 32-bit forms reach all
r13spStack pointer
r14lrLink register, holds a return address
r15pcProgram counter

The split between low and high matters because of instruction size. A 16-bit encoding has only three bits for a register number, so adds r0, r1, r2 fits in 16 bits and adds r8, r9, r10 does not. On a Cortex-M0, which has only the 16-bit core, you keep hot values in r0 to r7 and use the high registers as spill space.

The stack pointer is really two

r13 is a name for one of two physical registers. MSP, the main stack pointer, is used after reset and in every exception handler. PSP, the process stack pointer, exists so an RTOS can give each task its own stack while interrupts keep running on the main one. Which of the two sp means is chosen by bit 1 of the CONTROL special register: clear selects MSP, set selects PSP. The bit can only be changed in privileged mode, and it is forced back to MSP on exception entry. The two low bits of any stack pointer read as zero, because the stack must stay word aligned.

Programs that never touch CONTROL run entirely on MSP, which is why a bare-metal "blinky" never mentions PSP.

LR and PC

lr receives the return address when you call a function with bl. A leaf function can end with bx lr; a function that calls others has to push lr first and pop it back into pc on the way out. On exception entry the core loads lr with a special value called EXC_RETURN (for example 0xFFFFFFF9), and branching to that value is how a handler returns.

pc reads as the address of the current instruction plus four, because of the pipeline. Writing to it is a branch. The lowest bit of any address you place in pc through bx, blx or pop {pc} must be set on a Cortex-M, since the core only executes Thumb code, and a clear bit raises a fault. The row on that error in this series covers it in detail.

xPSR: three registers sharing one word

xPSR is a single 32-bit value that is really three overlapping views:

ViewContains
APSRCondition flags N, Z, C, V, and on M4 and later the sticky Q flag and GE bits
IPSRThe number of the exception being handled, zero in thread mode
EPSRThe Thumb state bit T (bit 24), plus IT-block and interrupt-continuable instruction state

IPSR is the one worth remembering in practice. A value of 0 means your code is running in thread mode; 3 means the HardFault handler, 11 is SVCall, 14 is PendSV and 15 is SysTick. External interrupt n shows up as 16 + n. Fault handlers and logging code read it to learn where they are.

Special registers

Three more registers live outside the numbered set and are reached only with mrs (read) and msr (write):

  • PRIMASK: bit 0 set blocks every maskable exception, which cpsid i does for you.

  • BASEPRI (Armv7-M and later): blocks exceptions at or below a priority number, useful for protecting data shared with only the lower-priority handlers.

  • CONTROL: bit 0 selects unprivileged thread mode, bit 1 selects PSP as above.

Armv6-M cores such as the Cortex-M0 have PRIMASK and CONTROL but no BASEPRI and no FAULTMASK.

Reading the registers from code

The function below returns the exception number the caller is running in. It uses mrs to read ipsr, masks the field, and returns it in r0 per the calling convention.

ipsr.Sasm
        .syntax unified
        .cpu    cortex-m4
        .thumb
        .text
        .global current_exception
        .type   current_exception, %function
        .thumb_func
current_exception:
        mrs     r0, ipsr        // read the IPSR view of xPSR
        bx      lr              // result is in r0

Call it from main and from the SysTick_Handler, with a C declaration such as unsigned current_exception(void);.

main.cc
volatile unsigned in_main, in_systick;

void SysTick_Handler(void)
{
    in_systick = current_exception();
}

int main(void)
{
    in_main = current_exception();
    for (;;) { }
}

Reading the two variables with a debugger after the first tick shows:

Text
in_main    = 0
in_systick = 15

0 is thread mode, 15 is the SysTick exception. The mrs r0, ipsr instruction is a 32-bit Thumb-2 encoding, so it works on the Cortex-M0 too: Armv6-M includes mrs and msr among its few 32-bit instructions.

Reading the flags

The APSR flags change when an instruction ends in s, or when a compare instruction runs. This fragment compares two values and reads the result back:

flags.Sasm
        .syntax unified
        .thumb
        movs    r0, #5
        movs    r1, #5
        cmp     r0, r1          // computes r0 - r1 and discards the result
        mrs     r2, apsr        // copy the flags into r2

After cmp of two equal numbers, Z is 1 (bit 30), C is 1 (no borrow, bit 29), N is 0 and V is 0. So r2 holds 0x60000000. On Armv7-M and Armv8-M the mrs source named apsr returns the flag bits; on Armv6-M the source is also named apsr, and you may see xpsr used for a read of the combined value.

Text
r2 = 0x60000000

What you cannot assume

Do not treat r12 as free across a function call. The procedure call standard (AAPCS) lets a callee destroy r0 to r3 and r12, and requires it to preserve r4 to r11. Reserved roles for r9 (a static base or a platform register) depend on the toolchain and the ABI variant. The next post in the series, on AAPCS, goes through that contract.

A practical habit: name your registers

Raw r4 and r5 make a routine hard to read after a week. GNU as supports .req, which gives a register an alias for the rest of the file:

asm
        .syntax unified
        count   .req r4
        ptr     .req r5
        movs    count, #8

The alias costs nothing at run time and shows up in the source only. Keep the list of aliases at the top of the function and .unreq them at the end so a name from one routine never leaks into the next. It also documents which registers you committed to preserving, which is the first thing a reviewer looks for.

Where beginners lose a day

Two mistakes come up again and again. The first is reading sp after a push and expecting the old value: push decrements first, so sp already points at the pushed data. The second is writing to pc with a plain mov pc, r0 for a function pointer. It works on some assemblers and cores and is a fault on others when the low bit is clear, so use bx r0, which states the intent and keeps the Thumb bit contract visible. When a register holds a surprise value in the debugger, check these two before suspecting the hardware.

Also inEmbedded
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.