(Lispex)sicp.io
5.9 · Controller execution

The program counter turns assembled instructions into a machine run.

A small 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. This executor models only its listed instruction shapes and does not implement an arbitrary assembler, stack, or machine language.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(begin
  (define controller
    (vector '(assign n constant 2)
            '(assign product constant 1)
            '(test zero? n)
            '(branch 7)
            '(assign product multiply product n)
            '(assign n sub1 n)
            '(goto 2)
            '(halt)))
  (define state (vector 0 0 #f 0))
  (define (run-machine remaining)
    (if (= remaining 0)
        'step-limit
        (let ((instruction (vector-ref controller (vector-ref state 3))))
          (cond
            ((eq? (car instruction) 'halt)
             (list (vector-ref state 0) (vector-ref state 1)
                   (vector-ref state 2) (vector-ref state 3)))
            ((eq? (car instruction) 'assign)
             (if (eq? (cadr instruction) 'n)
                 (vector-set! state 0
                   (if (eq? (caddr instruction) 'constant)
                       (cadddr instruction)
                       (- (vector-ref state 0) 1)))
                 (vector-set! state 1
                   (if (eq? (caddr instruction) 'constant)
                       (cadddr instruction)
                       (* (vector-ref state 1) (vector-ref state 0)))))
             (vector-set! state 3 (+ (vector-ref state 3) 1))
             (run-machine (- remaining 1)))
            ((eq? (car instruction) 'test)
             (vector-set! state 2 (= (vector-ref state 0) 0))
             (vector-set! state 3 (+ (vector-ref state 3) 1))
             (run-machine (- remaining 1)))
            ((eq? (car instruction) 'branch)
             (vector-set! state 3
               (if (vector-ref state 2)
                   (cadr instruction)
                   (+ (vector-ref state 3) 1)))
             (run-machine (- remaining 1)))
            ((eq? (car instruction) 'goto)
             (vector-set! state 3 (cadr instruction))
             (run-machine (- remaining 1)))))))
  (run-machine 40))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source1,876 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    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 bounded traces describe only this controller and step guard.

    Try it yourself

    Change the program before you read the hint.

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

    Show one hint

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