Intel x86 32-bit assembly – function call & stack frame

This post is originally written for a basic view on function call and stack frame in Intel x86 32-bit assembly code. EBP and ESP are the focus. A stack layout pic with dynamic register changes is also down there. May it help:)

/home/daveti/Ctest: cat assemblyTry.c
#include <stdio.h>

int myFunc( int a)
{
        int b;
        b = a + 1;
        return b;
}

int main()
{
        int stackInt1 = 0xffff;
        int stackInt2 = 0;
        stackInt2 = myFunc( stackInt1);
        return stackInt2;
}
========================================
/home/daveti/Ctest: gcc -o assemblyTry -ggdb -static assemblyTry.c
/home/daveti/Ctest: objdump -S -C -d -g -T -t -x -f assemblyTry > assemblyTry.objdump
/home/daveti/Ctest: vi assemblyTry.objdump

080481d0 <myFunc>:
#include <stdio.h>

int myFunc( int a)
{
 80481d0:       55                      push   %ebp
 80481d1:       89 e5                   mov    %esp,%ebp                /* save ebp and update ebp */
 80481d3:       83 ec 04                sub    $0x4,%esp                /* ‘grow’ 4 bytes in the stack for b */
        int b;
        b = a + 1;
 80481d6:       8b 45 08                mov    0x8(%ebp),%eax           /* pass stackInt1=+8(%ebp) to eax */
 80481d9:       40                      inc    %eax                     /* eax++ */
 80481da:       89 45 fc                mov    %eax,0xfffffffc(%ebp)    /* pass stackInt1+1 to b=-4(%ebp) */
        return b;
 80481dd:       8b 45 fc                mov    0xfffffffc(%ebp),%eax    /* return code for myFunc */
}
 80481e0:       c9                      leave                           /* mov %ebp,%esp; pop %ebp; – destory the stack frame before return from this func
ion call and set the ebp with the old ebp saved in the (top of) stack to restore the caller function’s stack */
 80481e1:       c3                      ret                             /* return to the caller function */

080481e2 <main>:
int main()
{
 80481e2:       55                      push   %ebp
 80481e3:       89 e5                   mov    %esp,%ebp                /* save ebp and update ebp with new esp */
 80481e5:       83 ec 08                sub    $0x8,%esp                /* ‘grow’ 8 bytes in the stack for stackInt1 and stackInt2 */
 80481e8:       83 e4 f0                and    $0xfffffff0,%esp         /* 4-byte alignment for esp */
 80481eb:       b8 00 00 00 00          mov    $0x0,%eax
 80481f0:       29 c4                   sub    %eax,%esp
        int stackInt1 = 0xffff;
 80481f2:       c7 45 fc ff ff 00 00    movl   $0xffff,0xfffffffc(%ebp) /* pass 0xffff to stackInt1; 0xfffffffc(%ebp)=-4(%ebp) */
        int stackInt2 = 0;
 80481f9:       c7 45 f8 00 00 00 00    movl   $0x0,0xfffffff8(%ebp)    /* pass 0x0 to stackInt2; 0xfffffff8(%ebp)=-8(%ebp) */
        stackInt2 = myFunc( stackInt1);
 8048200:       83 ec 0c                sub    $0xc,%esp                /* ‘grow 12 bytes in the stack for myFunc */
 8048203:       ff 75 fc                pushl  0xfffffffc(%ebp)         /* push stackInt1 (last parameter) into stack as (last) parameter of myFunc */
 8048206:       e8 c5 ff ff ff          call   80481d0 <myFunc>
 804820b:       83 c4 10                add    $0x10,%esp               /* ‘degrow’ 16 bytes in the stack */
 804820e:       89 45 f8                mov    %eax,0xfffffff8(%ebp)    /* pass return value b from eax to stackInt2 */
        return stackInt2;
 8048211:       8b 45 f8                mov    0xfffffff8(%ebp),%eax    /* return code for main */
}
 8048214:       c9                      leave                           /* destroy the stack frame of main */
 8048215:       c3                      ret                             /* return */
 8048216:       90                      nop
 8048217:       90                      nop

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Programming, Stuff about Compiler and tagged , , , , , , . Bookmark the permalink.

1 Response to Intel x86 32-bit assembly – function call & stack frame

  1. dsgs says:

    After research a number shopsellbags of of the weblog posts on your website now, and I really like your cheap Gucci handbags approach of blogging. I bookmarked it to my bookmark web site listing and will probably be checking back soon. Pls check out my cheap lv handbagssite as effectively and let me know what you think.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.