0

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.

enter image description here

  • Hi Landon! First impression: you are stripping relevant parts needed for further inference. Basically it's an conditional xor (3/2). – kn000x Mar 1 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. – Landon Call Mar 1 at 19:40
  • 1
    if(abc) {x = x^3;printf("%d\n",x)} else {x = x ^2;printf("%d\n",x)} – blabb Mar 1 at 19:59
5

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.

| improve this answer | |
  • 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! – Landon Call Mar 1 at 19:39
  • 1
    Yes a ^= 3 stands for a = a^3, that is a xor 3. I have updated my answer. Basically, xor with 2 changes second least significant bit to its negation, and xor with 3 changes both first and second least significant bit of a. They are just bit operations that act like sum modulo 2 on each bit. You can read more about xor here. – bart1e Mar 1 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.