Exercise 3: Saving and Restoring Both R0 and R7 in the Correct Order — Possible Solution ==================================================================== THE PUSH/POP PAIR ------------------------------ ; Save both R0 and R7 before the nested call ADD R6, R6, #-1 STR R0, R6, #0 ; push R0 first ADD R6, R6, #-1 STR R7, R6, #0 ; push R7 second (now on top of the stack) JSR SOMESUB ; the nested call -- safe now, both registers preserved ; Restore both registers before returning -- REVERSE order LDR R7, R6, #0 ; pop R7 first (it was pushed last, so it's on top) ADD R6, R6, #1 LDR R0, R6, #0 ; pop R0 second ADD R6, R6, #1 RET WHY THE RESTORE ORDER MUST BE THE EXACT REVERSE ------------------------------ Per this chapter's own description, the stack is a last-in-first-out (LIFO) structure: R6 (the stack pointer) always points at whatever was pushed MOST RECENTLY. In this sequence, R0 is pushed first, then R7 is pushed second -- which means R7 is sitting at the current top-of-stack position (the address R6 points to right now), and R0 is one slot further down. If the pop order matched the push order instead (popping R0 first), the first LDR would read R6's current top-of-stack value -- which is actually R7's saved value, not R0's -- and incorrectly load it into R0. The second pop would then read the wrong slot for R7 as well. Both registers would end up with each other's values, silently corrupting both without any error being raised. Popping in the exact reverse order (R7 first, since it was pushed last, then R0) guarantees each LDR reads back precisely the value that was pushed at that same stack position -- matching pushes to pops correctly requires undoing them in the opposite order they were made. WHY THIS WORKS AS AN ANSWER ------------------------------ It gives a correct, complete push/pop sequence for both registers around a nested call, and explains the reverse-order requirement using the chapter's own LIFO stack model -- showing concretely what goes wrong (the two registers' values get swapped) if the order isn't reversed, rather than just asserting that reversal is "the rule."