Implement till 5.8
parent
82128a6445
commit
402776f481
|
@ -119,11 +119,11 @@
|
|||
(goto (label expt-loop))
|
||||
after-expt
|
||||
(restore continue)
|
||||
(assign val (op *) (reg b) (reg val)) ; val now contains n(n - 1)!
|
||||
(goto (reg continue)) ; return to caller
|
||||
(assign val (op *) (reg b) (reg val))
|
||||
(goto (reg continue))
|
||||
base-case
|
||||
(assign val (const 1)) ; base case: 1! = 1
|
||||
(goto (reg continue)) ; return to caller
|
||||
(assign val (const 1))
|
||||
(goto (reg continue))
|
||||
expt-done)))
|
||||
|
||||
(set-register-contents! expt-rec-machine 'b 3)
|
||||
|
|
|
@ -1,8 +1,59 @@
|
|||
(load "util.scm")
|
||||
(load "misc/sicp-regsim.scm")
|
||||
|
||||
(display "\nex-5.7 - test-machines\n")
|
||||
|
||||
(display "\nex-5.7\n")
|
||||
; We have already tested the machines in the previous exercises.
|
||||
|
||||
(display "[answered]\n")
|
||||
|
||||
(display "\nex-5.8\n")
|
||||
; continue at 5.2.2 - the assembler
|
||||
|
||||
(display "\nex-5.8 - double-here\n")
|
||||
|
||||
(define (extract-labels text receive)
|
||||
(define (warn-if-label-exists label labels)
|
||||
(if (assoc label labels)
|
||||
(begin
|
||||
(display "WARNING duplicated label -- ")
|
||||
(display label)
|
||||
(newline))))
|
||||
(if (null? text)
|
||||
(receive '() '())
|
||||
(extract-labels (cdr text)
|
||||
(lambda (insts labels)
|
||||
(let ((next-inst (car text)))
|
||||
(if (symbol? next-inst)
|
||||
(warn-if-label-exists next-inst labels))
|
||||
(if (symbol? next-inst)
|
||||
(receive insts
|
||||
(cons (make-label-entry next-inst
|
||||
insts)
|
||||
labels))
|
||||
(receive (cons (make-instruction next-inst)
|
||||
insts)
|
||||
labels)))))))
|
||||
|
||||
(define double-here-machine
|
||||
(make-machine
|
||||
'(a)
|
||||
'()
|
||||
'(start
|
||||
(goto (label here))
|
||||
here
|
||||
(assign a (const 3))
|
||||
(goto (label there))
|
||||
here
|
||||
(assign a (const 4))
|
||||
(goto (label there))
|
||||
there)))
|
||||
|
||||
(start double-here-machine)
|
||||
(assert (get-register-contents double-here-machine 'a) 3)
|
||||
|
||||
; The register contains 3 because the assembler jumps to the first label in the
|
||||
; list.
|
||||
|
||||
(display "\nex-5.9\n")
|
||||
|
||||
(display "\nex-5.10\n")
|
||||
|
|
Loading…
Reference in New Issue