MIPS中三个整数的乘法 [英] Multiplication of three integers in MIPS

查看:606
本文介绍了MIPS中三个整数的乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MIPS中乘以三个整数.我的第一个想法是将第一个和第二个相乘,然后将结果与第三个相乘(就像我对add所做的那样).但是结果在64位的HILOW中给出.那么如何将其乘以第三个因子呢?

I want to multiply three integers in MIPS. My first thought was to multiply the first and the second one, and after that the result with the third one (like I did it with add). But the result is given in HI and LOW in 64-bit. So how can I multiply it with the third factor?

和: 32位整数* 32位整数= 64位整数.理论上会得到什么:

And: 32-bit integer * 32-bit integer = 64-bit integer. What (in the theory) would give that:

32位整数* 32位整数* 32位整数= ??

96位? 128?

感谢您的提示.

推荐答案

将n位数字与m位数字相乘会生成(n + m)位数字. 因此,将三个32位数字相乘可得出96位乘积

Multiplying an n-bit number with an m-bit number produces a (n+m) bit number. So multiplying three 32-bit numbers produces a 96-bit product

假设三个数字分别是a,b和c,我们得到以下结果

Suppose the three numbers are a, b and c, we have the following result

  a*b  =   ABH*2³² + ABL
c(a*b) = c(ABH*2³² + ABL)
       = c*ABH*2³² + c*ABL
       = (CABH_H*2³² + CABH_L)*2³² + (CABL_H*2³² + CABL_L)
       = CABH_H*2⁶⁴ + (CABH_L + CABL_H)*2³² + CABL_L

其中,H和L是结果的高部分和低部分,分别存储在HI和LO寄存器中.在这里,将2 32 和2 64 的乘法分别替换为左移32和64.

where H and L are the high and low parts of the result which are stored in the HI and LO registers respectively. Here the multiplications by 232 and 264 will be replaced by shifting left by 32 and 64 respectively.

因此在MIPS32中,如果a,b和c存储在$ s0,$ s1和$ s2中,我们可以进行如下计算

So in MIPS32 if a, b and c are stored in $s0, $s1 and $s2 we can do the math as below

multu   $s0, $s1        # (HI, LO) = a*b
mfhi    $t0             # t0 = ABH
mflo    $t1             # t1 = ABL

multu   $s2, $t1        # (HI, LO) = c*ABL
mfhi    $t4             # t4 = CABL_H
mflo    $s7             # s7 = CABL_L

multu   $s2, $t0        # (HI, LO) = c*ABH
mfhi    $s5             # s5 = CABH_H
mflo    $t3             # t3 = CABH_L

addu    $s6, $t3, $t4   # s6 = CABH_L + CABL_H
sltu    $t5, $s6, $t3   # carry if s6 overflows
addu    $s5, $s5, $t5   # add carry to s5
# result = (s5 << 64) | (s6 << 32) | s7

结果存储在元组($ s5,$ s6,$ s7)中

The result is stored in the tuple ($s5, $s6, $s7)

在MIPS64中,事情要简单得多

Things will be much simpler in MIPS64:

mul(unsigned int, unsigned int, unsigned int):
        multu   $4,$5
        dext    $6,$6,0,32
        mfhi    $3
        mflo    $2
        dins    $2,$3,32,32
        dmultu  $6,$2
        mflo    $3
        j       $31
        mfhi    $2

这是您可能需要对签名的操作进行一些修改

You may need some slight modification for signed operations

这篇关于MIPS中三个整数的乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆