sicp.io
5.2.3 · Controller execution

The program counter turns assembled instructions into a machine run.

An instruction executor can fetch the instruction named by pc, update an explicit register file, and follow resolved numeric branch targets until halt.

Guiding question

How does one numeric program counter coordinate assignment, testing, and control transfer?

  • Store resolved instructions in a directly indexed controller vector
  • Keep n, product, flag, and pc in an explicit register file
  • Interpret assign, test, branch, goto, and halt instructions
  • Bound execution and retain a visible state history

The controller uses the numeric targets produced by the preceding assembler lesson. The state vector is the register file in n, product, flag, and pc order. Each cycle fetches controller[pc]. An assign or test advances pc, branch either selects 7 or advances, goto writes 2, and halt returns the registers.

The first program executes the complete controller from n equal to 2 and leaves product equal to 2. The second records pc, n, product, and flag after every non-halt instruction while starting from n equal to 1. Both runs have a 40-step guard. The instruction executor handles assign, test, branch, goto, and halt over the resolved controller vector.

SICP code1,876 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns (0 2 #t 7). The second returns ((1 1 0 #f) (2 1 1 #f) (3 1 1 #f) (4 1 1 #f) (5 1 1 #f) (6 0 1 #f) (2 0 1 #f) (3 0 1 #t) (7 0 1 #t)).

    Trace focus

    Follow vector-ref at the current pc before each instruction. Locate product and n assignments, the flag becoming true at pc 2, the false branch advancing to pc 4, goto restoring pc 2, and the true branch selecting halt at pc 7. The execution traces record this controller under its 40-step guard.

    Try it yourself

    Change the program and compare the result.

    Change the first controller's initial n constant to 0. Predict the final registers and identify which assignments the true branch skips.

    Show hint

    After product becomes 1, test makes flag true and branch writes 7 directly into pc.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%