<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <!--Converted with LaTeX2HTML 2008 (1.71) original version by: Nikos Drakos, CBLU, University of Leeds * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan * with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --> <HTML> <HEAD> <TITLE>3.7 Parameters & Local Variables</TITLE> <META NAME="description" CONTENT="3.7 Parameters & Local Variables"> <META NAME="keywords" CONTENT="sdccman"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <META NAME="Generator" CONTENT="LaTeX2HTML v2008"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <LINK REL="STYLESHEET" HREF="sdccman.css"> <LINK REL="next" HREF="node66.html"> <LINK REL="previous" HREF="node64.html"> <LINK REL="up" HREF="node37.html"> <LINK REL="next" HREF="node66.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html1654" HREF="node66.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1648" HREF="node37.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1642" HREF="node64.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1650" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1652" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1655" HREF="node66.html">3.8 Overlaying</A> <B> Up:</B> <A NAME="tex2html1649" HREF="node37.html">3. Using SDCC</A> <B> Previous:</B> <A NAME="tex2html1643" HREF="node64.html">3.6 Absolute Addressing</A> <B> <A NAME="tex2html1651" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1653" HREF="node191.html">Index</A></B> <BR> <BR> <!--End of Navigation Panel--> <H1><A NAME="SECTION00470000000000000000"></A><A NAME="1606"></A><A NAME="1607"></A><A NAME="1608"></A><A NAME="sec:Parameters-and-Local-Variables"></A> <BR> 3.7 Parameters & Local Variables </H1> <P> 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 medium or large model). This in fact makes them similar to <I>static<A NAME="1610"></A></I> so by default functions are non-reentrant<A NAME="1611"></A>. <BR> <BR> They can be placed on the stack<A NAME="1612"></A> by using the <I>--stack-auto<A NAME="1613"></A></I> option, by using <I>#pragma stackauto</I><A NAME="1615"></A> or by using the <I>reentrant<A NAME="1616"></A></I> keyword in the function declaration, e.g.: <BLOCKQUOTE> <TT>unsigned char foo(char i) __reentrant </TT> <BR><TT>{ </TT> <BR><TT> ... </TT> <BR><TT>}</TT> </BLOCKQUOTE> Since stack space on 8051 is limited, the <I>reentrant</I> keyword or the <I>--stack-auto</I> option should be used sparingly. Note that the reentrant keyword just means that the parameters & local variables will be allocated to the stack, it <I>does not</I> mean that the function is register bank<A NAME="1626"></A> independent. <BR> <BR> Local variables<A NAME="1627"></A> can be assigned storage classes and absolute<A NAME="1628"></A> addresses, e.g.: <BLOCKQUOTE> <TT>unsigned char foo(__xdata int parm) </TT> <BR><TT>{</TT> <BR><TT> __xdata unsigned char i;</TT> <BR><TT> __bit bvar;</TT> <BR><TT> __data __at<A NAME="1634"></A> (0x31) unsigned char j;</TT> <BR><TT> ... </TT> <BR><TT>}</TT> </BLOCKQUOTE> In the above example the parameter<A NAME="1638"></A> <I>parm</I> and the variable <I>i</I> will be allocated in the external ram, <I>bvar</I> in bit addressable space and <I>j</I> in internal ram. When compiled with <I>--stack-auto</I> or when a function is declared as <I>reentrant</I> this should only be done for static variables. <P> It is however allowed to use bit parameters in reentrant functions and also non-static local bit variables are supported. Efficient use is limited to 8 semi-bitregisters in bit space. They are pushed and popped to stack<A NAME="1645"></A> as a single byte just like the normal registers. <P> <HR> <!--Navigation Panel--> <A NAME="tex2html1654" HREF="node66.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1648" HREF="node37.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1642" HREF="node64.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1650" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1652" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1655" HREF="node66.html">3.8 Overlaying</A> <B> Up:</B> <A NAME="tex2html1649" HREF="node37.html">3. Using SDCC</A> <B> Previous:</B> <A NAME="tex2html1643" HREF="node64.html">3.6 Absolute Addressing</A> <B> <A NAME="tex2html1651" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1653" HREF="node191.html">Index</A></B> <!--End of Navigation Panel--> <ADDRESS> 2011-03-20 </ADDRESS> </BODY> </HTML>