问题
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,
回答1:
Here is a UT-Dallas lecture on Shift
and Rotate
MIPS instructions/pseudoinstructions: https://www.utdallas.edu/~dodge/EE2310/lec14.pdf
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:
srl $t1, $s4, 1 #shift original word 1 bit to the right
sll $t2, $s4, 31 #shift original word 31 bits to the left
or $s2, $t1, $t2 #Combine two fragments of words and place result in source register
Let me know if this is unclear or I misunderstood your purpose.
回答2:
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.
回答3:
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...
来源:https://stackoverflow.com/questions/24542657/translating-a-mips-pseudo-instruction-rol