The CPU does not run C. Each line was turned into a few instructions. disassemble shows them; => marks the next one.
Instructions, registers and the return address
Level 13 of 13 · interactive GDB exercise · runs in your browser
Step one machine instruction at a time, read CPU registers, and find a function's return value and return address at the machine level.
stepi runs exactly one instruction. info registers shows the CPU's registers. A single one is written with a dollar sign: print $eax.
This is a 32-bit x86 machine: a function leaves its result in eax, and ret jumps to the address on top of the stack (esp).
Step through add one instruction at a time until ret is next, then look at what it leaves behind.
You work in a real GDB session on the program below. A breakpoint on add was set and the program was started; it is paused at the first line of add.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main(void) {
int sum = add(2, 3);
printf("sum = %d\n", sum);
return 0;
}
main.c, compiled with gcc -g -O0 -std=c11 -Wall -fno-omit-frame-pointer -fno-stack-protector -no-pie -fno-pie for 32-bit x86 Linux.
disassemblestepi(Enter)x/i $pcinfo registersprint $eaxx/a $espinfo framebreak *<addr>main. Type run; the breakpoint on add is still there, so you start again at the same place.eax before the add instruction has run. A register holds whatever the last instruction left in it. Check where you are with x/i $pc first.