I'm trying to understand what will be present on the stack during a function call.
As far as I have learnt, arguments to the callee (if any), the return address of the caller and the base address would be pushed on the stack before calling another function.
So, I wrote a simple C program
#include <stdio.h>
void
foo()
{
}
int
main()
{
foo();
return 0;
}
and the corresponding dis-assembled machine code is
08048334 <foo>:
8048334: 55 push %ebp
8048335: 89 e5 mov %esp,%ebp
8048337: c9 leave
8048338: c3 ret
08048339 <main>:
8048339: 55 push %ebp
804833a: 89 e5 mov %esp,%ebp
804833c: 83 ec 08 sub $0x8,%esp
804833f: 83 e4 f0 and $0xfffffff0,%esp
8048342: b8 00 00 00 00 mov $0x0,%eax
8048347: 83 c0 0f add $0xf,%eax
804834a: 83 c0 0f add $0xf,%eax
804834d: c1 e8 04 shr $0x4,%eax
8048350: c1 e0 04 shl $0x4,%eax
8048353: 29 c4 sub %eax,%esp
8048355: e8 da ff ff ff call 8048334 <foo>
804835a: b8 00 00 00 00 mov $0x0,%eax
804835f: c9 leave
8048360: c3 ret
8048361: 90 nop
8048362: 90 nop
8048363: 90 nop
While foo()'s code makes sense, I couldn't understand main's() code. Why is that there are so many operations? I was only expecting the following operations inside main()
1. Push the frame pointer
2. Call foo (which will inturn save the return address)
Can someone please explain me main()'s code? Thanks!