__ROL__ & __ROR__
Hello You. In today’s blog I’m going to explain what are ror
and rol
instructions.
Although the shift operations and the rotate operations seems similar but the difference is that the shifted bits are gone completely but in the case of rotation the bits are only rotated.
ROL
stands for Rotate Left, it simply rotate bits towards the left sideROR
stands for Rotate Right, and it rotates bit towards the right side :3
Let’s see there implementation in C
:
// Rotate Left (ROL) function
unsigned char ROL(unsigned char value, unsigned char shift) {
shift &= 7; // Ensure shift is in the range [0, 7]
// or shift %= 8;
return (value << shift) | (value >> (8 - shift));
}
// Rotate Right (ROR) function
unsigned char ROR(unsigned char value, unsigned char shift) {
shift &= 7; // Ensure shift is in the range [0, 7]
return (value >> shift) | (value << (8 - shift));
}
You can also use the defs.h
IDA defs header by including it in your c
code, but make sure to compile your c
code as c++
using g++
since defs.h
is a c++
header file.
#include <stdio.h>
#include "defs.h"
int main() {
int res = __ROR1__(3, 1337 & 7);
return 0;
}
you can find the defs.h
here
and Python
:
def ror(n, c, bits=64):
mask = (1 << bits) - 1
return ((n >> c) | (n << (bits - c))) & mask
def rol(n, c, bits=64):
return ror(n, bits - c, bits)
There is also a RotateLeft
and RotateRight
in the z3
library in python
Reversing Engineering ROR & ROL
In reverse engineering, when you encounter a value that has been rotated left (ROL) or rotated right (ROR), you can undo these operations by performing the opposite rotation.
That’s it for today, thanks for reading :3