0% found this document useful (0 votes)
33 views

Unit2 Procedures

Yes, C provides a way to define functions that accept a variable number of arguments using the ellipsis (...) syntax. The typical calling convention used for variable argument functions is cdecl. In cdecl, all arguments are pushed onto the stack from right to left. It is the caller's responsibility to clean up the stack after the call. So in summary: - C provides a way to define functions that accept a variable number of arguments using ... - The typical calling convention used for such variable argument functions is cdecl - In cdecl, all arguments are pushed onto the stack from right to left and it is the caller's responsibility to clean up the stack after the call.

Uploaded by

novavoid_18337
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Unit2 Procedures

Yes, C provides a way to define functions that accept a variable number of arguments using the ellipsis (...) syntax. The typical calling convention used for variable argument functions is cdecl. In cdecl, all arguments are pushed onto the stack from right to left. It is the caller's responsibility to clean up the stack after the call. So in summary: - C provides a way to define functions that accept a variable number of arguments using ... - The typical calling convention used for such variable argument functions is cdecl - In cdecl, all arguments are pushed onto the stack from right to left and it is the caller's responsibility to clean up the stack after the call.

Uploaded by

novavoid_18337
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 13

Function calls

Example

Call add from main

Memory Layout without argument


esp->|___Old EBP_______________| (%ebp)

Memory layout (function with arguments)


|_________________________| |_________________________| |___Function parameter 3_____| 16(%ebp) |___Function parameter 2_____| 12(%ebp) |___Function parameter 1_____| 8(%ebp) |___Return address__________| 4(%ebp) esp->|___Old EBP_______________| (%ebp)

Memory Layout (function with arguments and local variables)


|______________________| |______________________| indirect addressing |___Function parameter 3__| 16(%ebp) |___Function parameter 2__| 12(%ebp) |___Function parameter 1__| 8(%ebp) |___Return address_______| 4(%ebp) |___Old EBP____________| (%ebp) |______Local variable 1___| -4(%ebp)

Function prologue and epilogue


function: pushl %ebp subl $8, %esp . . movl %ebp, %esp popl %ebp #epilogue # prologue movl %esp, %ebp

Prologue

The first two instructions at the top of the function code save the original value of EBP to the top of the stack, and then copy the current ESP stack pointer (now pointing to the original value of EBP in the stack) to the EBP register.

Epilogue
After the function processing completes, the last two instructions in the function retrieve the original value in the ESP register that was stored in the EBP register, and restore the original EBP register value. Resetting the ESP register value ensures that any data placed on the stack within the function but not cleaned off will be discarded when execution returns to the main program (otherwise, the RET instruction could return to the wrong memory location).

Calling Conventions

A scheme for how subroutines receive parameters from their caller and how they return a result. Means :

where parameters and return values are placed (in registers; on the call stack; a mix of both) the order in which parameters are passed (or parts of a single parameter) how the task of setting up for and cleaning up after a function call is divided between the caller and the callee which registers that may be directly used by the

Calling Conventions

Differ

Language to Language Platform to Platform

Types

Caller clean-up - caller cleans the arguments from the stack

Cdecl - In cdecl, function parameters are pushed on the stack in a right-to-left order. Function return values are returned in the EAX register (gcc) Syscall Optlink

Types

Callee clean-up - callee cleans the arguments from the stack it needs to be known at compile time how many bytes the stack needs to be adjusted

Pascal - the parameters are pushed on the stack in left-to-right order (opposite of cdecl), and the callee is responsible for balancing the stack before return. (MS...) Stdcall the parameters are pushed onto the stack in right-to-left order, as in the _cdecl calling convention. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register. Win32 API

Interesting point !

Can we have a function with variable number of arguments ? Which calling convention to use ?

You might also like