cancel
Showing results for 
Search instead for 
Did you mean: 

RRX in C

oneohm
Associate
Posted on December 27, 2009 at 19:46

RRX in C

3 REPLIES 3
oneohm
Associate
Posted on May 17, 2011 at 13:35

I'm coding a program in which I want to load a register with a binary value and then drive a MOSFET with the binary value. I want to Rotate Right by one bit and then carry the dropped off bit to the MSB. Through some research found that the Cortex M3 instruction set supports the operation using the RRX assembler instruction but doesn't appear to carry.

I would like to use the function of RRX with carry in C and can't seem to find any equivelant C function. I have fiddled a bit with the REGISTER>>1 to shift the bit one position but looking for an easy way to carry the bit to the MSB.

st3
Associate II
Posted on May 17, 2011 at 13:35

This really sounds like something that need to be done in assmebler.

In 'C', you would have to save the LSB, do the shift, then put the saved LSB into the MSB...

tomas23
Associate II
Posted on May 17, 2011 at 13:35

Add to your project an assembly file with such function exported, see the cortex_m3.s or similar files, exporting some useful assembly functions, or use inline assembler, if allowed by compiler.

Instead of RRX, what about BFI? I guess that this instruction can spare you one additional, that needs to shift the Carry back to some register.

Try this (assuming std. ASM function called from C):

EXPORT __BFI

__BFI:

BFI r0, r0, #7, #1

BX lr

In C code do something like this:

u32 __BFI(u32 param);

u32 variable = __BFI(register);

assuming you added the assembly file to your project.