I'm having difficulties trying to understand the ALU and writing the hdl for it. I've tried filling out this chart but I know I'm doing something wrong due to my lack of understanding.
http://nand2tetrisquestionsandanswersforum.32033.n3.nabble.com/file/n95834/alu_worksheet.pdf Here is my attempt at trying to fill in the first box. http://imgur.com/a/c5oRr Can someone point me in the right direction? 
You're on the right track. There's just a mistake in your addition, which is causing the confusion.
1111 + 1111  1110 Now negate that, and what do you get? 
Thanks for clearing up the addition for me. That certainly helps a bit!
The 1 output was simple enough to figure out, but I'm having a little trouble on the x output though. Here's my thought process: zx = 0, this means that x (0100) is not zeroed. nx = 0, x is not negated zy = 1, y is zeroed (0000) ny = 1, y is negated (1111) x+y=0, x (0100) and y (1111) are not added So does this mean I simply ignore y? Leaving me with the desired output x (0100). 
Review the ALU spec. What does the last f bit mean when it is 1 or 0?
You can do it! 
When f is 1, then out = x + y. When f is 0, then out = x & y. In the book, it says that this means it's a bitwise And. Not entirely sure I understand that concept. Would this mean that the out is both x (0100) and 1 (1111)? But the resulting out on the chart shows that it's just x?

When f is 0, then take x AND y ("bitwise AND" is a logical AND, it is the AND you know).
When f is 1, then take x plus y (arithmetic sum). What is x now? 0100 What is y now? 1111 f = 0, so x AND y 0100 AND 1111 = ? 
So I understand that it's using the logic gate AND but I'm a bit lost on what exactly 0100 AND 1111 mean. Should I be thinking about it like this?
http://imgur.com/a/TinNm If so, I still don't understand how 0100 AND 1111 gives us the output of just x. Apologies if I'm not seeing the obvious. 
This is where the "bitwise" comes in.
If you know what 0 AND 1 are, what is 00 AND 11? What about 01 AND 10? 
I seem to be lacking some basic understanding. I don't know what 0 AND 1 are.

I bet you do.
Consider an AND gate. Do you know how it behaves? Consult a truth table. 0 AND 1 are the same thing, where 0 is an "off" input and 1 is "on" input into an AND gate. 
This post was updated on .
I drew up a truth table for the AND gate and I think I'm starting to get what you mean by 0 AND 1 (or at least I hope I do). If one of the inputs is 0 and the other 1, then the out is 0.
http://imgur.com/a/Zb0O7 Therefore meaning that 1 AND 0 = 0? I'm not sure how to expand upon this logic to reach 00 AND 11. But since it would require both of the inputs to be "on" for 00 AND 11 = 11, I'm guessing that 00 (off) AND 11 (on) = 00 ? Edit: I tried searching through the book for any more info on the bitwise AND but the book doesn't give any additional details other than the fact that this chip has already been built. I was able to fill out the rest of the worksheet except for the sections where the f bit is 0. 
Administrator

"Bitwise" means one bit at a time, just like the And16.hdl chip that you made in project 1.
Use the 1bit AND operation for each bit position in the inputs to generate the corresponding output bit. 11011010 AND 10010111 = 10010010: 1 1 0 1 1 0 1 0 AND 1 0 0 1 0 1 1 1  1 0 0 1 0 0 1 0Mark 
Hey Mark, I think I finally got it. The way you formatted the numbers really helped!
0 AND 1 = 0 00 AND 11 = 00 01 AND 10 = 00 Also, I'm having a little trouble converting negative binary numbers to their decimal value in a 4 bit system. I know that 0101 = (1*2^0)+(0*2^1)+(1*2^2)+(0*2^3) = 5 but this method does not work for binary numbers larger than 7. 
Administrator

What you have just learned is the maximum representable signed 4bit number. The range for 4bit signed numbers is 8 – 7. Mark 
I'm starting to have a better grasp of the the ALU conceptually after filling out the worksheet but how do I apply these concepts into writing the actual hdl code? I'm a bit lost as to where to go from here.
My first thought was that since there are 8 inputs (x[16], y[16], zx, nx, yz, ny, f, no) it would use multiple mux chips? But I'm almost certain this is incorrect and am scratching my head trying to figure how to build upon this. 
Administrator

See this post, Getting started with ALU. Mark 
I'm still having trouble wrapping my head around this. I'm lost at syntax usage, what chips to use, and how to even start. My first instinct is to use mux for the ifthen statements but I don't know how to incorporate all of the inputs into my code. I also have no idea what the zr and ng outputs mean in context of this whole thing other than zr being True if out=0 and ng being true if out < 0.

Administrator

These posts might be helpful.
Getting started with ALU ALU Trouble in starting To begin with ignore the 'zr' and 'ng' status outputs. Get your ALU to pass the ALUnostat.tst. Once your ALU is properly computing the 'out' value, then you can concentrate on 'zr' and ng'. Think about how to break the ALU into smaller conceptual blocks. For instance, 'x', 'zx' and 'nx' are used to generate one of the inputs to the And16 and Add16. 'y', 'zy', and 'ny' similarly generate the other input to the And and Add. Function processing and output processing can also be thought about as individual blocks. For 'zr' think about what the bits need to be for a 16bit number to be 0 and how you might detect that. It might be easier to to think about how you can detect that a number is not 0. For 'ng', what nakes a 2'scomplement number negative? Mark 
In reply to this post by snowkel

This post was updated on .
In reply to this post by cadet1620
Thanks for the links, they were helpful. I tried thinking about it as blocks and tried writing my hdl step by step like how the worksheet had certain steps to it. I emailed you a copy of my hdl file, I hope that's okay. I'm not sure about the 'zr' part but as for the 'ng' bit, I believe that that you'd have to flip the bits and add one to it.
Edit: Just saw your post ybakos. Messaged. Edit2: Just got my code to finally pass the ALU.tst! I want to thank ybakos and Mark for going to great lengths to help me understand this stuff. You guys have no idea how much of a difference it made. 
Free forum by Nabble  Edit this page 