Definition
Thumb is an instruction set of Arm processors in which the common instructions are 16 bits long, half the length of the original 32-bit Arm instructions, so that programs take less memory. Thumb-2 is the extension, introduced with the Armv6T2 architecture, that adds 32-bit instructions to Thumb; 16-bit and 32-bit instructions mix freely in one stream. The combined set is still called the Thumb instruction set, and later Arm manuals call it T32. Cortex-M microcontroller §6.1 cores execute only Thumb instructions.
The idea
In a small device memory is scarce, and the size of a program depends on how long its instructions are. The original Arm instruction set gives every instruction 32 bits. Thumb, introduced with the Armv4T architecture in the 1990s, re-encoded the most common operations in 16 bits. The price was that most 16-bit instructions can reach only registers r0 to r7, hold only small constants, and cannot be made conditional. A processor could switch between the two instruction sets, running Thumb code where size mattered and Arm code where speed mattered.
Thumb-2 removed the need to switch. It added 32-bit instructions to Thumb that cover nearly everything the Arm instruction set can do, while the common operations keep their 16-bit forms, and a compiler chooses the short form whenever one exists. For microcontrollers Arm then dropped the original instruction set entirely: the Armv7-M architecture runs Thumb code only.
The price of mixing two lengths is that a decoder, the part of the processor or of a tool that reads instructions, must know where each instruction begins. Thumb settles this with a rule that looks only at the first 16 bits of an instruction: five of those bits say whether a second 16-bit unit follows. The rule works forward from an instruction start that is already known. A 16-bit unit taken from the middle of the code cannot be classified, because the second half of a 32-bit instruction may hold any value at all.
How it works
A halfword is 16 bits, or 2 bytes, and a word is 32 bits. Thumb code is a sequence of halfwords stored at even addresses. Each instruction is either one halfword, a 16-bit instruction, or two consecutive halfwords, a 32-bit instruction. On Armv7-M, instruction fetches are always little-endian §4.21: the byte at the lower address holds bits 7 to 0 of a halfword, and the byte at the next address holds bits 15 to 8. In a 32-bit instruction the first halfword is stored first, at the lower address.
Decoding proceeds from an address that is known to be an instruction start.
Read the halfword at : the byte at gives the low 8 bits and the byte at the high 8 bits.
Look at its top five bits, bits 15 to 11.
If they are
11101,11110or11111, the halfword is the first half of a 32-bit instruction. Read the halfword at as the second half, decode the two together, and continue at .Otherwise the halfword is a complete 16-bit instruction. Decode it and continue at .
The three prefixes are exactly the halfword values from 0xE800 to 0xFFFF, because 11101 followed by eleven zero bits is , which is 0xE800 in hexadecimal. The prefix 11100, just below them, belongs to a 16-bit instruction, the unconditional branch, so a decoder must compare all five bits.
The rule gives the length and nothing more. Whether the 32 bits form a defined instruction must still be checked: some 32-bit patterns are undefined, and others exist only in later architecture versions or with optional extensions such as floating point.
Known instruction starts come from outside the instruction stream: the reset and exception handler §6.15 addresses in the vector table §6.14, function addresses in a symbol table §12.8, and the targets of branches already decoded. When an Arm processor branches with BX or BLX (branch and exchange instruction set) to an address held in a register, or loads such an address into the program counter §2.9, bit 0 of that address is a flag rather than part of the address: 1 means the target is Thumb code. Cortex-M cores support only Thumb, so their function pointers and vector table entries are odd. A function whose first instruction is at 0x08000100 is called through the address 0x08000101, and the processor fetches from the even address 0x08000100. Such a branch to an address whose bit 0 is 0 makes a Cortex-M core raise a UsageFault, the exception that reports an instruction used wrongly, with the cause INVSTATE (invalid state).
In Arm’s Unified Assembler Language (UAL), the common assembly syntax for Arm and Thumb code, the suffix .N (narrow) asks for a 16-bit encoding and .W (wide) for a 32-bit one. Without a suffix, the assembler picks the 16-bit encoding whenever one exists.
Worked example
Take the 8 bytes 17 22 4f f0 01 00 70 47, written in hexadecimal, at offsets 0 to 7 from the start of a function, so offset 0 is a known instruction start.
Offset 0. The bytes
17and22form the halfword0x2217, binary0010 0010 0001 0111. Its top five bits are00100, not a prefix, so this is a 16-bit instruction:movs r2, #23, move the constant 23 into register r2 and set the condition flags §4.42. Bits 15 to 11,00100, select this kind of move; bits 10 to 8,010, select r2; bits 7 to 0,00010111, are . The next start is .Offset 2. The bytes
4fandf0form0xF04F, binary1111 0000 0100 1111. Its top five bits are11110, a prefix, so this is a 32-bit instruction whose second half is the halfword at offset 4,0x0001. Together they aremov.w r0, #1, the 32-bit move of the constant 1 into r0, which does not set the flags. The next start is .Offset 6. The bytes
70and47form0x4770, binary0100 0111 0111 0000. Its top bits are01000, so this is a 16-bit instruction:bx lr, return by jumping to the address in the link register §4.45 (LR), which holds the return address. The next start is , the end of the bytes.
The instruction starts are offsets 0, 2 and 6. Offset 4 is not a start: it is the second half of mov.w. The LLVM §12.18 compiler toolkit’s assembler produces exactly these bytes for these three instructions.
Now decode the same bytes from the wrong start, offset 4, as a disassembler (a tool that turns machine code back into assembly text) does when it is given a wrong address. The halfword there is 0x0001, whose top bits 00000 make it a 16-bit instruction: movs r1, r0, copy r0 into r1 and set the flags. The real program never executes that instruction, yet it looks perfectly reasonable. Decoding then continues at offset 6, finds bx lr again and agrees with the correct decoding from there on, so nothing in the output reveals the mistake.
Stated precisely
Let be an address that is known to be an instruction start, and let be the byte at address . The halfword at is , and its top five bits form the number , where means rounded down to a whole number. The length in bytes of the instruction at is where 29, 30 and 31 are the binary numbers 11101, 11110 and 11111. The next instruction in sequence starts at . Starting from a known start , the sequential starts are , , , and so on, each computed from the one before.
The statement depends on three assumptions.
A true start. The address really is an instruction start. The rule cannot establish this, because the second halfword of a 32-bit instruction can hold any 16-bit value, including one of the three prefixes.
Thumb state on a Thumb-2 architecture. The processor executes Thumb code and implements 32-bit Thumb instructions: Armv6T2 or later, including Armv7-M and Armv8-M. Armv6-M, the architecture of the Cortex-M0, uses the same length rule but defines only a handful of 32-bit instructions, such as
BL(branch with link), the function call instruction.Code, not data. The bytes are instructions. Compilers place blocks of constants, called literal pools, inside the code area next to the functions that use them, and decoding them as instructions gives meaningless output.
Length, validity and legal entry are three separate facts. The prefix rule gives the length. The full encoding decides whether the instruction is defined. An address is a legal branch target only if it is an instruction start and does not lie inside an IT block §4.41, a group of up to four instructions made conditional by a preceding If-Then (IT) instruction, which the Conditional execution and IT block entry explains.
Limits and common mistakes
A halfword does not say whether it is an instruction start. Decoding must run forward from a known start; it cannot run backward, and a wrongly chosen start can produce a plausible but false instruction stream, as the worked example shows.
A linear-sweep disassembler, which decodes a code section from its first byte to its last, misreads literal pools as instructions. Arm object files therefore carry mapping symbols that mark each region: $t for Thumb code, $a for Arm code and $d for data. A disassembler that follows them avoids decoding data.
A 32-bit Thumb instruction needs only halfword alignment §4.22, so it may straddle a 4-byte word boundary. A tool that reads code as aligned 32-bit words, for example to patch it, must not assume that each word holds one whole instruction.
Bit 0 of a code address used for a branch is the Thumb flag, not part of the address. Comparing a function pointer with the address of the function’s first instruction, or reading the function’s code through the pointer, is off by one if bit 0 is forgotten.
Thumb-2 is not a separate instruction set from Thumb, and Thumb does not mean 16-bit only. On Armv7-M one Thumb program contains both lengths.
One 16-bit encoding can behave differently in different places. The halfword 0x2107 means movs r1, #7, which sets the flags, outside an IT block, and mov r1, #7 with a condition, which does not set the flags, inside one. An analysis must decode the encoding in its context, not trust the mnemonic.
Questions answered
How does a decoder distinguish the start of a 32-bit instruction?
A Thumb decoder recognizes a 32-bit instruction by the top five bits, bits 15 to 11, of the 16-bit halfword at a known instruction start. If they are 11101, 11110 or 11111, which is the same as the halfword being at least 0xE800, the halfword is the first half of a 32-bit instruction and the following halfword is its second half. Any other value is a complete 16-bit instruction. The decoder then moves on by 4 or 2 bytes and applies the rule again. Halfwords are read in little-endian order, so the deciding bits are in the byte at the higher address.
The rule works only forward from an address already known to be an instruction start, such as a function’s entry point, a vector table entry with its Thumb flag in bit 0 removed, or the target of a decoded branch. The second halfword of a 32-bit instruction can hold any value, including one that looks like a 16-bit instruction or a 32-bit prefix, so a halfword on its own cannot be identified as a start. The rule gives only the length: whether the 32 bits form a defined instruction for the processor’s architecture version is a separate check.
Related entries: Instruction encoding (§2.7), Control-flow graph (CFG) (§3.13), Machine-code proof boundary (§4.48), Conditional execution and IT block (§4.41), Arm Cortex-M4 (§6.3), Instruction set architecture (ISA) (§4.38). Further reading: [5, 81, 170].