Search code examples
cassemblyx86-64decompiling

x86-64 Assembly "cmovge" to C code


While I shouldn't list out the entire 4 line sample I'm given, (since this is a homework question) I'm confused how this should be read and translated into C.

cmovge %edi, %eax

What I understand so far is that the instruction is a conditional move for when the result is >=. It's comparing the first parameter of a function %edi to the integer register %eax (which was assigned the other parameter value %esi in the previous line of assembly code). However, I don't understand its result.

My problem is interpreting the optimized code. It doesn't manipulate the stack, and I'm not sure how to write this in C (or at least the gcc switch I could even use to generate the same result when compiling).

Could someone please give a few small examples of how the cmovge instruction might translate into C code? If it doesn't make sense as its own line of code, feel free to make something up with it.

This is in x86-64 assembly through a virtualized Linux operating system (CentOS 7).


Solution

  • I'm probably giving you the whole solution here:

    int doit(int a, int b) { return a >= b ? a : b; } With gcc -O3 -masm=intel becomes:

    doit:
    .LFB0:
        .cfi_startproc
        cmp edi, esi
        mov eax, esi
        cmovge  eax, edi
        ret
        .cfi_endproc