

Here is my snippet,
Not16(in=x, out=notx);
Inc16(in=notx, out=out);
Ideally it should give me a 2's compliment , but I am getting a zero, I am not sure why? I am using built in ones.

Administrator

I thought I had replied to this the day you posted it. Apparently not. My apologies.
I need to see more of your code to have a chance of seeing what is going on.
When you say you are getting zero, do you mean that regardless of what the value of x is that you are getting a value of zero for out?
What are you using this for? You don't need anything like this for the ALU.


I just added it to the "parts" portion of the standard template of the alu program. Nothing else. Yes, for any value of zero, like till not operation, it gives me the right value, but as soon as I increment it using INC16 , it gives me 0. I tried using add16, writing my own chips as suggested in the course rather than using the builtin ones, still the same output. Well, I was going to use this as an output for the negation of x or y in the alu, am I wrong somewhere with that? On Fri, May 28, 2021 at 10:34 PM WBahn [via Nand2Tetris Questions and Answers Forum] < [hidden email]> wrote:
I thought I had replied to this the day you posted it. Apparently not. My apologies.
I need to see more of your code to have a chance of seeing what is going on.
When you say you are getting zero, do you mean that regardless of what the value of x is that you are getting a value of zero for out?
What are you using this for? You don't need anything like this for the ALU.


Administrator

I don't understand what you mean by "any value of zero" or "like till not operation".
If you set x=0 and pass that through the Not gate, you will get all 1's (which is the two's complement representation of 1). If you then increment this, you will get all 0's (since the leading one is lost). But this is what you should get, since (0) = 0.
But you don't need to implement arithmetic negation this way. Your ALU can selectively invert any combination of the inputs and outputs, right?
Look at the table of ALU control signals and the operations that result. One of those operations is x and another is y. Look at the effect of putting the control signals in those states and walk through what happens to your signal from input to output.

Administrator

Also, if you just added these to the rest of the ALU operation, then I'm guessing you have more than one part driving the 'out' signal, which is not allowed.


Pardon me, I meant for any value I enter , I get a 0. I enter a value, it goes in the not16 gate, gives me the expected value, say x=10, I put it in not16 gate, I get 11. Then I put the output of the not16 gate to a inc16, which yeilds a zero, for the said input, I am not sure why. On Sat, May 29, 2021, 1:06 AM WBahn [via Nand2Tetris Questions and Answers Forum] < [hidden email]> wrote:
Also, if you just added these to the rest of the ALU operation, then I'm guessing you have more than one part driving the 'out' signal, which is not allowed.

Administrator

Are ANY other parts in your ALU tied to the 'out' signal?
What happens if you change the name of the signal at the output of your Inc16 to something else and look at it?
Feel free to post your entire code. We can delete it once we are finished discussing it and figuring out what is happening.


Here is my entire code:
CHIP ALU {
IN
x[16], y[16], // 16bit inputs
zx, // zero the x input?
nx, // negate the x input?
zy, // zero the y input?
ny, // negate the y input?
f, // compute out = x + y (if 1) or x & y (if 0)
no; // negate the out output?
OUT
out[16], // 16bit output
zr, // 1 if (out == 0), 0 otherwise
ng; // 1 if (out < 0), 0 otherwise
PARTS:
Not16(in=x, out=notx);
Inc16(in=notx, out=out);
// Put you code here:
}
I am only handling the input and output as of now in this case, so I take in x, do the "not", then do the "inc", which according to my understanding should be a 2's compliment, here is my output with one of the vaues:

Administrator

I don't see anything wrong.
Are you stepping the simulator after you enter the value for 'x'? I'm guessing that you are, since your Time is showing as 1 and I think you have to do this to get the internal pins updated.
Are you POSITIVE that you are using the builtin chips? Look in the directory that your ALU.hdl is in and be certain that there is no Inc16.hdl in there.


My bad, I thought I had deleted the chips, but I had deleted the test file of inc16 instead of the HDL file, it seems to work fine after that, thanks a bunch

Administrator

Good. Glad we got that figured out.
Now, be sure that you understand why you don't need to include a circuit to take the two's complement additiveinverse. It will make your ALU implementation a LOT simpler.


much thanks for your help, tho i am not sure how can I find the negative of x or y without doing a 2s compliment, is there something i am missing , ? On Sat, 29 May 2021 at 10:57 PM, WBahn [via Nand2Tetris Questions and Answers Forum] < [hidden email]> wrote:
Good. Glad we got that figured out.
Now, be sure that you understand why you don't need to include a circuit to take the two's complement additiveinverse. It will make your ALU implementation a LOT simpler.


Administrator

There's more than one way to take the additive inverse other than negating all of the bits and then adding one to the result.
The authors give you the way to do it with their ALU architecture.
Look at the ALU truth table in Figure 2.6. Do you see that it can generate x, y, xy, and yx?
All you need to do is configure the control signals as shown by that table.
You don't even need to understand why it does the magic it does, but it is worth the effort to do so.
The key is to note that the approach you are using is the following:
x = ~x + 1
But this can be rewritten as
~x = x  1

