4

Hey so I am kinda new here and to RISC-V.

One of my exercise questions is:

Rotate right by 4 bits the value of 0x0000000000000123. The expected result is 0x3000000000000012, i.e. all hexadecimal digits move right by one position while the rightmost one moves to the front*

So far, I learned a little bit about the Logical Operations: andi, or, and xori. In my previous exercises I learned add, addi, sub, slli, srli, srai.

I started off with:

addi x6, 0x, 0x123

However, I am stuck here. My textbook doesn't really describe things properly so any assistance is much appreciated!

phuclv
  • 37,963
  • 15
  • 156
  • 475
amiteur
  • 71
  • 1
  • 4
  • I'd suggest using a bit mask to extract the bits you need to wrap around, and using shifts to accomplish the end goal. – Ungeheuer Sep 16 '19 at 01:35
  • 1
    There's no single instruction to do this, but you can use several to do it. Consider: how would you accomplish this in C, as C also does not have rotate operators? If you can solve it in C you can transcribe that to assembly. – Erik Eidt Sep 16 '19 at 01:40
  • Thank you for the replies. Yeah your suggestions helped a lot in my problem. I haven't taken C courses, only learned Java so far. I masked the bits, and shifted it left, shifted the other remaining bits to the right, then added the two registers to my target register. – amiteur Sep 16 '19 at 02:12
  • Does this answer your question? [How do I write rotation Operation for the Risc-V(Assembly Language) Do we have any command for it like we have have in 8086?](https://stackoverflow.com/questions/55394123/how-do-i-write-rotation-operation-for-the-risc-vassembly-language-do-we-have-a) – maxschlepzig Feb 09 '20 at 16:38

1 Answers1

1

Here's what I ended up doing:

addi x30, x0, 0x123
andi x29, x30, 0x00f //masking all, but the right most hexadecimal digit
slli x6, x29, 60 //shifting this digit all the way to the left (rotating)
srli x7, x30, 4 //shifting the original value 4 bits right
add x5, x6, x7 //adding the two regs
Elliott de Launay
  • 1,027
  • 13
  • 31
  • 3
    The masking with `andi x29, x30, 0x00f` is superfluous because you shift-left those bits out of the register in the next line, anyways. – maxschlepzig Feb 09 '20 at 16:42