Calling conventions, Function call mechanism – Zilog EZ80F916 User Manual
Page 169

UM014423-0607
Using the ANSI C-Compiler
ZiLOG Developer Studio II
eZ80Acclaim!
®
User Manual
149
{
#ifdef __ZILOG__
printf("ZiLOG Compiler ");
#endif
#ifdef __EZ80__
printf("For eZ80 ");
#endif
#ifdef __ZDATE__
printf("Built on %d.\n", __ZDATE__);
#endif
}
CALLING CONVENTIONS
The C-Compiler imposes a strict set of rules on function calls. Except for special run-time
support functions, any function that calls or is called by a C function must follow these
rules. Failure to adhere to these rules can disrupt the C environment and cause a C pro-
gram to fail.
The following sections describe the calling conventions:
•
“Function Call Mechanism” on page 149
•
Function Call Mechanism
A function (caller function) performs the following tasks when it calls another function
(called function):
1. Save all registers (other than the return value register, to be defined below) that might
be needed in the caller function after the return from the call, that is, a “caller save”
mechanism is used. The registers are saved by pushing them on the stack before the
call.
2. Push all function parameters on the stack in reverse order (the rightmost declared
argument is pushed first, and the leftmost is pushed last). This places the leftmost
argument on top of the stack when the function is called. For a
varargs
function, all
parameters are pushed on the stack in reverse order.
3. Then call the function. The call instruction pushes the return address on the top of the
stack.
4. On return from the called function, caller pops the arguments off the stack or
increments the stack pointer.
5. The caller then restores the saved registers by popping them from the stack.
The following example illustrates what must be done in an assembly procedure that calls a
C function, including “caller save” of the BC register: