2
votes

I'm learning how to do MIPS and i'm confused on multiplication. Let's say I was converting the following C Code into MIPS.

c = b + a*3 

with a,b,c stored in registers $s1,$s2,$s3 respectively. How am I supposed to write a*3 in MIPS? Is there an addi type instruction for multiplication or do I store 3 in a temporary register and use the mult operator like this?

addi $t0, $zero, 3
mult $s1,$t0

If so how do I get the final value or product of this operation to complete my C code?

1
The 64-bit product is in the lo and hi registers. Use the mflo and mfhi instructions to get their values (or just mflo if you're only interested in the 32 least significant bits). Consult a MIPS instruction set reference for more info on those instructions. - Michael

1 Answers

3
votes

MIPS instruction set doesn't provide a mult instruction with immediate value. Hence you would have to store the value into a temporary register and then use mult instructions. The result of the mult instruction would be stored in lo and hi registers giving the lower 32-bit of the result and upper 32-bit of the result respectively.