프로그램 카운터는 제어를 데이터로 만든다.
pc 레지스터가 다음 명령을 가리키면 컨트롤러는 완전한 기계 상태를 바꾸며 진행할 수 있습니다.
생각해 볼 질문
전이 프로시저 하나가 여러 기계 명령을 어떻게 나타낼까요?
- 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)))리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 587 / 1,048,576바이트
예제
결과—
- 출력
- —
- 값
- —
- 진단
- —
보이는 실행 흐름0 / 0 개의 실행 이벤트
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.
힌트를 보기 전에 프로그램을 바꿔 보세요.
Add a register that counts every instruction rather than only completed remainder cycles.
힌트 하나 보기
Every cond branch must increase the instruction count exactly once.