Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
The T-Bit and Thumb State
Why bit 24 of EPSR must always be 1 on Cortex-M4, what happens when a function pointer points to an even address, and why ARM-mode code cannot run on Cortex-M processors.
1. Overview — What This Example Does
The 006_t-bit project deliberately causes a UsageFault (INVSTATE) by calling a function pointer whose address has LSB=0 (even address). This clears the T-bit in EPSR, signalling to the processor that it should execute ARM (32-bit) instructions — which Cortex-M4 does not support. The processor generates a UsageFault immediately.
2. Source Code — Annotated
#include <stdio.h>
#include <stdint.h>
void generate_interrupt() { /* same as examples 3/5 */ }
void change_access_level_unpriv(void) { /* same as example 5 */ }
int main(void)
{
printf("In thread mode : before interrupt\n");
/* Declare a function pointer */
void (*fun_ptr)(void);
/* Store address 0x080001e8 in fun_ptr.
* IMPORTANT: 0x080001e8 is an EVEN address (LSB = 0).
* This means bit0 = 0 → T-bit will become 0 when this is
* branched to via BX or BLX. */
fun_ptr = (void*)0x080001e8;
/* When fun_ptr() is called:
* 1. The compiler generates: BLX R0 (or similar)
* 2. The processor copies 0x080001e8 into PC
* 3. PC bit0 (=0) gets copied to EPSR.T-bit
* 4. T-bit becomes 0 → "switch to ARM mode"
* 5. Cortex-M4 doesn't support ARM mode → INVSTATE fault
* → UsageFault_Handler fires (if enabled)
* or HardFault_Handler fires (if UsageFault not enabled) */
fun_ptr();
printf("In thread mode : after interrupt\n");
for(;;);
}
void RTC_WKUP_IRQHandler(void) { printf("In handler mode : ISR\n"); }
void HardFault_Handler(void)
{
printf("Hard fault detected\n"); /* This fires in this example */
while(1);
}
3. The T-Bit — Complete Explanation
What is the T-Bit?
The T-bit (Thumb state bit) is bit 24 of the EPSR (Execution Program Status Register), which is part of the composite XPSR. It indicates which instruction set the processor is currently executing:
| T-bit Value | Instruction Set | Cortex-M4 Support? |
|---|---|---|
| 1 | Thumb / Thumb-2 | YES — all code runs here |
| 0 | ARM (32-bit) | NO — causes INVSTATE fault |
EPSR — Execution Program Status Register
| Bits | Field | Description |
|---|---|---|
| [31:27] | — | Reserved |
| [26:25] | ICI/IT[7:6] | Continuation bits for multi-load/store (LDM/STM) or IT-block |
| [24] | T | Thumb state bit. Must always be 1 on Cortex-M4. |
| [23:16] | ICI/IT[5:0] | IT-block condition state |
| [15:10] | — | Reserved |
| [9:0] | — | Reserved |
Why Does Function Pointer Address Have LSB=1?
In ARM Thumb code, all function addresses stored in the symbol table have their LSB set to 1. This is the thumb interworking convention. The actual address of the function in memory is address & ~1 (LSB cleared), but when stored as a function pointer or in the vector table, the LSB=1 signals “this is a Thumb function.”
/* Example: a Thumb function at Flash address 0x080001E9 */
/* In the linker map file: */
/* my_function = 0x080001E8 (actual address in Flash: even, 2-byte aligned) */
/* But the symbol value in the ELF: 0x080001E9 (LSB=1 = Thumb) */
void my_function(void) { /* ... */ }
/* Correct: address has LSB=1 */
void (*correct_ptr)(void) = my_function;
/* correct_ptr = 0x080001E9 (LSB=1) → T-bit=1 when called → OK */
/* WRONG: manually set to even address */
void (*wrong_ptr)(void) = (void*)0x080001E8;
/* wrong_ptr = 0x080001E8 (LSB=0) → T-bit=0 when called → HardFault! */
What Happens Step-by-Step When BX is Used with LSB=0
/* Calling fun_ptr() with address 0x080001E8:
*
* 1. Compiler generates: LDR R0, =0x080001E8
* BLX R0
*
* 2. BLX R0 executes:
* a. LR = current PC + 1 (return address with thumb bit)
* b. PC = R0 = 0x080001E8 (address to branch to)
* c. EPSR.T = R0[0] = 0 ← This is the problem!
*
* 3. EPSR.T = 0 means: "switch to ARM state"
* Cortex-M4 checks: "I cannot execute ARM instructions"
* → Generates INVSTATE UsageFault
*
* 4. If UsageFault is not enabled (SHCSR.USGFAULTENA=0):
* Escalates to HardFault_Handler
*
* 5. HardFault_Handler fires and prints "Hard fault detected" */
4. INVSTATE Fault — UFSR Register
The UFSR (UsageFault Status Register) at address 0xE000ED2A records the exact cause of a UsageFault:
| Bit | Field | Cause |
|---|---|---|
| 0 | UNDEFINSTR | Attempted to execute an undefined instruction |
| 1 | INVSTATE | Invalid state: T-bit was 0, or attempted ARM mode, or EPSR.IT state invalid |
| 2 | INVPC | Invalid PC on exception return (bad EXC_RETURN value) |
| 3 | NOCP | No coprocessor: attempted FPU instruction with FPU disabled |
| 8 | UNALIGNED | Unaligned memory access (with UNALIGN_TRP set in CCR) |
| 9 | DIVBYZERO | Division by zero (with DIV_0_TRP set in CCR) |
*(uint32_t*)0xE000ED24 |= (1 << 18);
5. ARM vs Thumb vs Thumb-2 ISA
| ISA | Instruction Width | Register Usage | Cortex-M4? |
|---|---|---|---|
| ARM (A32) | Fixed 32-bit | All 16 GP registers | NO — not supported |
| Thumb (T16) | Fixed 16-bit | Only R0–R7 (low registers) | Partial subset |
| Thumb-2 (T32) | Mixed: 16-bit or 32-bit | All 16 GP registers | YES — primary ISA |
Cortex-M4 executes Thumb-2 instructions. Thumb-2 is a superset of Thumb that adds 32-bit instructions for operations that don’t fit in 16 bits (multiply-accumulate, SIMD, VFP floating point etc.). All compiler output for Cortex-M targets uses Thumb-2.
Why Cortex-M Has No ARM Mode
ARM mode was included in older cores (ARM7TDMI, ARM9) because Thumb-only code was limited (no 32-bit multiplication, limited register access). Thumb-2 eliminated all those limitations. Cortex-M was designed as a purely microcontroller-focused architecture from scratch. The ARM mode was deliberately excluded to reduce gate count, power consumption, and silicon area — factors critical for microcontrollers.
The Interworking Mechanism
/* In ARM architecture, there are two interworking branch instructions:
*
* BX Rn: Branch and eXchange
* - Branches to address in Rn
* - EPSR.T = Rn[0] (bit 0 of the address sets T-bit)
* - PC = Rn & ~1 (actual address, LSB cleared)
*
* BLX Rn: Branch, Link and eXchange
* - Like BX but also saves return address in LR
* - Used for function calls through registers/pointers
*
* On ARM Cortex-A/R (which support both ARM and Thumb):
* Rn[0] = 0 → switch to ARM mode (T=0)
* Rn[0] = 1 → switch to Thumb mode (T=1)
*
* On ARM Cortex-M (Thumb only):
* Rn[0] = 0 → T=0 → INVSTATE fault (ARM not supported)
* Rn[0] = 1 → T=1 → normal Thumb execution */
/* Vector table addresses ALSO have LSB=1 (Thumb) */
/* Example: Reset_Handler at Flash offset 0x08000008 */
/* Vector table entry: 0x080001E9 (LSB=1) */
/* CPU reads this, loads PC = 0x080001E8, T-bit = 1 → Thumb OK */
6. T-Bit in the Vector Table
All vector table entries (exception handler addresses) must have LSB=1 on Cortex-M. If any entry has LSB=0, the corresponding exception will immediately generate an INVSTATE fault when taken.
/* Correct vector table in C */
extern void Reset_Handler(void);
extern void HardFault_Handler(void);
/* The linker ensures all function addresses have LSB=1 (Thumb) */
__attribute__((section(".isr_vector")))
uint32_t vector_table[] = {
0x20020000, /* Initial MSP (not a Thumb address) */
(uint32_t)Reset_Handler, /* = 0x080001E9 (LSB=1 = Thumb) */
(uint32_t)NMI_Handler,
(uint32_t)HardFault_Handler,
/* ... */
};
/* When the processor reads the reset vector and loads it into PC:
* - PC = Reset_Handler address & ~1 = 0x080001E8 (actual instruction address)
* - EPSR.T = 1 (from bit0=1 of the vector entry)
* - Execution starts in Thumb mode → correct */
7. Interview Questions & Answers
Q1. What is the T-bit and where is it located?
Q2. Why must function pointer addresses always have their LSB set to 1 on Cortex-M?
addr & ~1). So:• LSB=1: T-bit = 1 → Thumb mode → valid on Cortex-M
• LSB=0: T-bit = 0 → ARM mode → causes INVSTATE fault on Cortex-M
The ARM GNU toolchain automatically sets LSB=1 for all Thumb function symbols. When you take the address of a C function, the compiler returns the Thumb address (with LSB=1). Manually constructing an address without LSB=1 (as done in this example) defeats this protection and causes a fault.
Q3. What is INVSTATE and when does it occur?
1. The processor attempts to execute in ARM state (T-bit = 0) — applies to Cortex-M which only supports Thumb
2. Branching to an address with LSB=0 (clears T-bit)
3. Returning from an exception with an invalid XPSR T-bit value
4. Executing an instruction from an IT-block that is not consistent with the IT condition
The INVSTATE flag is bit 1 of UFSR at address 0xE000ED2A. Check this flag in your UsageFault handler to diagnose the exact cause.
Q4. What is the difference between ARM, Thumb, and Thumb-2 instruction sets?
Thumb (T16): Fixed 16-bit instruction width. Only accesses R0–R7 (low registers) directly. Smaller code size but limited functionality. First introduced in ARM7TDMI. The “v6-M” Cortex-M0/M0+ use only Thumb-16.
Thumb-2 (T32): Variable-length: both 16-bit and 32-bit instructions. All 16 registers accessible. Full instruction set including multiply-accumulate, saturating arithmetic, SIMD, and optional VFP/FPU. Introduced in ARMv6T2 for Cortex-M3, used in all Cortex-M3/M4/M7. This is what arm-none-eabi-gcc generates for STM32F407 (
-march=armv7e-m -mthumb).Q5. What is ARM-Thumb interworking and is it relevant on Cortex-M?
On Cortex-M, interworking is essentially irrelevant in practice because Cortex-M only supports Thumb/Thumb-2. You cannot have ARM-state code. However, the BX/BLX mechanism itself is still used — just with the requirement that the target address always has LSB=1 (T=1). The compiler flag
-mthumb-interwork still exists for toolchain compatibility but always generates Thumb code on Cortex-M targets.Q6. How does the processor use PC bit 0 differently from a normal register?
• When reading the PC (e.g.,
MOV R0, PC), bit 0 is always 0 (aligned to instruction boundary). The PC returns the current instruction address + 4 (pipeline prefetch effect).• When writing to PC (branch): bit 0 is NOT stored in PC (actual PC is always word- or halfword-aligned). Instead, bit 0 is used to set EPSR.T. The actual PC address becomes (new_value & ~1).
• Exception: when storing PC to the stack during an exception, the stacked PC value does NOT have the Thumb bit set — it is the plain return address.
Q7. In the example, what fault actually fires and why?
fun_ptr() where fun_ptr = (void*)0x080001e8 (LSB=0).Step 1: The compiler generates
BLX R0 to call through the pointer.Step 2: BLX sets T-bit = R0[0] = 0 (ARM state requested).
Step 3: Cortex-M4 detects T=0 → generates INVSTATE UsageFault.
Step 4: Since the example does NOT enable UsageFault (SHCSR.USGFAULTENA=0 — no code sets it), the UsageFault escalates to HardFault.
Step 5:
HardFault_Handler() runs, prints “Hard fault detected” and loops forever.If you added
*(uint32_t*)0xE000ED24 |= (1<<18) before the call, the UsageFault handler would run instead and you could read UFSR bit 1 (INVSTATE=1).Q8. Can you read the T-bit value in software?
MRS R0, EPSR returns 0 — the EPSR is write-ignore-reads, meaning it cannot be read this way. However, you can read it indirectly via XPSR: MRS R0, XPSR returns the composite PSR. Bit 24 of XPSR is the T-bit. But since Cortex-M always has T=1 when executing, reading it from running code will always show 1. The T-bit only becomes 0 as the processor is about to fault — at which point no further instructions execute normally anyway.Q9. What is the DUMMY_XPSR value used in RTOS task initialisation?
0x01000000. The bit pattern is:• Bit 24 = 1 (T-bit set = Thumb state) — this is the critical bit
• All other APSR/IPSR bits = 0 (no flags, not in exception)
When the scheduler switches to this task for the first time, it pops the fake exception frame from the PSP, loading XPSR = 0x01000000 which sets T-bit=1. If T-bit were 0 in this dummy value, the first instruction of the task would cause an INVSTATE fault. You’ll see this exact constant in examples 015 and 016:
#define DUMMY_XPSR 0x01000000U.Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments