Reputation: 1257
So I understand somewhat what the command sll is doing, as I read this and its pretty much just shifting all the bits left by 1. I'm just wondering why would I do this?
I have an assignment from class with an example of it... Where $s6 and $s7 is the base address for an array and $s1/$s2 are just some variables.
sll $t0, $s0, 2
add $t0, $s6, $t0
sll $t1, $s1, 2
add $t1, $s7, $t1
...
Why shift a bit? What is it doing in simple terms? My first thought it has something to do with indexing the variables in the array...but I'm not sure.
Upvotes: 3
Views: 55487
Reputation: 58437
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.
Upvotes: 7
Reputation: 1
I see in the program there is simple sll applied on $s0 and $s2 registers! In instruction sll $t0, $s0, 2, the value of register $s0 will be multiplied by 4. You can clear more at Shift left logical example
Upvotes: 0
Reputation: 22585
Shifting a number one bit to the left is the same as multiplying that number by 2. More generally, shifting a number N bits to the left is the same as multiplying that number by 2^N.
It is widely used to compute the offset of an array, when each element of the array has a size that is a power of 2.
Upvotes: 2