2012-04-08 26 views
0

我是Scheme語言的初學者,所以我無法編寫一個過程來獲取n位數並將其放入ALU中。 ALU應該使用1位ALU構建。算法中的ALU-n過程

這裏是1位ALU:

(define ALU1 
    (lambda (sel a b carry-in) 
    (multiplexor4 sel 
        (cons (andgate a b) 0) 
        (cons (orgate a b) 0) 
        (cons (xorgate a b) 0) 
        (multiplexor2 sub 
           (full-adder a b carry-in) 
           (full-adder a (notgate b) carry-in))))) 

其中,隨着多路複用器和全加器,工作原理。

這是我在使用一對夫婦的程序來模擬n位ALU嘗試:

(define ALU-helper 
    (lambda (selection x1 x2 carry-in n) 
    (if (= n 0) 
     '() 
     (ALU1 (selection x1 x2 carry-in))))) 

(define ALUn 
    (lambda (selection x1 x2 n) 
    (ALU-helper (selection x1 x2 c n)))) 

而且當它這樣做,它應該採取2 n位數字,並將它們添加,或減去等,按照「選擇」。這將是輸入:

(define x1 '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)) 
(define x2 '(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)) 
(ALUn 'add x1 x2 32) 

而且我運行它時看到由於「選擇」參數而發生錯誤。我確信我只是被所有參數弄糊塗了,但我不確定如何解決這個問題並讓ALU工作。我正在使用Dr.Retet程序,即R5RS語言運行此程序。

+0

你會得到什麼錯誤?你應該在你的問題('multiplexor4','multiplexor2','full-adder'等)中發佈所有相關的程序,使其成爲[SSCCE](http://homepage1.nifty.com/algafield/sscce。 HTML)。 – 2012-04-08 15:01:19

回答

0

通過在ALU-helper內部將參數放在ALU1的參數旁邊,您要求選擇被視爲一個函數,並且只將1個參數傳遞給ALU-helper。請嘗試:

(ALU1 selection x1 x2 carry-in)))) 

對ALUN中的ALU-helper的調用同樣的事情。

+0

謝謝,我會嘗試 – aclark 2012-04-08 19:06:33