diff --git a/examples.csv b/examples.csv index 5948693..b7830ef 100644 --- a/examples.csv +++ b/examples.csv @@ -6,3 +6,4 @@ and.nor,4. and halfadd.nor,5. half add fulladd.nor,6. full add add.nor,7. 4bit add +sub.nor,8. 4bit sub diff --git a/examples/sub.nor b/examples/sub.nor new file mode 100644 index 0000000..9576d1b --- /dev/null +++ b/examples/sub.nor @@ -0,0 +1,54 @@ +# 8. 4bit sub +function not(a) + return a nor a +end + +function or(a, b) + return not(a nor b) +end + +function and(a, b) + return not(a) nor not(b) +end + +function halfadd(a, b) + c = and(a, b) + s = and(or(a, b), not(c)) + return [c, s] +end + +function fulladd(a, b, x) + cs1 = halfadd(a, b) + cs2 = halfadd(cs1[1], x) + c = or(cs1[0], cs2[0]) + s = cs2[1] + return [c, s] +end + +function add(a, b) + x = 0 + s = [0, 0, 0, 0] + for i = 3 to 0 step -1 + cs = fulladd(a[i], b[i], x) + x = cs[0] + s[i] = cs[1] + next + return s +end + +function complement(a) + s = [0, 0, 0, 0] + for i = 0 to 3 + s[i] = not(a[i]) + next + return add(s, [0, 0, 0, 1]) +end + +function sub(a, b) + return add(a, complement(b)) +end + +a = [1, 0, 1, 0] # 10 +b = [0, 1, 1, 1] # 7 +print complement(a) +print sub(a, b)