MIPS multiplication problem
In MIP开发者_C百科S, how can I tranlate this expression to MIPS?
y = 2x+3z
(x,y,z are variables)
I use multi $t0,$s0, 2 # $s0 stores x and $s1 stores y.
to show 2x
. Is that correct?
Assuming that:
x is in $s0
z is in $s1
y will be in $s2
add $s2, $s0, $s0 # x*2 in $s2
add $t0, $s1, $s1 # z*2 in $t0
add $t0, $t0, $s1 # z*3 in $t0
add $s2, $s2, $t0 # x*2+z*3 in $s2
We don't actually multiply (it's slower than simple addition) and as you can see we destroy temporary register $t0 but don't touch $s0/$s1
精彩评论