The calling sequence and stack frame

When your function first gets control, the stack frame looks like:
  Caller's frame
  last arg
   
%esp+4 1st arg
%esp return address

To get an idea of how this can be used, let us say we have the following function:

   int isum(int N, int *v);

So, we want to sum up the vector v, and return the sum. Let us say that we are going to use all 7 integer registers, and put N into eax and v into edx. Our function prologue would then look like:

#   int isum(int N, int *v)
.global isum
        .type   isum,@function
isum:
#
#       Save non-scratch registers
#
        subl    $16, %esp
        movl    %ebx, (%esp)
        movl    %ebp, 4(%esp)
        movl    %esi, 8(%esp)
        movl    %edi, 12(%esp)
#
#       Load N and v
#
        movl    20(%esp), %eax
        movl    24(%esp), %edx

Assuming we accumulated our integer sum into ecx, the function epilogue would then consist of:

#
#       Set return value
#
        movl    %ecx, %eax
#
#       Restore registers
#
        movl    (%esp), %ebx
        movl    4(%esp), %ebp
        movl    8(%esp), %esi
        movl    12(%esp), %edi
        addl    $16, %esp
        ret



Clint Whaley 2012-07-10