Multiplication by a power of 2 using Logical shifts in MIPS assembly

Shifting a number n bits left multiples the number by 2n. For example n << 3 = n*2³ = n*8. The corresponding instruction is

SLL $s1, $s2, 1

To multiply any number you can split the number into sums of power of 2s. For example:

  • n*10 = n*8 + n*2 = (n << 3) + (n << 1) SLL $t1, $s2, 1 SLL $t2, $s2, 3 ADD $s2, $t1, $t2

You can also use a subtraction if it’s faster

  • n*15 = n*16 - n = (n << 4) - n SLL $t1, $s2, 4 SUB $s1, $t1, $s2

Leave a Comment