Thumb Instruction Set and Assembly Language Basics
What You'll Learn
- 1Distinguish 16 bit and 32 bit Thumb encodings
- 2Read common Cortex-M assembly mnemonics
- 3Explain if then blocks and conditional execution
- 4Disassemble C code and explain the generated instructions
Every Cortex-M core executes the Thumb instruction set, which packs common operations into 16 bit words and uses a 32 bit prefix for the rest. That mix is what gives ARM its code density without giving up a full instruction vocabulary. Assembly appears in startup code, interrupt entry, timing critical inner loops, and in the disassembly you read while debugging. You do not need to write large programs in assembly, but you must be able to read what the compiler produced.
Encodings and Instruction Fields
A 16 bit Thumb instruction covers register moves, small immediate arithmetic, narrow branches, and stack operations, which dominate typical code. A 32 bit instruction is formed by two halfwords and carries the wide arithmetic, multiply accumulate, long branches, and coprocessor operations. The program counter reads as the current instruction address plus four because of pipeline behavior, which matters when you build vector tables and veneers.
Instructions are aligned to halfword boundaries, and an attempt to fetch from an odd address triggers a usage fault. Branch targets use relative offsets rather than absolute addresses, so position independent code comes almost for free inside a flash region.
Conditional Execution and If Then Blocks
In older Thumb, most instructions could be conditional. Thumb-2 restores that flexibility through if then blocks, which conditionally execute up to four following instructions. The assembler writes them for you when you use a conditional branch pattern, and the disassembler prints the execution state bits back as an if then line.
Condition codes are the same set as the flags: equal, not equal, carry set, greater or equal, and so on. Compilers still prefer a compare plus a branch in most cases, so a long conditional sequence in assembly is usually a sign that you are reading a hand tuned kernel.
Loads, Stores, and Addressing Modes
Cortex-M memory instructions use base plus offset addressing with byte, halfword, and word sizes, plus write back and pre or post indexing. Multiple register loads and stores move blocks quickly, and the exclusive load and store pair implements atomic operations for spin locks and RTOS primitives. Unaligned word accesses are allowed on some cores and fault on others, so aligned buffers are the portable choice.
Literal pools hold constant data near the code that uses it, which is why you see a program counter relative load of a constant in almost every disassembly window. Peripheral drivers mostly reduce to load and store instructions aimed at a base address held in a register.
Thumb Encoding at a Glance
| Class | Encoding | Typical Example |
|---|---|---|
| Register move | 16 bit | Copy a value between low registers |
| Immediate arithmetic | 16 bit | Add a small constant to a register |
| Wide arithmetic | 32 bit | Add a larger constant or two high registers |
| Multiply accumulate | 32 bit | Single cycle DSP style accumulation |
| Load and store | 16 or 32 bit | Word access with base plus offset |
| Branch | 16 or 32 bit | Relative branch with a condition code |
Real-World Examples
- Read a startup disassembly â Identify the vector table, the reset handler address, and the copy and zero loops that initialize data and bss.
- Explain a compiler loop â Notice how the decrement and compare fuse into one instruction and how the branch folds the condition.
- Spot an atomic sequence â An exclusive load followed by an exclusive store is a lock free critical section used by an RTOS.
- Find a constant â A program counter relative load reveals a literal pool entry holding a peripheral base address.
đĄRead the Disassembly, Not Just the Source
Common Mistakes
Assuming every mnemonic is one memory word
Fix: Many Thumb instructions take 32 bits, so code size must come from the map file rather than an instruction count.
Misreading the program counter value
Fix: Reading the PC returns the instruction address plus four, which shifts any hand computed offsets.
Writing assembly for everything
Fix: Hand written assembly loses to the compiler on maintenance; reserve it for startup, barriers, and proven hot loops.
Key Takeaways
- Thumb mixes 16 bit and 32 bit encodings for density and capability.
- If then blocks restore conditional execution in Thumb-2.
- Base plus offset addressing covers nearly all memory access.
- Disassembly is the ground truth for timing and size questions.
Frequently Asked Questions
Why is it called Thumb?
The original idea was a narrow 16 bit instruction set, a smaller thumbprint, later extended into Thumb-2 with 32 bit instructions.
Does Thumb code run slower than ARM mode code?
Cortex-M cores only implement Thumb, so there is no alternative mode to compare against on these devices.
How do I count code size accurately?
Use the linker map or the section sizes, because variable length instructions and alignment padding defeat manual counting.
Interview Questions
Q1: Explain what happens on a branch with link instruction.
The target address goes into the link register and the processor branches, so the following return instruction can resume after the call.
Q2: How would you find a spin lock in assembly?
Look for an exclusive load, a compare, a conditional branch back, and an exclusive store, which is the standard load exclusive store exclusive pattern.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type â Always start by categorizing the problem
- Incorrect formula application â Verify the formula applies to your specific case
- Unit errors â Always check units are consistent throughout
- Sign errors â Pay careful attention to positive and negative values
- Rounding too early â Keep full precision until the final answer
- Forgetting constraints â Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic â Build on the foundation laid here
- Next Topic â See how this concept extends to more advanced material
- Practice Problems â Additional exercises with solutions
- Video Explanation â Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions