简体   繁体   English

用汇编语言减去 16 位 2 的补码数

[英]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.我正在尝试构建一个计算机芯片,类似于在 nand2tetris 上发现的 Add16 芯片,它减去 16 而不是添加它。 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:以此类推,数字每一步都在变大,达到 16 个。期望的结果如下:

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

Any help that can be given will be much appreciated!任何可以提供的帮助将不胜感激!

There are a couple of formulas for AB. AB有几个公式。

One is A + ~B + 1.一个是 A + ~B + 1。

Another is ~(~A + B).另一个是〜(〜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 ) 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";对于专用功能单元,第一个公式更好,因为您可以“免费”获得 +1; you only need a slightly modified 16 bit adder and a 16 bit not unit.您只需要一个稍加修改的 16 位加法器和一个 16 位非单元。 I will leave it to you to figure out how it's done.我会把它留给你弄清楚它是如何完成的。

Have fun!玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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