<!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.14.4 Assembler Routine (reentrant)</TITLE> <META NAME="description" CONTENT="3.14.4 Assembler Routine (reentrant)"> <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="previous" HREF="node88.html"> <LINK REL="up" HREF="node85.html"> <LINK REL="next" HREF="node90.html"> </HEAD> <BODY > <!--Navigation Panel--> <A NAME="tex2html2005" HREF="node90.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1999" HREF="node85.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1995" HREF="node88.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html2001" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html2003" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html2006" HREF="node90.html">3.15 int (16 bit)</A> <B> Up:</B> <A NAME="tex2html2000" HREF="node85.html">3.14 Interfacing with Assembler</A> <B> Previous:</B> <A NAME="tex2html1996" HREF="node88.html">3.14.3 Assembler Routine (non-reentrant)</A> <B> <A NAME="tex2html2002" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html2004" HREF="node191.html">Index</A></B> <BR> <BR> <!--End of Navigation Panel--> <H2><A NAME="SECTION004144000000000000000"> 3.14.4 Assembler Routine (reentrant)</A> </H2> <P> In this case<A NAME="2415"></A><A NAME="2416"></A> the second parameter<A NAME="2417"></A> onwards will be passed on the stack, the parameters are pushed from right to left i.e. before the call the second leftmost parameter will be on the top of the stack (the leftmost parameter is passed in registers). Here is an example: <BLOCKQUOTE> <TT>extern int asm_func(unsigned char, unsigned char, unsigned char) reentrant;</TT> <BR> <BR><TT>int c_func (unsigned char i, unsigned char j, unsigned char k) reentrant </TT> <BR><TT>{ </TT> <BR><TT> return asm_func(i,j,k); </TT> <BR><TT>} </TT> <BR> <BR><TT>int main() </TT> <BR><TT>{ </TT> <BR><TT> return c_func(10,9,8); </TT> <BR><TT>}</TT> </BLOCKQUOTE> The corresponding (unoptimized) assembler routine is: <BLOCKQUOTE> <TT>.globl _asm_func </TT> <BR><TT>_asm_func: </TT> <BR><TT> push _bp </TT> <BR><TT> mov _bp,sp ;stack contains: _bp, return address, second parameter, third parameter</TT> <BR><TT> mov r2,dpl</TT> <BR><TT> mov a,_bp</TT> <BR><TT> add a,#0xfd ;calculate pointer to the second parameter</TT> <BR><TT> mov r0,a </TT> <BR><TT> mov a,_bp </TT> <BR><TT> add a,#0xfc ;calculate pointer to the rightmost parameter</TT> <BR><TT> mov r1,a </TT> <BR><TT> mov a,@r0</TT> <BR><TT> add a,@r1</TT> <BR><TT> add a,r2 ;calculate the result (= sum of all three parameters)</TT> <BR><TT> mov dpl,a ;return value goes into dptr (cast into int)</TT> <BR><TT> mov dph,#0x00 </TT> <BR><TT> mov sp,_bp </TT> <BR><TT> pop _bp </TT> <BR><TT> ret</TT> </BLOCKQUOTE> The compiling and linking procedure remains the same, however note the extra entry & exit linkage required for the assembler code, _bp is the stack frame pointer and is used to compute the offset into the stack for parameters and local variables. <P><P> <BR> <P> <HR> <!--Navigation Panel--> <A NAME="tex2html2005" HREF="node90.html"> <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next.png"></A> <A NAME="tex2html1999" HREF="node85.html"> <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up.png"></A> <A NAME="tex2html1995" HREF="node88.html"> <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="prev.png"></A> <A NAME="tex2html2001" HREF="node1.html"> <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents.png"></A> <A NAME="tex2html2003" HREF="node191.html"> <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index.png"></A> <BR> <B> Next:</B> <A NAME="tex2html2006" HREF="node90.html">3.15 int (16 bit)</A> <B> Up:</B> <A NAME="tex2html2000" HREF="node85.html">3.14 Interfacing with Assembler</A> <B> Previous:</B> <A NAME="tex2html1996" HREF="node88.html">3.14.3 Assembler Routine (non-reentrant)</A> <B> <A NAME="tex2html2002" HREF="node1.html">Contents</A></B> <B> <A NAME="tex2html2004" HREF="node191.html">Index</A></B> <!--End of Navigation Panel--> <ADDRESS> 2011-03-20 </ADDRESS> </BODY> </HTML>