<!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.13.2 Naked Functions</TITLE> <META NAME="description" CONTENT="3.13.2 Naked Functions"> <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="node84.html"> <LINK REL="previous" HREF="node82.html"> <LINK REL="up" HREF="node81.html"> <LINK REL="next" HREF="node84.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html1921" HREF="node84.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1915" HREF="node81.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1909" HREF="node82.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1917" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1919" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1922" HREF="node84.html">3.13.3 Use of Labels</A> <B> Up:</B> <A NAME="tex2html1916" HREF="node81.html">3.13 Inline Assembler Code</A> <B> Previous:</B> <A NAME="tex2html1910" HREF="node82.html">3.13.1 A Step by</A> <B> <A NAME="tex2html1918" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1920" HREF="node191.html">Index</A></B> <BR> <BR> <!--End of Navigation Panel--> <H2><A NAME="SECTION004132000000000000000"></A><A NAME="sub:Naked-Functions"></A><A NAME="2247"></A> <BR> 3.13.2 Naked Functions </H2> <P> A special keyword may be associated with a function declaring it as <I>_naked<A NAME="2248"></A><A NAME="2249"></A>.</I> The <I>_naked</I> function modifier attribute prevents the compiler from generating prologue<A NAME="2251"></A> and epilogue<A NAME="2252"></A> code for that function. This means that the user is entirely responsible for such things as saving any registers that may need to be preserved, selecting the proper register bank, generating the <I>return</I> instruction at the end, etc. Practically, this means that the contents of the function must be written in inline assembler. This is particularly useful for interrupt functions, which can have a large (and often unnecessary) prologue/epilogue. For example, compare the code generated by these two functions: <BLOCKQUOTE> <TT>volatile<A NAME="2255"></A> data unsigned char counter;</TT> <BR> <BR><TT>void simpleInterrupt(void) __interrupt<A NAME="2257"></A><A NAME="2258"></A> (1)</TT> <BR><TT>{</TT> <BR><TT> counter++;</TT> <BR><TT>}</TT> <BR> <BR><TT>void nakedInterrupt(void) __interrupt (2) __naked</TT> <BR><TT>{</TT> <BR><TT> __asm<A NAME="2265"></A><A NAME="2266"></A></TT> <BR><TT> inc _counter ; does not change flags, no need to save psw</TT> <BR><TT> reti ; MUST explicitly include ret or reti in _naked function.</TT> <BR><TT> __endasm<A NAME="2269"></A><A NAME="2270"></A>;</TT> <BR><TT>}</TT> </BLOCKQUOTE> For an 8051 target, the generated simpleInterrupt looks like: <BLOCKQUOTE> <TT>Note, this is an</TT> <TT><I>outdated</I></TT> <TT>example, recent versions of SDCC generate</TT> <BR><TT>the</TT> <TT><I>same</I></TT> <TT>code for simpleInterrupt() and nakedInterrupt()!</TT> <BR> <BR><TT>_simpleInterrupt:</TT> <BR><TT> push acc</TT> <BR><TT> push b</TT> <BR><TT> push dpl</TT> <BR><TT> push dph</TT> <BR><TT> push psw</TT> <BR><TT> mov psw,#0x00</TT> <BR><TT> inc _counter</TT> <BR><TT> pop psw</TT> <BR><TT> pop dph</TT> <BR><TT> pop dpl</TT> <BR><TT> pop b</TT> <BR><TT> pop acc</TT> <BR><TT> reti</TT> </BLOCKQUOTE> whereas nakedInterrupt looks like: <BLOCKQUOTE> <TT>_nakedInterrupt:</TT> <BR><TT> inc _counter ; does not change flags, no need to save psw</TT> <BR><TT> reti ; MUST explicitly include ret or reti in _naked function</TT> </BLOCKQUOTE> The related directive #pragma exclude<A NAME="2301"></A> allows a more fine grained control over pushing & popping<A NAME="2302"></A> the registers. <P> While there is nothing preventing you from writing C code inside a <TT>_naked</TT> function, there are many ways to shoot yourself in the foot doing this, and it is recommended that you stick to inline assembler. <P> <HR> <!--Navigation Panel--> <A NAME="tex2html1921" HREF="node84.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1915" HREF="node81.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1909" HREF="node82.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html1917" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html1919" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html1922" HREF="node84.html">3.13.3 Use of Labels</A> <B> Up:</B> <A NAME="tex2html1916" HREF="node81.html">3.13 Inline Assembler Code</A> <B> Previous:</B> <A NAME="tex2html1910" HREF="node82.html">3.13.1 A Step by</A> <B> <A NAME="tex2html1918" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html1920" HREF="node191.html">Index</A></B> <!--End of Navigation Panel--> <ADDRESS> 2011-03-20 </ADDRESS> </BODY> </HTML>