Skip to content

Commit

Permalink
add example sub
Browse files Browse the repository at this point in the history
  • Loading branch information
taisukef committed Jan 9, 2025
1 parent 6ea38b0 commit db8a5b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 54 additions & 0 deletions examples/sub.nor
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit db8a5b7

Please sign in to comment.