JMU
The C Calling Convention
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review
Calling Conventions
The C Calling Convention - The Caller at Call-Time
  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

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.)

...

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
  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
Some Details of Intel x86-32 Processors (cont.)