6. Thumb Mode & Thumb-2
ARM processors support multiple instruction sets, including the ARM instruction set (a fixed 32-bit instruction set) and Thumb instruction sets (which primarily use 16-bit instructions but can also include 32-bit instructions in Thumb-2). The Thumb instruction sets provide a more compact representation of ARM instructions, improving code density while maintaining performance.
Thumb Mode: 16-bit vs. 32-bit Instructions
Thumb mode was introduced to reduce the memory footprint of applications by using 16-bit instructions instead of the traditional 32-bit ARM instructions. Thumb mode provides a subset of ARM instructions but offers increased efficiency, especially in memory-constrained environments (e.g., embedded systems and mobile devices).
| Feature | ARM Mode (A32) | Thumb Mode (T16) | Thumb-2 (T32) |
|---|---|---|---|
| Instruction Size | 32-bit | 16-bit | Mixed 16-bit & 32-bit |
| Code Density | Lower | Higher (Better for memory-limited systems) | Higher |
| Instruction Set | Full ARM instruction set | Limited subset of ARM | Almost full ARM instruction set |
| Performance | High | Generally lower but efficient | High |
| Use Case | Performance-critical applications | Code size reduction | Balance of performance and size |
Switching Between ARM and Thumb Mode (BX, BLX)
Since ARM and Thumb use different instruction encodings, special instructions (BX and BLX) are used to switch between them dynamically.
1. BX (Branch and Exchange) Instruction
- Used to switch between ARM and Thumb states.
- If the least significant bit (LSB) of the target address is 1, execution switches to Thumb mode.
- If the LSB is 0, execution remains in ARM mode.
Example: Switching to Thumb Mode
1
2
MOV R0, #1 // Load value 1 into R0
BX R0 // Switch to Thumb mode (because LSB is 1)
2. BLX (Branch with Link and Exchange) Instruction
- Similar to
BXbut also stores the return address (used for function calls). - Allows calling a function written in a different mode (ARM ↔ Thumb).
Example: Calling a Thumb Function from ARM
1
BLX Function_Thumb // Branch to Thumb function and switch mode
Thumb-2 Extensions (T32 Instruction Set)
Thumb-2 was introduced in ARMv6T2 and later architectures to bridge the gap between ARM and Thumb. It extends the 16-bit Thumb instruction set by allowing a mix of 16-bit and 32-bit instructions, making it more powerful while still maintaining high code density.
Key Features of Thumb-2
- Uses both 16-bit and 32-bit instructions for better performance.
- Expands the available instruction set, making it almost equivalent to full ARM.
- Maintains backward compatibility with Thumb (T16).
Example of Thumb-2 Code
1
2
MOVS R0, #5 // 16-bit instruction
ADD R1, R0, R2 // 32-bit instruction in Thumb-2
Example: Mixing ARM and Thumb Code
ARM Code
1
2
3
4
.global main
main:
BLX thumb_function ; Call a Thumb function
B main ; Loop
Thumb Code
1
2
3
4
5
6
.thumb
.thumb_func
.global thumb_function
thumb_function:
MOV R0, #42
BX LR ; Return to ARM mode