Shifting in MIPS using a formula

280 views Asked by At

Im learning MIPS and coming from a procedural programming background its proving difficult. This is a question i came across when it came to shifting. If there are any pointers in answering it, it would be appreciated.

Suppose that rt is a register that contains an integer. Explain why the instruction

sll $rd, $rt, h 

has the effective of putting

2^h ∗ rt 

in register rd

1

There are 1 answers

0
lostbard On

This doesn't have anything to do with prodecural programming but rather binary numbers.

Some examples using 8 bit numbers:

If you have the number 1 (decimal) - in binary that would be 0000 0001

If you shift left by 1, you move all the numbers up by 1, and insert a 0 into the new shift space

So now you would have binary 0000 0010 - or 2 decimal.

Shift left another gives 0000 0100 or 4 decimal.

So if you shift the original number 1 left by 2, you have 4.

Or looking at it as a pattern:

1 left shift = *2 (2^1)

2 left shift = *4 (2^2)

All of which looks like

2^h ∗ rt