Register Tracer
Run the Register Tracer MicroSim Fullscreen
You can include this MicroSim on your website using the following iframe:
1 2 | |
About This MicroSim
Reading assembly is a skill you build by tracing, not by studying. This is a loop that counts down from 5, and the only way to understand it is to walk it one instruction at a time with the state visible.
1 2 3 4 5 | |
Press Step repeatedly. Watch r0 on the right, and watch the zero flag lamp.
CMP and BNE Are One Mechanism
This is the thing to take away, and it is not obvious from the source.
CMP r0, #0 looks like it asks a question. It does not. It performs the
subtraction r0 - 0, throws the result away, and keeps only the flags. When
the result was zero, the Z flag is set.
BNE loop_start does not compare anything. It reads the Z flag that CMP left
behind and branches if it is clear.
Neither instruction is a conditional on its own. The condition lives in the flag between them — one instruction writes it, the next reads it. Every conditional loop in every assembly language works this way, and once you see it here you will recognize it everywhere.
Why This Matters for FFT Code
The next chapters put butterflies in assembly. Those loops run thousands of times, and every iteration pays for its CMP and its BNE.
Once you can trace a loop, you can start asking the optimizer's questions: does the SUB already set the flags, making the CMP redundant? Can the loop count down to zero so the comparison is free? Those savings are only visible to someone who can see the flag hand-off — which is what this sim is training.
How to Use
- Press Step once. r0 becomes 5, and note the Z lamp does not change —
MOVdoes not set flags. - Step past the label. Nothing executes; a label is just a name for an address.
- Step through SUB, then CMP. Read the "Last action" panel each time.
- Step the BNE. Z is clear, so it branches back — the arrow jumps up.
- Keep stepping. After five iterations r0 reaches 0, the lamp lights, and BNE falls through instead of branching.
- Press Reset, then Run to completion to watch the whole thing.
Lesson Plan
Grade Level
Undergraduate (college junior/senior)
Duration
10-12 minutes
Prerequisites
- A register holds a value
- Loops and conditional branches in a high-level language
Learning Objective
Students will be able to interpret a short assembly loop by tracing register contents and status flags one instruction at a time.
Activities
- Full trace (5 min): Students step the entire loop and record r0 and Z after every instruction.
- Count the iterations (3 min): Students state how many times the body runs and confirm it against the source.
- Find the hand-off (4 min): Students identify which instruction writes Z and which reads it, and explain why neither is conditional alone.
Assessment
Ask: "If you deleted the CMP r0, #0 line, what would BNE branch on, and would
the loop still terminate?"
Related Resources
References
- ARM Cortex-M instruction set — MOV, SUB, CMP, and the branch conditions.
- Status register — the flags mechanism this loop depends on.