Specifying the Memory Model for a Function

C51 Functions normally use the default memory model to determine which memory space to use for function arguments and local variables. See the “Memory Model” section above for more information about memory models.

You may, however, specify which memory model to use for a single function by including the small, compact, or large function attribute in the function declaration. For example:
#pragma small         /* Default to small model */

extern int calc (char i, int b) large reentrant;
extern int func (int i, float f) large;
extern void *tcp (char xdata *xp, int ndx) small;


int mtest (int i, int y) /* Small model */
{
return (i * y + y * i + func(-1, 4.75));
}


int large_func (int i, int k) large /* Large model */
{
return (mtest (i, k) + 2);
}

The advantage of functions using the SMALL memory model is that the local data and function argument parameters are stored in the internal 8051 RAM. Therefore, data access is very efficient. The internal memory is limited, however. Occasionally, the limited amount of internal data memory available when using the small model cannot satisfy the requirements of a very large program, and other memory models must be used. In this situation, you may declare that a function use a different memory model, as shown above.

By specifying the function model attribute in the function declaration, you can select which of the three possible reentrant stacks and frame pointers are used. Stack access in the SMALL model is more efficient than in the LARGE model.