user201535
user201535

Reputation: 107

Translating a mips pseudo instruction 'rol'

I'm trying to translate the mips pseudo instruction rol (rotate left). I need to translate it before I can submit an assignment, which is unfortunate because the pseudo instruction worked quite well for me.

The instruction was,

      rol   $s4,$s4, 1  #goes to the left most bit (which as it says, gets the left most bit)

the way I translated it was,

       lui $s4, 0x8001     
       ori $t0, $s4, 0x0004
       srl $s4, $t0, 31
       sll $s5, $t0, 1
       or $s5, $s5, $s3

but it completely messes up my code, can someone please help me translate it correctly? Or tell me what I'm doing wrong?

Thank you in advance,

Upvotes: 0

Views: 10065

Answers (3)

p_sutherland
p_sutherland

Reputation: 491

Here is a UT-Dallas lecture on Shift and Rotate MIPS instructions/pseudoinstructions.

Slide Nine pulls back the curtain on the rol/ror pseudoinstruction.

Behind the scenes there are a pair of shifts (srl and sll) which move relevant bits into their final desired locations and fills non-relevant bit positions with 0's, and then an or to recombine them with bits rotated as desired.

In your case you want to rotate all bits one position to the left. To create this behavior we can use three MIPS instructions:

 # rol $s4, $s4, 1    # expanded as a pseudo-instruction

 srl $at, $s4, 1   #shift original word 1 bit to the right
 sll $s4, $s4, 31  #shift original word 31 bits to the left
 or  $s4, $s4, $at #Combine two fragments of words and place result in source register

It needs one temporary register; the other shift result can be written to the eventual destination, using it to hold the other temporary. This uses $at (the "assembler temporary) for thefirst, like other pseudo-instructions such as blt $reg, $reg, target. Writing the destination second avoids destroying it, in case it's also one of the inputs.

Upvotes: 3

Nihilus
Nihilus

Reputation: 31

Actually 'rol/ror' is only pseudo-instrations for ISAs before mips32r2 / mips64r2. Release 2 added hardware support for these instructions.

You can always look at rol32 / ror32 in bitops.h in Linux for a C implementation and then dissasemble it.

Upvotes: 1

Alexis Wilke
Alexis Wilke

Reputation: 20818

It looks like you swapped quite a few things.

The instruction format is:

inst rd, rs1, rs2

So your code should be:

lui $s4, 0x8001     
ori $s4, $t0, 0x0004
sll $s5, $s4, 1
srl $s4, $s4, 31
or $s4, $s5, $s4

Also you use the srl which is good. That one ignores the sign. Note, however, that I first shift to s5, then shift $s4.

Also I save the result in $s4 since your rol would do that (and not save in $s5.)

Also, to properly simulate your rol, you most certainly should use $t1 (register 1, temporary) and not $s5 unless you want to save $s5 on your stack and then restore it...

Upvotes: 0

Related Questions