next up previous contents index
Next: 3.6 Overlaying Up: 3. Using SDCC Previous: 3.4 Pointers   Contents   Index

3.5 Parameters & Local Variables

Automatic (local) variables and parameters to functions can either be placed on the stack or in data-space. The default action of the compiler is to place these variables in the internal RAM (for small model) or external RAM (for Large model). This in fact makes them static so by default functions are non-reentrant.

They can be placed on the stack either by using the -stack-auto compiler option or by using the reentrant keyword in the function declaration, e.g.:

unsigned char foo(char i) reentrant  
{  
...  
} 

Since stack space on 8051 is limited, the reentrant keyword or the -stack-auto option should be used sparingly. Note that the reentrant keyword just means that the parameters & local variables will be allocated to the stack, it does not mean that the function is register bank independent.

Local variables can be assigned storage classes and absolute addresses, e.g.:

unsigned char foo() { 
    xdata unsigned char i; 
    bit bvar; 
    data at 0x31 unsiged char j; 
    ...  
} 
 
In the above example the variable i will be allocated in the external ram, bvar in bit addressable space and j in internal ram. When compiled with -stack-auto or when a function is declared as reentrant this can only be done for static variables.

Parameters however are not allowed any storage class, (storage classes for parameters will be ignored), their allocation is governed by the memory model in use, and the reentrancy options.


next up previous contents index
Next: 3.6 Overlaying Up: 3. Using SDCC Previous: 3.4 Pointers   Contents   Index
Johan Knol
2001-07-13