레이블 표는 이름을 명령 위치로 바꾼다.
조립기는 컨트롤러 텍스트를 훑고 실행 명령만 세어 기호 분기 대상을 조립된 명령 위치로 바꿀 수 있습니다.
기계가 숫자 위치를 필요로 하기 전까지 컨트롤러가 읽기 쉬운 레이블을 쓰려면 어떻게 해야 할까요?
- Distinguish label symbols from instruction lists
- Count instruction positions without counting labels
- Build a table from each label to its next instruction
- Resolve branch and goto targets while removing labels
extract-labels carries the position of the next instruction. Encountering a symbol records that position without incrementing it; encountering an instruction increments the position. start therefore names 0, loop names 1, and done names 5.
assemble skips label symbols and asks resolve to replace only branch and goto targets. Ordinary instructions remain unchanged. The returned sequence is assembled controller data with numeric targets; this lesson does not execute that sequence or prove a complete machine assembler.
(begin
(define (extract-labels controller position)
(cond ((null? controller) '())
((symbol? (car controller))
(cons (cons (car controller) position)
(extract-labels (cdr controller) position)))
(else
(extract-labels (cdr controller) (+ position 1)))))
(define controller
'(start
(assign n 3)
loop
(test zero?)
(branch done)
(assign n sub1)
(goto loop)
done
(halt)))
(extract-labels controller 0))- 출력
- —
- 값
- —
- 진단
- —
The first program returns ((start . 0) (loop . 1) (done . 5)). The second returns ((assign n 3) (test zero?) (branch 5) (assign n sub1) (goto 1) (halt)).
Watch position advance only for instruction lists, then locate assoc lookups for done and loop before branch and goto receive 5 and 1. The resulting list is assembled data, not an executed machine run.
힌트를 보기 전에 프로그램을 바꿔 보세요.
Insert a label named check immediately before (test zero?). Predict its table entry and whether any existing instruction position changes.
힌트 하나 보기
A label names the next instruction but does not occupy an instruction position.