Miharu461 Miharu461 - 9 months ago 61
C Question

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
to the integer register
(which was assigned the other parameter value
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
switch I could even use to generate the same result when compiling).

Could someone please give a few small examples of how the
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).

Answer Source

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:

    cmp edi, esi
    mov eax, esi
    cmovge  eax, edi