1

I'm making n-bit ALU in scheme and I have a 1-bit ALU so far. Can someone tell me how to approach this problem? Here are the instructions for it:

The format of the procedure is

  (ALUn selection x1 x2 n)    

where the first parameter, selection can take any of the values: 'add, 'sub, 'and, 'or, or 'xor. The second and third parameters x1 and x2 are two n-bit binary numbers in list format.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Regina
  • 11
  • 2

1 Answers1

1

Here's the code for and, or xor:

(define (ALUn selection x1 x2 n)
  (cond
    [(or (not (= (length x1) n)) (not (= (length x2) n))) (error "Wrong register length!")]
    [(equal? selection 'and) (map bitwise-and x1 x2)]
    [(equal? selection  'or) (map bitwise-ior x1 x2)]
    [(equal? selection 'xor) (map bitwise-xor x1 x2)]))

You should figure how to do add and sub yourself!

Racket Noob
  • 1,056
  • 1
  • 8
  • 16