Question

So my problem is one I though was rather simple and I have an algorithm, but I can't seem to make it work using thumb-2 instructions.

Amway, I need to reverse the bits of r0, and I thought the easiest way to do this would be to Logically shift the number right into a temporary register and then shift that left into the result register. However LSL, LSR don't seem to allow you to store the shifted bit that is lost to the Most significant bit or least significant bit(while also shifting the bits of that register). Is there some part of the instruction I am miss understanding.

This is my ARM reference: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0204j/Cjacbgca.html

Was it helpful?

Solution

The bit being shifted out can be copied into the C bit (carry flag) if you use the S suffix ("set flags"). And the RRX instruction uses C to set the bit 31 of the result. So you can probably do something like:

  ; 32 iterations
  MOV R2, #32
  ; init result
  MOV R1, #0
loop
  ; copy R0[31] into C and shift R0 to left
  LSLS R0, R0, #1
  ; shift R1 to right and copy C into R1[31]
  RRX  R1, R1
  ; decrement loop counter
  SUBS R2, #1
  BNE  loop
  ; copy result back to R0
  MOV R0, R1

Note that this is a pretty slow way of reversing bits. If RBIT is available, you should use it, otherwise check some bit twiddling tricks.

OTHER TIPS

How about using the rbit instruction? My copy of the ARMARM shows it having a Thumb-2 Encoding in ARMv6T2 and above.

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0489c/Cihjgdid.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top