Exercise 2: Tracing the Z80's UPDATE_MAX With Candidate 80, MAX 50 — Possible Solution ==================================================================== GIVEN ------------------------------ Candidate = 80 MAX (in memory) = 50 STEP BY STEP ------------------------------ LD C, A saves the candidate (80) into C. LD A, (MAX) loads the current MAX (50) into A. CP C computes A - C = 50 - 80 = -30. Per this chapter's own explanation of Z80 CP semantics — the OPPOSITE convention from the 6502 — Carry is SET if A < the compared value (a borrow WAS needed), and Carry is CLEAR if A >= the compared value (no borrow needed). Since A (50, the old MAX) is less than C (80, the candidate), a borrow WAS needed, so Carry ends up SET after this CP. JR NC, SKIP only jumps when there is NO carry (NC). Since Carry is SET here (not clear), the jump condition is NOT met, so execution does NOT jump to SKIP — it falls straight through to the next two instructions instead. LD A, C reloads the candidate (80) into A. LD (MAX), A stores 80 into MAX, replacing the old value of 50. RET then returns from the subroutine. RESULT ------------------------------ MAX IS updated. It becomes 80 after the subroutine call, since the candidate (80) was genuinely larger than the previous MAX (50), and this comparison correctly recognized that and performed the update. WHY THIS WORKS AS AN ANSWER ------------------------------ It computes the actual CP subtraction, correctly applies the Z80's own Carry-Set-means-a-borrow-was-needed convention (explicitly the opposite of the 6502's, as this chapter states), correctly determines that JR NC's jump condition is NOT met so the update code runs, and states the final, updated value of MAX with the reasoning shown.