(Lispex)sicp.io
5.8 · 조립기 레이블

레이블 표는 이름을 명령 위치로 바꾼다.

조립기는 컨트롤러 텍스트를 훑고 실행 명령만 세어 기호 분기 대상을 조립된 명령 위치로 바꿀 수 있습니다.

생각해 볼 질문

기계가 숫자 위치를 필요로 하기 전까지 컨트롤러가 읽기 쉬운 레이블을 쓰려면 어떻게 해야 할까요?

  • 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.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(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))
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 524 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    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.