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
(begin(definecontroller(vector'(testbase?n)'(branch11)'(savecontinue)'(saven)'(assignnsub1n)'(assigncontinueconstant7)'(goto0)'(restoren)'(restorecontinue)'(assignvalmultiplynval)'(goto-registercontinue)'(assignvalconstant1)'(goto-registercontinue)'(halt)))(define(register-indexname)(cond((eq?name'n)0)((eq?name'val)1)((eq?name'continue)2)((eq?name'flag)3)((eq?name'pc)4)))(define(run-factorialstart)(let((registers(vectorstart013#f0))(stack'())(instruction-count0)(total-pushes0)(max-depth0))(define(get-registername)(vector-refregisters(register-indexname)))(define(set-register!namevalue)(vector-set!registers(register-indexname)value))(define(advance!)(set-register!'pc(+(get-register'pc)1)))(define(push!value)(set!stack(consvaluestack))(set!total-pushes(+total-pushes1))(set!max-depth(maxmax-depth(lengthstack))))(define(pop!)(let((value(carstack)))(set!stack(cdrstack))value))(define(assign-valueinstruction)(let((operation(caddrinstruction)))(cond((eq?operation'constant)(cadddrinstruction))((eq?operation'sub1)(-(get-register(cadddrinstruction))1))((eq?operation'multiply)(*(get-register(cadddrinstruction))(get-register(list-refinstruction4)))))))(define(executeremaining-steps)(if(=remaining-steps0)'step-limit(let*((instruction(vector-refcontroller(get-register'pc)))(operation(carinstruction)))(set!instruction-count(+instruction-count1))(cond((eq?operation'halt)(list(get-register'val)instruction-counttotal-pushesmax-depth(lengthstack)))((eq?operation'test)(set-register!'flag(=(get-register(caddrinstruction))1))(advance!)(execute(-remaining-steps1)))((eq?operation'branch)(set-register!'pc(if(get-register'flag)(cadrinstruction)(+(get-register'pc)1)))(execute(-remaining-steps1)))((eq?operation'save)(push!(get-register(cadrinstruction)))(advance!)(execute(-remaining-steps1)))((eq?operation'restore)(set-register!(cadrinstruction)(pop!))(advance!)(execute(-remaining-steps1)))((eq?operation'assign)(set-register!(cadrinstruction)(assign-valueinstruction))(advance!)(execute(-remaining-steps1)))((eq?operation'goto)(set-register!'pc(cadrinstruction))(execute(-remaining-steps1)))((eq?operation'goto-register)(set-register!'pc(get-register(cadrinstruction)))(execute(-remaining-steps1)))))))(execute200))); value, fetched instructions, pushes, max depth, final depth(run-factorial3))
Examples
Result—
Output
—
Value
—
Diagnostic
—
Execution trace0 / 0 events
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.