Parameter Passing


Parameters are passed on the stack. The parameters are pushed on the stack then function call is made. So the parameters are just below the return address. If a function want to read the the parameter, then it read from the stack just below the return address.
Lets see this in an example

C code:
void fun(int x, int y)
{
    x++;
    x += y;
}
int main()
{
    fun(2, 3);
}
Here is the generated assembly code:
.text
.globl fun
fun:
    pushl	%ebp
    movl	%esp, %ebp
    addl	$1, 8(%ebp)
    movl	12(%ebp), %eax
    addl	%eax, 8(%ebp)
    popl	%ebp
    ret
.globl main
main:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $8, %esp
    movl    $3, 4(%esp)
    movl    $2, (%esp)
    call    fun
    leave
    ret
Location of local variables of the stack (local variables are explained here)
x ==> 8(%ebp)
y ==> 12(%ebp), %eax

This is very much related to what is described in the local variable chapters. The parameters are allocated as the local variables only. There is only one difference is that the local variables are allocated on the top where where the return address is pushed on stack and parameter lies beneath the return address.

The parameter passing can be explained with this diagram:


Comments on the generated code:

# .text segment starts
    .text

# export the the function fun
.globl fun

# function fun starts here
fun:

# save ebp on stack
    pushl   %ebp
    movl    %esp, %ebp

# x++;
    addl    $1, 8(%ebp)

# tmp = y
    movl    12(%ebp), %eax

# x = x + tmp
    addl    %eax, 8(%ebp)

#restore ebp and return
    popl    %ebp
	ret

# export the main function
.globl main

# main function starts here
main:
# save the ebp register
    pushl   %ebp
    movl    %esp, %ebp

# stack_pointer -= 8
    subl	$8, %esp

# push the second parametr
    movl    $3, 4(%esp)

# push the first parameter
    movl    $2, (%esp)

# call the function fun
    call	fun

# return the main function
    leave
    ret

Points to understand:

up