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?
And: 32-bit integer * 32-bit integer = 64-bit integer. What (in the theory) would give that:
32-bit int * 32-bit int * 32-bit int = ??
96-bit? 128?
Thanks for your hints.
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
Suppose the three numbers are a, b and c, we have the following result
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.
So in MIPS32 if a, b and c are stored in $s0, $s1 and $s2 we can do the math as below
The result is stored in the tuple ($s5, $s6, $s7)
Things will be much simpler in MIPS64:
Here's some sample assembly output from GCC on Godbolt
You may need some slight modification for signed operations