Click to view CodeAnalysis
Use gcc -g example.c -o example -m32
instructions to generate a 32-bit assembly on a 64-bit machine gdb example
, and then use the instructions to enter the GDB debugger.
After entering, set a breakpoint at the main function, run again, use disassemble
the instructions to get the assembly code, and use i(info) r(registers)
the instructions to see the values of each register:
The stack base of the main function is 0xffffcf98
x(examine)
view values in memory address with directives %esp
: 0, %ebp
: 0
The call instruction adds the address of the next instruction to the stack, at which point the value of the %esp
%ebp
stack is:
Put the base address of the previous function into the stack, starting with the current %esp
base:
Prepare for the transfer of the parameters first:
The calculation of the arguments is %eax
performed in:
Assembly code for the F function:
Actual argument into the stack:
The call command will put the address of the next instruction into the stack:
Calculate Short+int:
pop %ebp
The instruction pushes the stack top to %ebp
medium and %esp
adds 4 bytes:
The RET instruction will reload the stack top to %eip
:
Because the function f has been modified %esp
, it is restored with the leave command. The leave instruction will first %esp
go to it and %ebp
then reload the top of the stack to %ebp
:
Main function assembly code:
GDB Debug Assembler Stack Process analysis