ARM Registers, Banked Access, and Program Status Word
What You'll Learn
- 1Name each core register and its calling convention role
- 2Explain how MSP and PSP are banked and switched
- 3Decode APSR, IPSR, and EPSR fields
- 4Trace register changes through a function call
The Cortex-M programmer model is deliberately small, which is why it is easy to learn and hard to outgrow. Thirteen general purpose registers plus stack pointer, link register, and program counter carry almost all computation. Several registers are banked, meaning the hardware swaps in a different copy depending on privilege, exception state, or processor mode. The program status word packs condition flags, interrupt masks, and the current exception number into one status register that you read after almost every fault.
General Registers and the Calling Convention
Registers R0 through R3 pass arguments and return values, so a caller must assume they are destroyed across a call. Registers R4 through R11 hold local values and are preserved, which means a callee that uses them pushes them first. R12 is a temporary scratch register used by the linker for long address materialization. This division is the ARM architecture procedure call standard and every compiler honors it.
A leaf function can often run entirely in registers, while a deeper call chain spills to the stack. Watch the compiler map or the disassembly when you profile, because stack traffic and register spills usually explain more slowdown than the arithmetic itself.
Banked Stack Pointer and Link Register
The stack pointer has two physical copies. The main stack pointer serves the reset handler, interrupts, and privileged code, while the process stack pointer serves unprivileged task code. The CONTROL register selects which copy is active, and exception entry always switches back to the main stack pointer. Reading R13 returns whichever copy is currently selected.
The link register holds the return address written by a branch with link instruction. On exception entry the hardware instead stores an exception return value with special pattern bits, and the handler later returns through it to trigger exception unwinding. Real functions push the link register on entry, which is why nested calls work at all.
Program Status Word Fields
The program status word is presented as three views. APSR holds the condition flags: zero, carry, negative, overflow, and the sticky saturate flag used by DSP code. IPSR holds the current exception number, where zero means thread mode. EPSR holds the Thumb state bit and the if then execution state bits that make conditional Thumb-2 instructions possible.
Reading special registers uses dedicated instructions rather than normal loads, and writing them goes through mask register instructions that let you set or clear individual bits atomically. After a fault, the IPSR value plus the stacked registers tell you which exception was active and what the code was doing.
Core Register Roles
| Register | Role | Convention |
|---|---|---|
| R0 to R3 | Arguments and return values | Caller saved |
| R4 to R11 | Long lived locals | Callee saved |
| R12 | Scratch for the linker | Temporary only |
| R13 | Stack pointer | MSP or PSP selected |
| R14 | Link register | Return address or exception return |
| xPSR | Program status word | Flags, Thumb state, exception number |
Real-World Examples
- Trace a function call â The caller places the first argument in R0, executes a branch with link, and expects the result back in R0 after return.
- Read the condition flags â A compare instruction sets APSR flags, and the following conditional branch consumes them without an extra move.
- Switch to the process stack â Set the CONTROL stack select bit so task code uses PSP while interrupts still use MSP.
- Inspect a fault â Read IPSR for the exception number, then read the stacked PC and xPSR to see the exact faulting instruction.
â ī¸Never Write R13 From C
Common Mistakes
Treating R0 to R11 as interchangeable
Fix: Only R0 to R3 are scratch across calls, so assuming otherwise corrupts live values.
Forgetting the exception return value pattern
Fix: Returning from an exception uses a special bit pattern, not a plain address, or the return will fault.
Ignoring the Thumb state bit
Fix: Clearing the Thumb bit in EPSR causes an undefined instruction fault on the next instruction fetch.
Key Takeaways
- The calling convention splits caller saved and callee saved registers.
- MSP and PSP are banked copies selected by CONTROL and exception state.
- APSR flags come from arithmetic, IPSR holds the active exception number.
- Special register access needs dedicated instructions, not normal loads.
Frequently Asked Questions
How many registers does a Cortex-M have?
Architecturally sixteen in the low register view with R13 to R15 special, plus banked copies of the stack pointer and link register contexts.
Why does the link register contain a pattern on exception entry?
The pattern encodes exception return information such as which stack was used and whether floating point state is present.
Can you run with interrupts disabled by writing a register?
Yes, using the mask register instructions on the priority mask register, which is how critical sections are built.
Interview Questions
Q1: Explain MSP versus PSP in two sentences.
MSP is used by handlers and by privileged thread code at reset, while PSP is intended for unprivileged task code, and the CONTROL register chooses between them in thread mode.
Q2: What happens to R0 through R3 across a function call?
They are caller saved, so the compiler treats them as clobbered; any value that must survive a call moves to R4 or higher or to the stack.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions