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
}
Posted on 2008-08-26 12:58:43 by pn0k
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.
Posted on 2008-08-26 20:36:28 by dsouza123
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.

#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.
Posted on 2008-08-27 10:42:41 by ti_mo_n
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.
Posted on 2008-08-27 20:28:21 by dsouza123