Function Parameters and the Stack

The stack pointer on the 8051 accesses internal data memory only. C51 locates the stack area immediately following all variables in the internal data memory. The stack pointer accesses internal memory indirectly and can use all of the internal data memory up to the 0xFF limit.

The total stack space is quite limited: only 256 bytes maximum. Rather than consume stack space with function parameters or arguments, C51 assigns a fixed memory location for each function parameter. When a function is called, the caller must copy the arguments into the assigned memory locations before transferring control to the desired function. The function then extracts its parameters, as needed, from these fixed memory locations. Only the return address is stored on the stack during this process. Interrupt functions require more stack space because they must switch register banks and save the values of a few registers on the stack.

Optionally, the C51 compiler can pass up to three function arguments in registers. This enhances speed performance. For more information, refer to the “Passing Parameters in Registers” section below.

NOTE
Some 8051 derivatives provide as little as 64 bytes of internal memory. The 8051 provides 128 and the 8052 provides 256. Take this into consideration when determining which memory model to use, because the amount of internal data memory directly affects the amount of stack space.