Search code examples
assemblymips

How does sll in MIPS makes calculations easier compared to direct mult and div?


I am trying to understand how SLL will help me perform complex calculations in MIPS.

For example, how is, where I have to keep track of bits:

sll $t1, $t0, 3    // essentially $t1 = $t0 * 8

Better than (other than the extra assignment):

addi $t2,$zero,8
mult $t1, $t0, $t2      

Solution

  • Biggest reason is simple: It's much faster to bit shift than to multiply. This is true for pretty much every CPU. That's the only part that's "easier" really, it's harder to write using bit shifts but is more efficient than a mult or div. Even with optimizations off, most C compilers will use bit shifts and adds rather than hardware multiplication.

    Keep in mind, however, that there is still the possibility for signed overflow when left-shifting. If, in your first example, $t0 contained 0x7FFFFFFF,then you'd end up with 0xFFFFFFF8 or -8 in $1 after shifting three times.