Search⌘ K

GDB Disassembly Output—Optimization

Explore how compiler optimization affects GDB disassembly output of a simple arithmetic C++ program. Learn to compile with optimization flags, use breakpoints, and interpret optimized assembly code to better understand code execution and memory changes.

Disassembly with code optimization

Source code

The source code of the arithmetic program is given below:

C++
int a, b;
int main(int argc, char* argv[])
{
a = 1;
b = 1;
b = b + a;
++a;
b = b * a;
return 0;
}

The flowchart below shows the working of the source code:

Flowchart of the arithmetic program
Flowchart of the arithmetic program

Compilation

We can add certain flags to optimize the code while compiling our source file. The flag -O is used for the optimization of code and -O1 is an optimization flag that further optimizes the code size and execution time. There are other flags for optimization, such as -O2, and -O3, which reduce the size and execution time of the code even further. Now, let’s ...