Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this assembly code generated complete?

I wrote this simple C code and compiled it using Visual Studio 2010, with assembler output.

int main(){
    int x=1;
    int y=2;
    int z=x+y;
    return 0;
}  

And this is the assembly output..

; Listing generated by Microsoft (R) Optimizing Compiler Version 16.00.40219.01 

    TITLE   foobar.cpp
    .686P
    .XMM
    include listing.inc
    .model  flat

INCLUDELIB OLDNAMES

EXTRN   @__security_check_cookie@4:PROC
PUBLIC  _main
; Function compile flags: /Ogtp
; File foobar.cpp
;   COMDAT _main
_TEXT   SEGMENT
_main   PROC                        ; COMDAT

; 2    :    int x=1;
; 3    :    int y=2;
; 4    :    int z=x+y;
; 5    :    return 0;

    xor eax, eax

; 6    : }

    ret 0
_main   ENDP
_TEXT   ENDS
END

Is this complete? I do not see any ADD statement. What compiler can be used to compile it?

like image 324
questions Avatar asked Dec 14 '25 15:12

questions


1 Answers

Since your code doesn't do anything with those values, the compiler has optimized most of it out. As Carl mentioned, all that remains is the xor eax, eax which is zeroing eax, the register that the return value is placed.

Even if you were to printf("%d", z), your result z is a compile-time constant (3), and that is all you would see in the assembly listing.

What you can do is disable optimizations in your project C++ properties, and you should see your expected assembly. Also, building in Release mode should minimize the extra debug stuff you see in the asm.

like image 195
Jonathon Reinhart Avatar answered Dec 16 '25 11:12

Jonathon Reinhart