...
/A Disassembly Project with Function and Stack
A Disassembly Project with Function and Stack
Learn about disassembled code for function and stack and discuss about parameter mismatch problems.
We'll cover the following...
We'll cover the following...
Project disassembled code with comments
Here is the commented code disassembly of main with memory addresses removed for visual clarity.
push %rbp # establishing stack framemov %rsp,%rbpsub $0x20,%rsp # creating stack frame for local variables# and function parametersmov %edi,-0x14(%rbp) # saving the first main parametermov %rsi,-0x20(%rbp) # saving the second main parametermov $0x1,%esi # setting the second parameter for# arithmetic functionmov $0x1,%edi # setting the first parameter for arithmetic# functioncallq 0x55555555514d <_Z10arithmeticii>mov %eax,-0x4(%rbp) # setting the result local variablemov $0x0,%eax # main should return 0leaveq # restoring the previous stack pointer# and stack frame, equivalent to# mov %rbp, %rsp# pop %rbpretq # return from main
Below is the commented disassembly of the arithmetic function, with memory addresses removed for visual clarity.
push %rbp # establishing stack framemov %rsp,%rbpmov %edi,-0x4(%rbp) # saving the first arithmetic parameter (a)mov %esi,-0x8(%rbp) # saving the second arithmetic parameter (b)mov -0x4(%rbp),%eax # (a) -> eaxadd %eax,-0x8(%rbp) # eax + (b) -> (b)addl $0x1,-0x4(%rbp) # 1 + (a) -> (a)mov -0x8(%rbp),%eax # (b) -> eaximul -0x4(%rbp),%eax # (a) * eax -> eaxmov %eax,-0x8(%rbp) # eax -> (b)mov -0x8(%rbp),%eax # (b) -> eaxpop %rbp # restoring the previous stack frame# no need to restore stack pointer as# it didn’t changeretq # result value is in eax
We can put a breakpoint on the first arithmetic calculation’s address and examine the raw stack data pointed to by the %RBP register:
gcc FunctionParameters.cpp Arithmetic.cpp -o FunctionParameters
gdb ./FunctionParameters
After executing and loading the program, we get into the GDB container:
We create the breakpoint of the programs with the break main command:
break main
The breakpoint ...