Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How is this function's assembly implementing the conditional?

Ok, I’m just dumb. Sorry 😀


The following code,

int foo(int);
int bar(int);

int foobar(int i) {
    int a = foo(i);
    int b = bar(i);
    return a == b ? a : b;
};

with GCC trunk is compiling to this assembly:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

foobar(int):
        push    rbx
        mov     ebx, edi
        call    foo(int)
        mov     edi, ebx
        pop     rbx
        jmp     bar(int)

Here is it live.

This TU has no clue what i it will be given, and it has no clue what foo and bar will return, so it has no clue whether a == b will be true or false. So it must
inspect the outputs of the two calls and somehow compare them to decide which one it shuld return.

But I can’t see that in the assembly. What am I missing?

>Solution :

return a == b ? a : b; is same as return a == b ? b : b; is same as return b;.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading