The C Calling Convention
An Introduction
|
Prof. David Bernstein
James Madison University
|
|
Computer Science Department
|
bernstdh@jmu.edu
|
Review
- Program:
- A file containing machine language instructions, the entry-point
address, data, symbol and relocation tables, and other
information
- Process:
- An instance of a program execution
- Process Memory:
- Text (i.e., instructions), intiialized data, uninitialized data,
stack, heap
Calling Conventions
- Defined:
- A calling convention is a protocol governing the calling of
and returning from "subroutines"
- Options:
- Many such protocols are possible
- Using a common calling convention makes it possible for
code written in one language to call code written in another
- A Popular Common Calling Convention:
The C Calling Convention - The Caller at Call-Time
- Save the contents of registers that the callee can modify
(called caller-saved) by pushing them onto the stack
- Push the parameters onto the stack in last-to-first
order (to allow for a variable number of parameter)
- Push the address of the next instruction in the caller
onto the stack
- Transfer control to the callee
The C Calling Convention - The Callee
Prologue
-
Push the base pointer onto the stack
- Copy the current stack pointer into the base pointer
(so that parameters and local variables can be found)
- Adjust the stack pointer (i.e., decrease it if the stack grows
down) to make room for each local variable (the amount of
the adjustment will depend on the size of the variable)
- Save the contents of registers the callee will modify
(called callee-saved) by pushing them onto the
stack
Body
...
The C Calling Convention - The Callee (cont.)
...
Epilogue
-
Store the return value in an appropriate register
- Restore the callee-saved registers by popping them off the stack
- Deallocate local variables by copying the base pointer
into the stack pointer
- Pop the base pointer off the stack (to restore the caller's
value)
- Pop the address of the next instruction in the caller off
the stack
- Transfer control to that instruction
The C Calling Convention - The Caller at Return-Time
- Remove the parameters from the stack (to restore
the stack to its original state)
- Restore the caller-saved registers
- Retrieve the return value (if any) from the approriate register
Some Details of Intel x86-32 Processors
- Pointers:
-
ebp
holds the base pointer
-
esp
holds the current stack pointer
-
eip
holds the next instruction to be executed
- Saved Registers:
- Caller-Saved:
eax
,ecx
,
edx
- Callee-Saved:
ebx
,edi
,
esi
-
mov
:
- Copies data between operands
- AT&T/GCC Notation:
mov
source
destination
- Intel/Microsoft Notation:
mov
destination, source
Some Details of Intel x86-32 Processors (cont.)
-
push
:
- Adjusts
esp
by 4 (e.g., down if the stack grows
down)
- Places its operand into the contents of the 4-byte location
at address
[esp]
-
pop
:
- Places the 4-bytes at address
[esp]
into the
operand
- Adjusts
esp
by 4 (e.g., up if the stack
grows down)
-
jmp
:
- Transfers program control to the instruction at the memory
location indicated by the operand
-
call
:
- Pushes the current code location onto the stack
- Jumps to the code location indicated by the operand
-
ret
:
- Pops a code location of the stack
- Jumps to that code location
There's Always More to Learn