Search⌘ K

GDB Disassembly Output – No Optimization

Explore how to use GDB to debug C/C++ programs compiled without optimization. Learn to set breakpoints, disable address randomization, inspect assembly instructions, and track changes in memory and registers to better understand program execution and disassembly output.

Arithmetic example in the C/C++ program

Let’s rewrite our arithmetic program in C/C++. Corresponding assembly language instructions are edited in the comments:

C++
int a, b;
int main(int argc, char* argv[])
{
// Assign a value to variable a
a = 1; // adr x0,a
// mov w1, #1
// str w1, [x0]
// Assign a value to variable b
b = 1; // adr x0,b
// mov w1, #1
// str w1, [x0]
// Add b and a and store in variable b
b = b + a; // adr x0,b
// ldr w1, [x0]
// adr x0, a
// ldr w0, [x0]
// add w1, w1, w0
// adr x0, b
// str w1, [x0]
// Increment variable a by one
++a; // adr x0, a
// ldr w1, [x0]
// add w1, w1, #1
// str w1, [x0]
// Multiply b with a and store into b
b = b * a; // adr x0,b
// ldr w1, [x0]
// adr x0, a
// ldr w0, [x0]
// mul w1, w1, w0
// adr x0, b
// str w1, [x0]
// results: [a] = 2 and [b] = 4
// Return 0 means it executed successfully
return 0;
}

The flowchart below shows the working of the source code:

Disassembly in the no-optimization mode

It is easier to track the progress of our program during debugging if we can recompile without optimization.

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

Compilation

If we compile and link the program in the no-optimization mode (default), as shown below, we get the ...