Login  Register

Re: Figuring out zr

Posted by cadet1620 on Jan 25, 2011; 2:51pm
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/Figuring-out-zr-tp2326310p2330292.html

mbm29414 wrote
That's what I thought, but the code in the file from Google Code seemed to indicate you could.

Here's the Google Code section:
-----------------------------------------------------

/** out2 is the same as the value passed to out
 *   ng1 is the same as the value passed to ng
 */
Add16(a[0..15]=out2, b[0..15]=true, out[0..15]=out3, out[15]=out315);
Not(in=ng1, out=ngcomp);
And(a=ngcomp, b=out315, out=zr);
So, in the code above, the output value is added to '1111111111111111'. Then, the result is stored into a 16-bit bus and the left-most digit is stored into a 1-bit bus. (Side question: As far as I can tell, out3 is never used; is it only "produced" because you have to have a "full" out before grabbing part of it? Could you not just leave out the "out[0..15]=out3" part?) Then, ng1 (the left-most digit of the output value) is negated and zr is set to ngcomp*out315. This doesn't make sense to me, but I was thinking that these posts are from the authors.
The above circuit does work, but is extremely inefficient; Add16 is huge and slow! Here's what the circuit does:
          out2 = out         (given)
           ng1 = ng          (given)
               = (out < 0)
        out315 = (out-1 < 0)
               = (out < 1)
        ngcomp = ! ng1
               = (out >= 0)
            zr = (out >= 0) & (out < 1)
               = (out = 0)
As you noticed, 'out3' is unused and is not required.

Your final solution is an efficient solution. Other than signal names it's the same as mine.

--Mark