Hi there. Simple question. I'm looking for some pure C or C++ code that does what the RCR code below in ASM does. Is this possible? Perhaps a simple function "int RCR(int value, int rotateNr)"?

_asm

{

mov eax,val1

mov cl,al

rcr eax, cl

mov val1,eax

}

The function isn't that simple, the output depends on the starting value of the carry flag.

It is really a rotate of 33 bits, 31..0 and -1, only when the low byte of the starting

dword is a multiple of 33 will the dword result be the same with either a carry starting value of 0,1. Otherwise the dword result will differ by one bit.

The function produces an outgoing side effect, the value of the carry flag.

Without accounting for the carry flag, it is hard to determine what would be a correct

result. Perhaps a 1 bit random function to load a simulated carry.

It is really a rotate of 33 bits, 31..0 and -1, only when the low byte of the starting

dword is a multiple of 33 will the dword result be the same with either a carry starting value of 0,1. Otherwise the dword result will differ by one bit.

The function produces an outgoing side effect, the value of the carry flag.

Without accounting for the carry flag, it is hard to determine what would be a correct

result. Perhaps a 1 bit random function to load a simulated carry.

There have been proposed some rotation operators to C but as for now, no major compiler supports them. So the easiest, but of course not the best, way to add ror to our code is to define some clever macros.

Decent compilers with optimizations turned on should compile "ROR(x);" to simple ror instruction (at least Visual C++ 2008 detects these kinds of code of produces single 'ror'). Adding the support for carry flag is not trivial. But you can define a variable which will emulate the carry flag:

This way it'll work on machines that don't have the carry flag.

And then you use it like:

I haven't tested it so there may be errors.

`#define ROR(x) x=((((unsigned)x)>>1)|((x&1)?(1<<((sizeof(x)*8)-1)):0))`

Decent compilers with optimizations turned on should compile "ROR(x);" to simple ror instruction (at least Visual C++ 2008 detects these kinds of code of produces single 'ror'). Adding the support for carry flag is not trivial. But you can define a variable which will emulate the carry flag:

`#define RCR(x, c) x=((((unsigned)x)>>1)|((c&1)?(1<<((sizeof(x)*8)-1)):0)|((c=(x&1))&0))`

This way it'll work on machines that don't have the carry flag.

And then you use it like:

`int a=7;`

ROR(a);

// ...

char b=3, c;

c = initial_value_for_our_carry;

RCR(b, c);

// here our carry contains modified value

I haven't tested it so there may be errors.

For any given dword input value there are 63 possible dword output values.

32 with a starting carry 1,

32 with a starting carry 0,

1 the same as the original dword value, the carry has no impact on the result.

32 with a starting carry 1,

32 with a starting carry 0,

1 the same as the original dword value, the carry has no impact on the result.