dud3
dud3

Reputation: 419

Gdb: gcc -O0 assmbly exmple

Hopefully not a silly question.

Compiled without specifying optimization: gcc test.c -o test (which seem to chose -O0).

gcc -O2 or -O3 output much cleaner (at least it seems to me) assembly code than -O0.

What's the reason for -O0, how does it help us, I can't see that it's simpler than -O1 or -O2.

...
int sum(int x, int y)
{
    int sum = x + y;
    return sum;
}
...

0x00000000004004ed <+0>:     push   %rbp
0x00000000004004ee <+1>:     mov    %rsp,%rbp
0x00000000004004f1 <+4>:      mov    %edi,-0x14(%rbp)
0x00000000004004f4 <+7>:      mov    %esi,-0x18(%rbp)
0x00000000004004f7 <+10>:    mov    -0x18(%rbp),%eax
0x00000000004004fa <+13>:    mov    -0x14(%rbp),%edx
0x00000000004004fd <+16>:    add    %edx,%eax
0x00000000004004ff <+18>:     mov    %eax,-0x4(%rbp)
0x0000000000400502 <+21>:   mov    -0x4(%rbp),%eax
0x0000000000400505 <+24>:   pop    %rbp
0x0000000000400506 <+25>:   retq   

Upvotes: 0

Views: 153

Answers (1)

fuz
fuz

Reputation: 93044

With optimizations turned off, there is a 1:1 representation between source code and machine code, allowing for easier debugging. With optimizations turned on, the compiler can do strange things like rearranging code or getting rid of variables that make debugging the code much harder.

Compiling with -O0 is also typically faster as the optimizer is usually the slowest component of every modern compiler.

Upvotes: 3

Related Questions