- Forward


The C Calling Convention
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back SMYC Forward
  • 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
Back SMYC Forward
  • 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 C Calling Convention - The Caller at Call-Time
Back SMYC Forward
  1. Save the contents of registers that the callee can modify (called caller-saved) by pushing them onto the stack
  2. Push the parameters onto the stack in last-to-first order (to allow for a variable number of parameter)
  3. Push the address of the next instruction in the caller onto the stack
  4. Transfer control to the callee
The C Calling Convention - The Callee
Back SMYC Forward

Prologue

  1. Push the base pointer onto the stack
  2. Copy the current stack pointer into the base pointer (so that parameters and local variables can be found)
  3. 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)
  4. 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.)
Back SMYC Forward

...

Epilogue

  1. Store the return value in an appropriate register
  2. Restore the callee-saved registers by popping them off the stack
  3. Deallocate local variables by copying the base pointer into the stack pointer
  4. Pop the base pointer off the stack (to restore the caller's value)
  5. Pop the address of the next instruction in the caller off the stack
  6. Transfer control to that instruction
The C Calling Convention - The Caller at Return-Time
Back SMYC Forward
  1. Remove the parameters from the stack (to restore the stack to its original state)
  2. Restore the caller-saved registers
  3. Retrieve the return value (if any) from the approriate register
Some Details of Intel x86-32 Processors
Back SMYC Forward
  • 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.)
Back SMYC Forward
  • 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
Back -