Why shift a bit using sll and such in MIPs Assembly?

its pretty much just shifting all the bits left by 1

The example they showed was a shift by 1 bit. The sll instruction isn’t limited to just shifting by 1 bit; you can specify a shift amount in the range 0..31 (a shift by 0 might seem useless, but SLL $zero, $zero, 0 is used to encode a NOP on MIPS).

A logical left shift by N bits can be used as a fast means of multiplying by 2^N (2 to the power of N). So the instruction sll $t0, $s0, 2 is multiplying $s0 by 4 (2^2) and writing back to $t0. This is useful e.g. when scaling offsets to be used when accessing a word array.

Leave a Comment