sicp.io
5.2.2 · Control in a register

A program counter turns control into data.

A pc register identifies the next instruction, so a controller can advance by transforming the complete machine state.

Guiding question

How can one transition procedure represent several machine instructions?

  • Use a pc value to select one instruction
  • Advance control and data registers together
  • Recognize a halt state in the controller

The state holds pc, a, b, temp, and cycles. Each cond branch implements exactly one instruction and writes the pc of the instruction that should follow.

Instruction 0 tests b and either enters the remainder cycle or jumps to halt at pc 5. The other instructions move values through temp before returning control to the test.

SICP code587 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 halts with (5 2 0 0 2). pc 5 marks halt, a holds 2, and two remainder cycles completed.

    Trace focus

    Read the recurring pc sequence 0, 1, 2, 3, 4. The final test changes pc directly from 0 to the halt value 5.

    Try it yourself

    Change the program and compare the result.

    Add a register that counts every instruction rather than only completed remainder cycles.

    Show hint

    Every cond branch must increase the instruction count exactly once.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%