Arm architecture
Arm registers
| Name | Alias | Purpose |
|---|---|---|
| R0 | – | General purpose |
| R1 | – | General purpose |
| R2 | – | General purpose |
| R3 | – | General purpose |
| R4 | – | General purpose |
| R5 | – | General purpose |
| R6 | – | General purpose |
| R7 | – | Holds Syscall Number |
| R8 | – | General purpose |
| R9 | – | General purpose |
| R10 | – | General purpose |
| R11 | FP | Frame Pointer |
Special Purpose Registers
| Name | Alias | Purpose |
|---|---|---|
| R12 | IP | Intra Procedural Call |
| R13 | SP | Stack Pointer |
| R14 | LR | Link Register |
| R15 | PC | Program Counter |
| CPSR | – | Current Program Status Register |
The CPSR contains the following ALU status flags:
N: Set when the result of the operation was Negative.Z: Set when the result of the operation was Zero.C: Set when the operation resulted in a Carry.V: Set when the operation caused overflow.
Note
PC points two instructions ahead of the current PC value, because older ARM processors always fetched two instructions ahead of the currently executed instructions
Common instructions
MOV- Move dataEOR- Bitwise XORMVN- Move and negateLDR- LoadADD- AdditionSTR- StoreSUB- SubtractionLDM- Load MultipleMUL- MultiplicationSTM- Store MultipleLSL- Logical Shift LeftPUSH- Push on StackLSR- Logical Shift RightPOP- Pop off StackASR- Arithmetic Shift RightB- BranchROR- Rotate RightBL- Branch with LinkCMP- Compare BXBranch- and eXchangeAND- Bitwise ANDBLX- Branch with Link and eXchangeORR- Bitwise ORSWI/SVC- System Call
Function prologue
push {r7 lr}saves two register to the stack:lr- return addressr7- frame pointer (equivalent ofebpin x86)
- They both restored at the end of the function with
pop {r7, pc}, restoring the saved return addresslrintopc
Arm vs Thumb ISA
The ARM Thumb Instruction set (or mode) spans from a compact 16-bit code-saving origins in the 1990s to the variable length standard used in moder micro-controllers.
ARM's documentation specifies that:
The ARMv7 architecture contains two main instruction sets, the ARM and Thumb instruction sets. Much of the functionality available is identical in the two instruction sets. The Thumb instruction set is a subset of the most commonly used 32-bit ARM instructions. Thumb instructions are each 16 bits long, and have a corresponding 32-bit ARM instruction that has the same effect. The main reason for using Thumb code is to reduce code density. Because of its improved density, Thumb code tends to cache better than the equivalent ARM code and can reduce the amount of memory required.
The T bit in the CPSR register (Current Program Status Register) indicates the mode:
- T bit = 1 -> Thumb mode
- T bit = 0 -> ARM mode
It’s bit 5 of the CPSR (the PSR.T flag).
Main differences
| Specificity | Thumb ISA | ARM ISA |
|---|---|---|
| Instruction Size | 16 bits* | 32 bits |
| T bit (CPSR) | 1 | 0 |
| Register access | Mostly R0–R7 | Full R0–R15 |
| Conditional exec | Only branches and IT blocks Thumb-2 | Almost every instruction |
| Code density | Higher (smaller code) | Lower (larger code) |
| Performance | Can be lower on 32-bit buses | Generally higher raw performance |
| Immediate/op range | More restricted | Wider |
| Entry instruction | BX/BLX to address with bit[0] = 1 |
BX/BLX to address with bit[0] = 0 |
| PC alignment | Halfword (2-byte) | Word (4-byte) |
Note
- Thumb-2 mixes 16-bit and 32-bit instructions, so not all Thumb instructions are 16 bits.
- This applies to 32-bit ARM (AArch32). In 64-bit mode (AArch64/A64), there is no Thumb instruction set, so there’s no
Tbit.