← All lessons

The Stack and Functions · Lesson 21 of 30 · about 15 minutes

Preserving a Caller’s State

Use a save/restore convention when a function needs scratch space.

01 / Explain

Understand the idea

A function may change registers unless the program agrees on a convention. A callee that promises to preserve R1 must save its value before using R1 as scratch space, then restore it before returning.

PUSH and POP save data; CALL and RETURN manage separate call frames. A called function can return safely while the caller has an active LOOP. Restoring data with POP does not consume a function’s return address.

02 / Try

Watch it happen

Expect 2, 4, 6. CALL returns to the loop body; it does not terminate the outer loop.

LOAD 1 R1
LOOP 3
LOAD R1
CALL double
PRINT
LOAD R1
ADD 1
COPY R0 R1
RETURN
HALT
double: MUL 2
RETURN

Use Step to follow one instruction at a time. You can change the example and replay it.

03 / Challenge

Make it work

Read a saved value into R1, then a function argument. Call a helper that returns argument + 1 while temporarily using R1 as scratch space. Print the result and then the preserved first value. Use PUSH and POP to preserve R1.

The checker runs your current editor program in a fresh machine for each of 3 test cases. It supplies inputs and seeded memory itself; the lab’s current output and memory do not decide your result.

INPUT
COPY R0 R1
INPUT
CALL helper
PRINT
LOAD R1
PRINT
HALT
helper:
  # Preserve R1, calculate argument + 1, restore R1.
RETURN
Need a hint?

Save the argument in R2 before loading R1 to push it. Save the result while POP restores the old R1.

Reveal a worked solution

Read the program, predict each instruction’s effect, then step through it in the lab.

INPUT
COPY R0 R1
INPUT
CALL helper
PRINT
LOAD R1
PRINT
HALT
helper: COPY R0 R2
LOAD R1
PUSH
LOAD 99 R1
LOAD R2
ADD 1
COPY R0 R2
POP
COPY R0 R1
LOAD R2
RETURN

Emoji CPU lab

Ready · 0 cycles

Emoji program

Type LOAD, ADD, or another opcode then Space to insert emoji. Ctrl/⌘ + Enter runs or pauses; Escape pauses; Ctrl/⌘ + ] indents. Tab moves focus. Labels use a colon. Jumps use zero-based instruction addresses.

Instruction map and breakpoints (0)

Breakpoints stop before an instruction. Run resumes past the stopped breakpoint once; Step executes it directly. Editing source clears old breakpoints and machine state.

    CPU registers

    R0
    0
    R1
    0
    R2
    0
    R3
    0
    R4
    0
    R5
    0
    R6
    0
    R7
    0
    Program counter (PC)
    0
    Stack pointer (SP)
    255
    Flags
    Zero: off · Negative: off · Overflow: off
    Delay request
    0 ms
    Stacks and loop frames

    SP = 255 − data depth − call depth. The stack is separate from memory.

    Data stack (bottom → top)

    Empty

    Call return addresses (bottom → top)

    Empty

    Loop frames

    Empty

    Output and input

    Run a PRINT instruction to see output.

    Queued input: Empty

    Memory · 256 bytes · 0 nonzero

    Each cell shows address:value. R = read this step; W = written this step. Select a cell to inspect or initialize it before execution. Use arrow keys to move, Home/End for the row, and Ctrl/⌘ + Home/End for the whole memory.

    Reset before initializing memory. Stored values wrap to 0–255.
    Execution trace · 0 entries

    Recent entries below. Inspect any zero-based index to see complete detached before/after state.

    Check your challenge

    You can run this check any time. Every case must pass to record completion.

    Progress uses localStorage only. It stays on this browser and is never sent to a server.