如何将两个 32 位寄存器组合成一个 64 位答案?

发布于 2024-10-03 17:53:51 字数 236 浏览 7 评论 0原文

我正在使用 pcsim 程序,我相信它使用 MIPS。我并不乐观,因为我对汇编语言很陌生。我需要仅使用加法和移位来乘以两个 32 位数字,并将乘积存储在两个寄存器中。如果结果可以存储在 32 位中,我已经知道它可以成功地将两个数字相乘。问题是,如果数字大于该数字,我无法弄清楚如何将产品的右半部分与左半部分组合起来。左半寄存器应保存从 2^32 开始一直向上的值。如果这还不清楚,我可以尝试解释更多。有没有一些我忽略的简单方法来实现这一点?感谢您的任何帮助。

I am using the pcsim program which I believe uses MIPS. I'm not positive as I am very new to assembly language. I need to multiply two 32-bit numbers using only add and shift and store the product in two registers. I have gotten it to where it will successfully multiply two numbers IF the result can be stored in 32 bits. The problem is that if the number is larger than that, I can't figure out how to combine the right half of the product with the left half. The left half register should hold values starting at 2^32 all the way up. If this isn't clear I can try to explain more. Is there some easy way I am overlooking to accomplish this? Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2024-10-10 17:53:51

如果我没理解错的话,你就陷入了实际上需要进行 64 位算术的境地,对吧?

如果您正在进行典型的移位加法二进制长乘法,则可以从 32 位运算构建一些 64 位移位和加法原语,然​​后使用相同的方法。

以下是一些 C 片段示例(如果您实际使用的是 MIPS,那么转换为 MIPS 应该很简单)。我假设您正在使用无符号 32 位数字并想要无符号 64 位结果。

逻辑左移 1 位:

tmp = lo >> 31;  /* top bit of lo to bottom bit of tmp, rest of tmp is 0 */
lo <<= 1;
hi <<= 1;
hi |= tmp;

逻辑右移 1 位:(

tmp = hi << 31;  /* bottom bit of hi to top bit of tmp, rest of tmp is 0 */
hi >>= 1;
lo >>= 1;
lo |= tmp;

实际上可以将 131 替换为 n( 32 - n) 移动其他位数)

64 位加法:(

result_lo = a_lo + b_lo;
result_hi = a_hi + b_hi;
if (result_lo < a_lo)
    result_hi++;

参见 此处了解更多详细信息,具体参考 MIPS)。


另一种方法是将每个 32 位输入视为一对 16 位“数字”;两个 16 位数字相乘最多得出 32 位结果。所以基本的想法是这样的:(

0x12345678 * 0x23456789 =     0x5678 * 0x6789
                          + ((0x1234 * 0x6789) << 16)
                          + ((0x5678 * 0x2345) << 16)
                          + ((0x1234 * 0x2345) << 32)

你仍然需要一些 64 位的补充)。

If I've understood correctly, you're stuck at the point where you actually have a need to do 64-bit arithmetic, right?

If you're doing a typical shift-and-add binary long multiplication, you could build some 64-bit shift and addition primitives from 32-bit operations, then use the same method.

Here are some examples as C fragments (should be trivial to translate to MIPS, if that's what you're actually using). I'm assuming that you're working with unsigned 32-bit numbers and want unsigned 64-bit results.

Logical shift left 1 bit:

tmp = lo >> 31;  /* top bit of lo to bottom bit of tmp, rest of tmp is 0 */
lo <<= 1;
hi <<= 1;
hi |= tmp;

Logical shift right 1 bit:

tmp = hi << 31;  /* bottom bit of hi to top bit of tmp, rest of tmp is 0 */
hi >>= 1;
lo >>= 1;
lo |= tmp;

(in fact you can replace the 1 and 31 with n and (32 - n) to shift by some other number of bits)

64-bit addition:

result_lo = a_lo + b_lo;
result_hi = a_hi + b_hi;
if (result_lo < a_lo)
    result_hi++;

(see here for more detail about that, with specific reference to MIPS).


An alternative approach is to treat each of your 32-bit inputs as a pair of 16-bit "digits"; multiplying two 16-bit numbers gives at most a 32-bit result. So the basic idea is like this:

0x12345678 * 0x23456789 =     0x5678 * 0x6789
                          + ((0x1234 * 0x6789) << 16)
                          + ((0x5678 * 0x2345) << 16)
                          + ((0x1234 * 0x2345) << 32)

(you'll still need some 64-bit additions).

奈何桥上唱咆哮 2024-10-10 17:53:51

没有办法将这两半“组合”成一个 32 位寄存器。如果你想在内存中将两半组合成一个 64 位值,则需要根据机器的字节顺序将两半分开存储。如果您使用 SPIM,它似乎使用与您的主机相同的年份。

X86?小端。先存放下半部分。
竞价排名?大尾数。先存放上半部分。

There is no way of "combining" the two halves into one 32-bit register. If you want to combine the two halves into one 64-bit value in memory, you need to store both halves besides eachother according to the endianess of your machine. If you are using SPIM, it appears it uses teh same enianness ass your host computer.

X86? Small endian. Store the lower half first.
PPC? Big endian. Store the upper half first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文