Skip to content

Commit

Permalink
First few exercises of chapter 5.
Browse files Browse the repository at this point in the history
  • Loading branch information
skilldrick committed May 4, 2012
1 parent f15bd68 commit 34caea5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ex5.02.scm
@@ -0,0 +1,10 @@
(controller
(assign p (const 1))
(assign c (const 1))
test-c
(test (op >) (reg c) (reg n))
(branch (label rem-done))
(assign p (op *) (reg p) (reg c))
(assign c (op +) (reg c) (const 1))
(goto (label test-c))
rem-done)
15 changes: 15 additions & 0 deletions ex5.03.scm
@@ -0,0 +1,15 @@
(controller
test-g
(test (op ge?) (reg g) (reg x))
(branch (label sqrt-done))
(assign g (op imp) (reg g))
(goto (label test-g))
sqrt-done)

(controller
test-g
(test (op <) ((op abs) ((op -) ((op sq) (reg g)) (reg x))))
(branch (label sqrt-done))
(assign g (op avg) (reg g) ((op /) (reg g) (reg x)))
(goto (label test-g))
sqrt-done)
31 changes: 31 additions & 0 deletions ex5.04.scm
@@ -0,0 +1,31 @@
;a

(controller
(assign continue (label expt-done))
expt-loop
(test (op =) (reg n) (const 0))
(branch (label base-case))
(save continue)
(assign n (op -) (reg n) (const 1))
(assign continue (label after-expt))
(goto (label expt-loop))
after-expt
(restore continue)
(assign val (op *) (reg b) (reg val))
(goto (reg continue))
base-case
(assign val (const 1))
(goto (reg continue))
expt-done)

;b

(controller
(assign p (const 1))
test-c
(test (op =) (reg n) (const 0))
(branch (label expt-done))
(assign counter (op -) (reg n) (const 1))
(assign p (op *) (reg b) (reg p))
(goto (label test-c))
expt-done)

0 comments on commit 34caea5

Please sign in to comment.