Search code examples
assemblyreverse-engineering

Conditional Flags Assembly - cmp, test


I'm having a hard time understanding conditional code in assembly. The assembly on the right is for funA() on the left, but I'm having trouble with lines 3-4 in assembly.

Here is my thought process:

cmp rax, rcx // a[idx] <= *b

However, the actual if statement in the code is the exact opposite. I know it has something to do with how in assembly, the conditional executed is the reverse...therefore it makes a[idx] > *b instead. Does this have to do with "jle"? Would someone mind explaining this to me?

enter image description hereenter image description here


Solution

  • If you look at .L1 and .L2, the JIT compiler has just decided to reverse the order - it's put the else code first, and reversed the condition. The jle is "jump if less than or equal" so it's become the equivalent of the C#:

    if (a[idx] <= *b)
    {
        *b = *b + *b;
    }
    else
    {
        *b = a[idx];
    }