Search⌘ K
AI Features

GDB Disassembly Output—No Optimization

Explore how to use GDB to disassemble and debug a simple arithmetic program compiled without optimization. Learn to set breakpoints, disable address randomization, and analyze assembly output to better understand program execution and memory changes within the GDB environment.

Arithmetic program

Let’s rewrite our arithmetic program in way that works in both C and C++. Corresponding assembly language instructions are written in the comments:

C++
int a, b;
int main(int argc, char* argv[])
{
// Assign a value to variable a and b
a = 1; // movl $1, a
b = 1; // movl $1, b
// Add b and a and store in variable b
b = b + a; // mov a, %eax
// mov b, %edx
// add %edx, %eax
// mov %eax, b
// Increment variable a by one
++a; // mov a, %eax
// add $1, %eax
// mov %eax, a
// Multiply a with b and store into b
b = b * a; // mov b, %edx
// mov a, %eax
// imul %edx, %eax
// mov %eax, b
// results: (a) = 2 and (b) = 4
// Return 0 means it executed successfully
return 0;
}

Disassembly in no-optimization mode

It’s easier to track the progress of our program during debugging if we can recompile without optimization. The steps to initialize the GDB environment are below:

Note: You can practice all the commands in the coding playground provided at the end of the lesson.

Let’s compile and link the program in no optimization mode (default):

gcc
...