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.
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.
(begin
(define (step state)
(let ((pc (car state))
(a (cadr state))
(b (caddr state))
(temp (cadddr state))
(cycles (list-ref state 4)))
(cond ((= pc 0) (list (if (= b 0) 5 1) a b temp cycles))
((= pc 1) (list 2 a b (remainder a b) cycles))
((= pc 2) (list 3 b b temp cycles))
((= pc 3) (list 4 a temp temp cycles))
((= pc 4) (list 0 a b temp (+ cycles 1)))
(else state))))
(define (run state)
(if (= (car state) 5) state (run (step state))))
(run (list 0 10 4 0 0)))- Output
- —
- Value
- —
- Diagnostic
- —
The first program halts with (5 2 0 0 2). pc 5 marks halt, a holds 2, and two remainder cycles completed.
Read the recurring pc sequence 0, 1, 2, 3, 4. The final test changes pc directly from 0 to the halt value 5.
Change the program before you read the hint.
Add a register that counts every instruction rather than only completed remainder cycles.
Show one hint
Every cond branch must increase the instruction count exactly once.