Please view the following code snippets:
int& sum(int& num1, int& num2) {
num1++;
num2++;
}
00000000 <_Z3sumRiS_>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: e8 fc ff ff ff call 4 <_Z3sumRiS_+0x4> // why here is a jump
8: 05 01 00 00 00 add $0x1,%eax
// why 0x8, my understanding is there are in total 3 parameters
// num2 -- 0xc(%ebp), num1 -- 0x8(%ebp), this -- 0x4(%ebp)
// am I right????
d: 8b 45 08 mov 0x8(%ebp),%eax
10: 8b 00 mov (%eax),%eax
12: 8d 50 01 lea 0x1(%eax),%edx // what the heck is this?
15: 8b 45 08 mov 0x8(%ebp),%eax
18: 89 10 mov %edx,(%eax)
1a: 8b 45 0c mov 0xc(%ebp),%eax
1d: 8b 00 mov (%eax),%eax
1f: 8d 50 01 lea 0x1(%eax),%edx
22: 8b 45 0c mov 0xc(%ebp),%eax
25: 89 10 mov %edx,(%eax)
27: 90 nop
28: 5d pop %ebp
29: c3 ret
I need to figure out the meaning of every single line of it, kinda confused me.
3: e8 fc ff ff ff call 4 <_Z3sumRiS_+0x4>
This isn't the real destination of the call, it is something will be filled in by linker. If you run objdump -dr sum.o
, you will find it is actually a call to __x86.get_pc_thunk.ax
. Same for the following add
, to set up a pointer to the GOT. (This function doesn't need one but you compiled without optimization, with -fpie
on by default.)
For more details, take a look Why does gcc generates strange code without flag -fno-pie?
System V i386 ABI, Section 2.2.2 tells the structure of a stack frame.
So your stack frame looks like this:
0xc | num2 |
0x8 | num1 |
0x4 | return address |
0x0 | previous %ebp | <-- %ebp
For remaining instructions, here is a step-by-step analysis.
// as num1 and num2 are references, they represents address in assembly
d: 8b 45 08 mov 0x8(%ebp),%eax // load num1 to %eax
10: 8b 00 mov (%eax),%eax // load *num1 to %eax
12: 8d 50 01 lea 0x1(%eax),%edx // put *num1 + 1 into %edx
15: 8b 45 08 mov 0x8(%ebp),%eax // load num1 to %eax
18: 89 10 mov %edx,(%eax) // save *num1 + 1 at num1
1a: 8b 45 0c mov 0xc(%ebp),%eax // same as above
1d: 8b 00 mov (%eax),%eax
1f: 8d 50 01 lea 0x1(%eax),%edx
22: 8b 45 0c mov 0xc(%ebp),%eax
25: 89 10 mov %edx,(%eax)