In: Computer Science
The C language permits a variable to be left- or right-shifted by a nonconstant amount (e.g. i >> j where i and j are variables), but the MIPS instruction set only supports shifts by a constant value (e.g. i >> 2). In words rather than code, describe how a variable-length right shift could be performed using MIPS instructions.
The MIPS instructions support the shifts operation by a constant value only. In C language the right shift is indicated by the symbol >>. In C, a variable can be left- or right-shifted by a variable value. In MIPS, a variable can be left- or right-shifted by a constant value. Example: i>>2. Here, the bits of the value i is right shifted by 2 positions.
The syntax of this instruction is given below:
srlv $d, $t, $s
This instruction shifts the value in register $t to the right with the number of bits specified in the register $s and stores the result in register $d.
Consider the example, the following instruction shifts the value $t2 with 2 bits to the right, and places the result in $t3:
li $t1, 2 #load register $t1with 2
li $t2, 9422 # load register $t1with 9422
#$t2 = 0000 0000 0000 0000 0010 0100 1100 1110(in binary)
srlv $t3, $t2, $t1 # shifts the value $t2 with 2 bits to the right, and places the result in $t3
#$t3 = 0000 0000 0000 0000 0000 1001 0011 0011