0

So let's say that we have two registers $s0 and $s1. Register $s0 can take values from 0 to 31 and is used to show how many bits register $s1 will be shifted left. How can i do this without using sll or srl. I know that left logical shift by n bits is the same as multiplying with 2^n. This exercise gives a hint that this can be done with 4 instructions only. Both registers are 32-bit.

3
  • 4 instructions, or 4 lines of code? I.e., are loops allowed?
    – Michael
    Sep 27, 2015 at 20:56
  • @Michael 4 instructions. It says nothing about loops.
    – CharisAlex
    Sep 27, 2015 at 21:00
  • How many bits does it take to store values between 0 and 31? How does each bit affect the shift value, and how does that affect the result? Think this might be related to the four-instruction implementation?
    – Davislor
    Sep 27, 2015 at 22:41

1 Answer 1

1

You can add $s1 to itself $s0 times (each time it will multiply its value by 2 which shifts its bits left once).

The code would look like this:

 beqz $s0, end
loop:
  add $s1, $s1, $s1
  subu $s0, $s0, 1
  bnez $s0, loop  
end:

The first conditional branch is to ensure no action is taken to $s1 when $s0 holds value zero.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.