[Back]

5.1 一个寄存器机器模拟器

5.1

自行模拟~

5.2

(data-paths
    (registers
        ((name res)
            (buttons ((name res<-mult (source (operation mult)))))
         (name n)
            (buttons ((name n<-plus1 (source (operation plus1)))))))
    (operations
        ((name mult)
            (inputs (register n) (register res)))
        ((name plus1)
            (inputs (register n) (constant 1)))
        ((name =)
            (inputs (register n) (constant cnt)))))
(controller
    test-n
        (test =)
        (branch (label fac-done))
        (res<-mult)
        (n<-plus1)
        (goto (label test-n))
    fac-done)

5.3

(data-paths
    (registers
        ((name guess)
            (buttons ((name guess<-ave) (source (operation ave)))))
        ((name x)))
    (operations
        ((name squ)
            (inputs (register guess)))
        ((name -)
            (inputs (operation squ) (register x)))
        ((name <)
            (inputs (operation -) (constant eps)))
        ((name /)
            (inputs (register x) (register guess)))
        ((name ave)
            (inputs (register guess) (operation /)))))
(controller
    test-guess
    (test (op <) (op -) (constant eps))
    (branch (label sqrt-done))
    (assign guess (op ave) (reg guess) (op /))
    (goto (label test-guess))
    sqrt-done)

5.4

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

(controller
    (assign val (const 1))
    test-cnt
    (test (op =) (reg cnt) (const 0))
    (branch (label exp-done))
    (assign val (op *) (reg val) (reg b))
    (assign cnt (op -) (reg cnt) (const 1))
    (goto (label test-cnt))
    exp-done)

5.5

请自行模拟

5.6

Fib-loop 中的save n和after-fib-1中的restore n可以删去