% Written assignment 1 % - Exercise 1 alias 2.2 in [HC1] % adder3(A2,A1,A0, B2,B1,B0, C3,C2,C1,C0) % Each of , and represent binary numbers A,B,C % where it holds that A+B=C. adder3(A2,A1,A0, B2,B1,B0, C3,C2,C1,C0):- halfadder(A0,B0, X0,C0), fulladder(A1,B1,X0, C1,X1), fulladder(A2,B2,X1, C2,C3). % subtract(C3,C2,C1,C0, B2,B1,B0, A2,A1,A0) % Each of , and represent binary numbers A,B,C % where it holds that C-B=A (which is the same as A+B=C ;-). subtract(C3,C2,C1,C0, B2,B1,B0, A2,A1,A0):- adder3(A2,A1,A0, B2,B1,B0, C3,C2,C1,C0). %%%%%%%%%%%%% PROGRAM TEXT AS GIVEN IN FILE circuits.txt %%%% Notice that order of the arguments in haldadder/4 and fulladder/5 are %%%% not smart - but I kept here the order given (notice that in chapter 9, the order is different, sic!) not(0, 1). not(1, 0). and(0, 0, 0). and(0, 1, 0). and(1, 0, 0). and(1, 1, 1). xor(0, 0, 0). xor(0, 1, 1). xor(1, 0, 1). xor(1, 1, 0). or(0, 0, 0). or(0, 1, 1). or(1, 0, 1). or(1, 1, 1). halfadder(A, B, Carry, Sum):- and(A, B, Carry), xor(A, B, Sum). fulladder(A, B, Carryin, Sum, Carryout):- xor(A, B, X), and(A, B, Y), and(X, Carryin, Z), xor(Carryin, X, Sum), or(Y, Z, Carryout).