I am new to reverse engineering and I am trying to figure out exactly what xor is doing in this little program. I know if I put any number other than 0 I get a xor eax,3 so if I put in 1 I get 2 if I input 2 I get 1 if I input 7 I get 4 I am just trying to understand why.
-
Hi Landon! First impression: you are stripping relevant parts needed for further inference. Basically it's an conditional xor (3/2).– knxCommented Mar 1, 2020 at 10:08
-
@kn0x yes I am just trying to figure out what exactly xor does. Is there more info you need to answer that question? I can get the function before the jne. But basically it's just checking if the int entered by the user == 0 if it is then it goes to the xor ecx,2 if not then it does xor eax,3 on whatever number the user entered.– ParziCommented Mar 1, 2020 at 19:40
-
1if(abc) {x = x^3;printf("%d\n",x)} else {x = x ^2;printf("%d\n",x)}– blabbCommented Mar 1, 2020 at 19:59
Add a comment
|
1 Answer
In C
, this function would look like this:
int fun()
{
int a;
// some code you haven't pasted here; probably scanf("%d", &a);...
if (some_condition)
a ^= 3; // xor a with 3
else
a ^= 2; // xor a with 2
printf("a = %d.\n", a);
return 0;
}
I cannot say anything more about it having only the snipped you shared with us. If there is some magic, it is contained in the part you haven't uploaded.
-
This is very helpful. I am just trying to understand what xor does exactly. I assume its the a ^= 3 part? I am not familier with ^= could you add that part into your answer and then I think you have answered my question. Thanks!– ParziCommented Mar 1, 2020 at 19:39
-
1Yes
a ^= 3
stands fora = a^3
, that isa xor 3
. I have updated my answer. Basically,xor
with2
changes second least significant bit to its negation, andxor
with3
changes both first and second least significant bit ofa
. They are just bit operations that act like sum modulo2
on each bit. You can read more aboutxor
here.– bart1eCommented Mar 1, 2020 at 19:43