简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM