4-13
PROGRAMMING CONSIDERATIONS
4.5.4 Linking Subroutines
Parameters are passed to subroutines via the stack. Parameters are pushed into the stack from the
rightmost parameter to the left. The 8-bit parameters are pushed into the stack with the high-order
byte undefined. The 32-bit parameters are pushed onto the stack as two 16-bit values; the most-
significant half of the parameter is pushed into the stack first. As an example, consider the fol-
lowing procedure:
void example_procedure (char param1, long param2, int param3);
When this procedure is entered at run-time, the stack will contain the parameters in the following
order:
If a procedure returns a value to the calling code (as opposed to modifying more global variables)
the result is returned in the temporary storage space (TMPREG0, in this example) starting at 1CH.
TMPREG0 is viewed as either an 8-, 16-, 32-, or 64-bit variable, depending on the type of the
procedure.
The standard calling convention adopted by the C programming language has several key fea-
tures:
• Procedures can always assume that the eight or sixteen bytes of register file memory
starting at 1CH can be used as temporary storage within the body of the procedure.
• Code that calls a procedure must assume that the procedure modifies the eight or sixteen
bytes of register file memory starting at 1CH.
• Code that calls a procedure must assume that the procedure modifies the processor status
word (PSW) condition flags because procedures do not save and restore the PSW.
• Function results from procedures are always returned in the variable TMPREG0.
The C programming language allows the definition of interrupt procedures, which are executed
when a predefined interrupt request occurs. Interrupt procedures do not conform to the rules of
normal procedures. Parameters cannot be passed to these procedures and they cannot return re-
sults. Since interrupt procedures can execute essentially at any time, they must save and restore
both the PSW and TMPREG0.
param3
low word of param2
high word of param2
undefined;param1
return address ← Stack Pointer