diff --git a/ex-5_01-06.scm b/ex-5_01-06.scm index 1bfc43c..c9deb40 100644 --- a/ex-5_01-06.scm +++ b/ex-5_01-06.scm @@ -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) diff --git a/ex-5_07-xx.scm b/ex-5_07-xx.scm index af52201..e7e8cee 100644 --- a/ex-5_07-xx.scm +++ b/ex-5_07-xx.scm @@ -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")