Exercise 3: Where LDA $FF,X Actually Reads From — Possible Solution ==================================================================== GIVEN ------------------------------ X = $02 Instruction: LDA $FF,X THE NAIVE (WRONG) EXPECTATION ------------------------------ A programmer thinking of this like ordinary absolute,X addressing might expect the base address $FF plus X ($02) to simply carry over into the next page, landing at $0101 — treating the addition as normal, full 16-bit arithmetic the way absolute,X genuinely does. WHAT ACTUALLY HAPPENS ------------------------------ Per this chapter's own warn-box, zero page,X addressing wraps WITHIN zero page itself — the high byte of the resulting address stays permanently zero no matter how far the addition would otherwise carry. The addition $FF + $02 is performed as 8-bit arithmetic confined to the single zero-page byte, not as a full 16-bit address computation: $FF + $02 = $101 in ordinary arithmetic, but confined to 8 bits, the carry is simply dropped, leaving $01. So LDA $FF,X actually reads from address $0001 — a location near the very START of zero page, not $0101 in page 1 as the naive expectation would predict. WHY THE NAIVE EXPECTATION IS WRONG ------------------------------ The mistake is applying absolute,X's behavior (ordinary 16-bit addition, no wraparound, able to spill into any later page) to zero page,X, which is a genuinely different mode with its own wraparound rule. Per the chapter's own distinction, "absolute,X has no such limit; only the zero-page indexed modes wrap this way" — the two modes look similar (both add an index register to a base address) but behave differently specifically at page boundaries, which is exactly the kind of quirk this chapter flags as a real, documented source of bugs. WHY THIS WORKS AS AN ANSWER ------------------------------ It computes the actual wrapped result ($0001, not $0101) using 8-bit arithmetic as the chapter describes, states clearly what the (incorrect) naive expectation would have predicted instead, and explains the root cause of the mistake — conflating zero page,X's wrapping behavior with absolute,X's non-wrapping behavior.