Re: ALU, negationx, y, add16 off by one?
Posted by
cadet1620 on
Dec 11, 2011; 5:16pm
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/ALU-negationx-y-add16-off-by-one-tp3564869p3577682.html
While rogerdodger91 was writing:
Whats wrong with my ALU basically theoretically speaking.
I emailed him:
The Hack ALU is a very clever design. It's not obvious why you don't need to use 2's complement negation to do subtraction. Here's why it works to use binary NOT.
First, the definition of 2's complement gives:
-x = NOT(x) + 1
-x - 1 = NOT(x)
The x-y op code for the ALU is
zx=0 nx=1 zy=0 ny=0 f=1 no=1
so the output is:
out = NOT( NOT(x) + y )
out = NOT( (-x - 1) + y )
out = NOT( y - x - 1 )
out = -(y - x - 1) - 1
out = -y + x + 1 - 1
out = x - y
--Mark