ALU Help

classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

Tarsis Lima
Snowkel, thanks for posting this link (http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/file/n95834/alu_worksheet.pdf).

It was really helpul!
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

lnorman917
In reply to this post by ybakos
Hi, ybakos. I am working on the ALU as well and came across this thread and want to follow along to see if I can come to an understanding as well.

My question here is if we negate 1110, wont we we get 0010, which would be 2? In the book it says you can keep the least significant 1 and then invert the rest of the bits OR flip all the bits and then add 1. Either way I end up with 0010 and the output is supposed to be 0001.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
In the context of the ALU's implementation, "negation" means Boolean (bitwise) negation, not arithmetic negation.

From ALU.hdl:
// Implementation: the ALU logic manipulates the x and y inputs
// and operates on the resulting values, as follows:
// if (zx == 1) set x = 0        // 16-bit constant
// if (nx == 1) set x = !x       // bitwise not
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

lnorman917
So am I confusing boolean negation with -x to x which would be arithmetic negation?
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
Yes, "!x" and "~x" are use in the book for Boolean negation, which is also called "not".  "-x" is used for arithmetic negation.

So the ALU is to compute -x, where x = -2:

The control bits are set (zx, nx, zy, ny, f, no) = (0, 0, 1, 1, 1, 1)
x = 1...1110 = -2
zx=nz=0 so x is unchanged.
y = dont-care
zy=1 sets y = 0...0000
ny=1 sets y = not(y) = 1...1111 = -1 decimal
f=1 selects add: sum = -2 + -1 = -3 = 1...1101
no=1 sets out = not(sum) = 0,,,0010 = 2

--Mark

Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

lnorman917
Oh that makes complete sense. So basically Boolean negation is just inverting each bit?

Also, when you said "So the ALU is to compute -x, where x = -2:" did you mean:

 "So the ALU is to compute , !x where x = -2:"? Just based on what you said in the first part of your reply "-x" is used for arithmetic negation which is not what we want. Just want to get everything perfectly clear and thanks for all your help!
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
Should have been an "if" in there:
"So [if] the ALU is to compute -x, where [the input value of] x = -2"

I meant that the value of 'out' was to be the arithmetic computation -x.  This is what the ALU would be computing for the the Hack assembly language command D=-D, for example.

The steps that follow show how using NOT with addition can result in the arithmetic negative operation, turning -2 input value to +2 output value.

The ALU worksheet will help you understand this.

When you are doing this worksheet, remember that nx, ny and no = 1 means NOT.

(Hint: You ALU.hdl will Use Not16 chips as part of the nx, ny and no processing.)

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

lnorman917
Ok, I was just misunderstanding what the ALU does. After your explanations I can definitively see what we are trying to accomplish. Thanks You for all the very helpful replies!
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

HeinzWW
In reply to this post by ybakos
It's incredible. Today (year 2021) I'm looking for the same problem, I solved line 2 in the worksheet with wrong solution. You must know I'm a systems programmer for mainframes in retirement, and I'm familiar with all kind of bit manipulations. But I run in the pitfall to resolve after thinking one second "1111" + "1111" is "0000" with carry bit ignored. It's very awkward.
12