sicp.io
5.2.6 · Machine instrumentation

The machine can report the work its controller performed.

An explicit register-machine executor counts fetched instructions, stack pushes, and maximum stack depth while preserving the controller result.

Guiding question

What can controller-level counters reveal that the final register values alone cannot?

  • Count every fetched instruction with one explicit convention
  • Count total save operations without confusing them with current stack depth
  • Track maximum stack depth while restore operations unwind the stack
  • Compare the base and recursive paths of the same factorial controller
  • Keep controller measurements distinct from wall-clock time and hardware profiling

The controller is the same finite recursive factorial machine for every run. Its explicit register vector holds n, val, continue, flag, and pc. A separate list represents the stack. The executor increments instruction-count as soon as it fetches an instruction, including halt. push! increments total-pushes and updates max-depth from the current list length; pop! shortens the live stack but does not erase the historical push count.

With n equal to 1, the branch jumps directly to the base assignment and reaches halt after five fetched instructions with no stack use. With n equal to 3, the controller saves continue and n at two recursive levels, so it performs four pushes, reaches depth four, and fetches 27 instructions before halting with value 6 and an empty stack. These numbers describe this exact controller, input, and counting convention. They are not elapsed time, CPU instructions, allocation cost, or a general profiler.

SICP code3,860 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 (6 27 4 4 0). The second returns ((1 1 5 0 0) (2 2 16 2 2) (4 24 38 6 6)).

    Trace focus

    Follow instruction-count immediately after each vector fetch, including halt. Compare each save with a push increment and a possible max-depth update, then watch restore shorten the current stack without reducing total-pushes. The base path jumps directly to instruction 11, while each additional recursive level contributes two saves and later two restores.

    Try it yourself

    Change the program and compare the result.

    Predict the report for input 3 without running it. Then change the counting convention so halt is not included and explain exactly which field changes.

    Show hint

    Input 3 creates two recursive levels. Each level pushes continue and n. Excluding halt subtracts one only from the fetched-instruction count.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%