Exercise 2: The Three Jobs DJNZ Combines — Possible Solution ==================================================================== THE THREE SEPARATE JOBS DJNZ COMBINES ------------------------------ 1. DECREMENT — it subtracts 1 from register B, exactly like a standalone "decrement B" instruction would. 2. COMPARE (against zero) — it checks whether that decremented result is zero, functioning like a comparison against zero without needing a separate CPX-style instruction. 3. CONDITIONAL BRANCH — it jumps back to the loop's target label if the decremented result was NOT zero, and falls straight through to the next instruction if it WAS zero. All three of these happen as the effect of executing exactly one instruction. THE CONSEQUENCE FOR THE "7 VS. 3" COMPARISON ------------------------------ On the 6502 side, this chapter's own loop needed three completely separate instructions to accomplish the same three jobs: INX (decrement/increment — the counting step), CPX #5 (the comparison step), and BNE LOOP (the actual conditional branch step). Each of those instructions has to be individually fetched, decoded, and executed as its own separate step in the fetch-decode-execute cycle (assembly1-2/cpu8bit1-2's own shared foundation). DJNZ collapses exactly those same three logical steps into a single fetch-decode-execute pass. This is precisely why the loop-management portion of the Z80's loop body is so much shorter in instruction count than the 6502's — it isn't that the Z80 is somehow "skipping" any of the three jobs, it's that one Z80 instruction does the combined work that took three separate 6502 instructions to express. WHY THIS WORKS AS AN ANSWER ------------------------------ It names each of the three jobs DJNZ performs individually rather than describing it as a vague "shortcut," and explicitly maps each job back to its own separate 6502 instruction (INX, CPX, BNE) to show precisely why the instruction-count gap exists.