프로그램 카운터는 조립된 명령을 실제 기계 실행으로 만든다.
작은 실행기는 pc가 가리키는 명령을 가져와 명시적인 레지스터 파일을 바꾸고 숫자 분기 대상을 따라 halt까지 진행할 수 있습니다.
숫자 프로그램 카운터 하나가 대입과 검사와 제어 이동을 어떻게 조정할까요?
- Store resolved instructions in a directly indexed controller vector
- Keep n, product, flag, and pc in an explicit register file
- Interpret assign, test, branch, goto, and halt instructions
- Bound execution and retain a visible state history
The controller uses the numeric targets produced by the preceding assembler lesson. The state vector is the register file in n, product, flag, and pc order. Each cycle fetches controller[pc]. An assign or test advances pc, branch either selects 7 or advances, goto writes 2, and halt returns the registers.
The first program executes the complete controller from n equal to 2 and leaves product equal to 2. The second records pc, n, product, and flag after every non-halt instruction while starting from n equal to 1. Both runs have a 40-step guard. This executor models only its listed instruction shapes and does not implement an arbitrary assembler, stack, or machine language.
(begin
(define controller
(vector '(assign n constant 2)
'(assign product constant 1)
'(test zero? n)
'(branch 7)
'(assign product multiply product n)
'(assign n sub1 n)
'(goto 2)
'(halt)))
(define state (vector 0 0 #f 0))
(define (run-machine remaining)
(if (= remaining 0)
'step-limit
(let ((instruction (vector-ref controller (vector-ref state 3))))
(cond
((eq? (car instruction) 'halt)
(list (vector-ref state 0) (vector-ref state 1)
(vector-ref state 2) (vector-ref state 3)))
((eq? (car instruction) 'assign)
(if (eq? (cadr instruction) 'n)
(vector-set! state 0
(if (eq? (caddr instruction) 'constant)
(cadddr instruction)
(- (vector-ref state 0) 1)))
(vector-set! state 1
(if (eq? (caddr instruction) 'constant)
(cadddr instruction)
(* (vector-ref state 1) (vector-ref state 0)))))
(vector-set! state 3 (+ (vector-ref state 3) 1))
(run-machine (- remaining 1)))
((eq? (car instruction) 'test)
(vector-set! state 2 (= (vector-ref state 0) 0))
(vector-set! state 3 (+ (vector-ref state 3) 1))
(run-machine (- remaining 1)))
((eq? (car instruction) 'branch)
(vector-set! state 3
(if (vector-ref state 2)
(cadr instruction)
(+ (vector-ref state 3) 1)))
(run-machine (- remaining 1)))
((eq? (car instruction) 'goto)
(vector-set! state 3 (cadr instruction))
(run-machine (- remaining 1)))))))
(run-machine 40))- 출력
- —
- 값
- —
- 진단
- —
The first program returns (0 2 #t 7). The second returns ((1 1 0 #f) (2 1 1 #f) (3 1 1 #f) (4 1 1 #f) (5 1 1 #f) (6 0 1 #f) (2 0 1 #f) (3 0 1 #t) (7 0 1 #t)).
Follow vector-ref at the current pc before each instruction. Locate product and n assignments, the flag becoming true at pc 2, the false branch advancing to pc 4, goto restoring pc 2, and the true branch selecting halt at pc 7. The bounded traces describe only this controller and step guard.
힌트를 보기 전에 프로그램을 바꿔 보세요.
Change the first controller's initial n constant to 0. Predict the final registers and identify which assignments the true branch skips.
힌트 하나 보기
After product becomes 1, test makes flag true and branch writes 7 directly into pc.