Explain ARM Registers in brief.
ARM registers are small, high-speed storage locations inside the CPU used to hold data, memory addresses, instruction addresses, processor status and intermediate calculation results.
1. ARM 32-bit registers
ARM Cortex-M and classic AArch32 processors have 16 visible core registers, R0–R15, each 32 bits wide.
| Register | Purpose |
|---|---|
| R0–R3 | Arguments, return values, temporary data |
| R4–R11 | General-purpose / callee-saved registers |
| R12 | Scratch register |
| R13 (SP) | Stack Pointer |
| R14 (LR) | Link Register |
| R15 (PC) | Program Counter |
| PSR | Processor status register |
2. General-purpose registers (R0–R12)
Used for arithmetic operations, function arguments, temporary data and memory addresses.
Consider the following C code:
int a = 10;
int b = 20;
int c = a + b;
Illustrative ARM assembly:
MOV R0, #10
MOV R1, #20
ADD R2, R0, R1
After execution:
| Register | Value |
|---|---|
| R0 | 10 |
| R1 | 20 |
| R2 | 30 |
ARM is a load-store architecture, meaning arithmetic instructions operate on registers rather than directly on memory.
LDR R0, [R1] ; Load data from memory
ADD R0, R0, #1 ; Increment data
STR R0, [R1] ; Store data back to memory
Here, R1 contains the memory address, while R0 contains the data.
3. Stack Pointer (R13 – SP)
Holds the address of the current top of the stack.
Used for local variables, saving registers, function calls and interrupt handling.
PUSH {R0, R1}
POP {R0, R1}
Address Before PUSH After PUSH
0x20001004 Empty Empty
0x20001000 <-- SP Empty
0x20000FFC Empty R1 = 20
0x20000FF8 Empty R0 = 10 <-- SP
Stack grows towards lower addresses.
For two 32-bit registers, PUSH decreases SP by 8 bytes, and POP increases SP by 8 bytes.
Cortex-M provides two stack pointers:
- MSP: Main Stack Pointer, used by default and in exception handlers.
- PSP: Process Stack Pointer, commonly used for RTOS tasks.
4. Link Register (R14 – LR)
Stores the return address when a function is called using the BL instruction.
BL my_function
my_function:
MOV R0, #10
BX LR
BL: Branch and Link, saves the return address in LR.BX LR: Returns to the calling function.
If a function calls another function, it generally needs to preserve its return address, often on the stack.
5. Program Counter (R15 – PC)
Holds the current instruction-related address and controls program execution.
B my_function
The branch instruction changes the PC, causing the CPU to execute instructions at the target address.
Due to architectural pipeline semantics, reading PC typically returns the current instruction address +8 in A32 state or +4 in Thumb state.
6. Program Status Register (PSR)
Stores processor status and arithmetic condition flags.
31 30 29 28 27–0
+----+----+----+----+-------------------+
| N | Z | C | V | Other status bits |
+----+----+----+----+-------------------+
- N – Negative
- Z – Zero
- C – Carry
- V – Signed overflow
Example:
MOV R0, #10
MOV R1, #10
CMP R0, R1
BEQ equal
CMP internally performs subtraction without storing the result.
Since 10 − 10 = 0, the CPU sets the Z flag to 1, and BEQ takes the branch.
Classic AArch32 processors use CPSR and, in applicable exception modes, SPSR. Cortex-M uses xPSR, which combines APSR, IPSR and EPSR.
7. Cortex-M special registers
| Register | Purpose |
|---|---|
| MSP | Main stack pointer |
| PSP | Process stack pointer |
| PRIMASK | Masks configurable-priority exceptions |
| BASEPRI | Masks interrupts based on priority threshold |
| FAULTMASK | Masks all exceptions except NMI, during normal execution |
| CONTROL | Controls thread privilege and stack selection |
| APSR | Arithmetic status flags |
| IPSR | Current exception number |
| EPSR | Execution state information |
Some special registers are not available on every Cortex-M processor.
CPSID i ; Set PRIMASK
CPSIE i ; Clear PRIMASK
MRS R0, CONTROL ; Read CONTROL register
MSR CONTROL, R0 ; Write CONTROL register
8. ARM 64-bit registers (AArch64)
AArch64 is commonly used in embedded Linux, Android and modern Cortex-A processors.
| Register | Purpose |
|---|---|
| X0–X30 | 31 general-purpose 64-bit registers |
| W0–W30 | Lower 32-bit views of X registers |
| X0–X7 | Function arguments |
| X29 | Frame pointer (FP) |
| X30 | Link register (LR) |
| SP | Stack pointer |
| PC | Program counter |
| PSTATE | Processor state |
| XZR/WZR | Zero register |
| V0–V31 | 128-bit SIMD/Floating-point registers |
| ELR_ELx | Exception return address |
| SPSR_ELx | Saved processor state |
| TTBR0_EL1 | Translation table base register |
| SCTLR_EL1 | System control register |
Unlike AArch32, the AArch64 PC is not a general-purpose register.
Writing to a W register automatically clears the upper 32 bits of its corresponding X register.
9. What happens to registers during a Cortex-M interrupt?
On normal exception entry, hardware automatically pushes the following basic stack frame:
Higher memory address
+------------------+
| xPSR |
| PC |
| LR |
| R12 |
| R3 |
| R2 |
| R1 |
| R0 | <-- SP points to R0
+------------------+
Lower memory address
32-byte basic stack frame
The CPU loads the ISR address into PC from the vector table and places an EXC_RETURN value in LR. On exception return, hardware restores the saved registers.
Additional stacking may occur for floating-point context and alignment.
Interview takeaway: R0–R12 handle data and calculations, SP manages the stack, LR preserves return information, PC controls instruction execution, and PSR holds processor status and condition flags.