简体   繁体   中英

Subtracting 16-bit 2's compliment numbers in assembly language

I am trying to build a computer chip, similar to the Add16 chip found on nand2tetris, that subtracts 16 rather than adds it. I keep on running into incorrect results however. Can someone help me?

Chip Sub16 {
IN a[16], b[16];
OUT out[16];

PARTS:
Not16(in=b, out=subB);
Add16(a=a, b=notB, out=out);
}

I have also tried this version too:

Not(in=b[0], out=out0);
FullAdder(a=a[0], b=out0, c=false, sum=out[0], carry=c1);
Not(in=b[1], out=out1);
FullAdder(a=a[1], b=out1, c=c1, sum=out[1], carry=c2);

...

and so on and so forth, the numbers getting bigger with each step, going up to 16. The desired results are as follows:

显示 a、b 和减法输出的所需结果

Any help that can be given will be much appreciated!

There are a couple of formulas for AB.

One is A + ~B + 1.

Another is ~(~A + B). This latter is the one used by the HACK ALU (see https://b1391bd6-da3d-477d-8c01-38cdf774495a.filesusr.com/ugd/56440f_2e6113c60ec34ed0bc2035c9d1313066.pdf )

For a dedicated functional unit, the first formula is the better one since you can get the + 1 "for free"; you only need a slightly modified 16 bit adder and a 16 bit not unit. I will leave it to you to figure out how it's done.

Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM