Exercise 2: Tracing the Branchless-Max CMOV Example — Possible Solution ==================================================================== GIVEN ------------------------------ RAX = 10 RBX = 25 STEP 1: CMP RAX, RBX ------------------------------ This computes RAX - RBX = 10 - 25 = -15 (a negative result) and sets the flags accordingly, without changing RAX or RBX themselves. Since 10 is genuinely less than 25 as signed values, the flags end up reflecting a true "RAX is less than RBX" (signed) condition. STEP 2: CMOVL RAX, RBX ------------------------------ Per this chapter's own explanation, CMOVL moves RBX into RAX ONLY IF the signed "less than" condition (set by the preceding CMP) is true. Since RAX (10) genuinely is less than RBX (25), that condition IS true, so the move DOES happen: RAX becomes RBX's value, 25. FINAL VALUE ------------------------------ RAX = 25 after this sequence. WHY THIS IS THE CORRECT "BRANCHLESS MAX" RESULT ------------------------------ The goal of this pattern (per the chapter's own description) is for RAX to end up holding the maximum of its own original value and RBX's value. max(10, 25) = 25, which is exactly what RAX holds afterward — confirming the sequence correctly computed a maximum with no branch instruction anywhere in it. WHY THIS WORKS AS AN ANSWER ------------------------------ It walks through both instructions individually, correctly determines that CMP sets the flags to reflect RAX < RBX, correctly applies CMOVL's own stated condition to determine the move DOES execute, and confirms the final RAX value matches the intended max(10, 25) result.