Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > e677bbbdff6d27fe001f15e0ef2bb4cc > files > 255

sdcc-3.0.0-0.fc14.x86_64.rpm

<!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>
 &nbsp; <B>  <A NAME="tex2html2002"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <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>&nbsp;
<BR>&nbsp;
<BR><TT>int c_func (unsigned char i, unsigned char j, unsigned
char k) reentrant </TT>&nbsp;
<BR><TT>{ </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;return asm_func(i,j,k); </TT>&nbsp;
<BR><TT>} </TT>&nbsp;
<BR>&nbsp;
<BR><TT>int main() </TT>&nbsp;
<BR><TT>{ </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;return c_func(10,9,8); </TT>&nbsp;
<BR><TT>}</TT>

</BLOCKQUOTE>
The corresponding (unoptimized) assembler routine is:
<BLOCKQUOTE>
<TT>.globl _asm_func </TT>&nbsp;
<BR><TT>_asm_func: </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;push&nbsp;_bp </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;_bp,sp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;stack contains: _bp,
return address, second parameter, third parameter</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;r2,dpl</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;a,_bp</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;a,#0xfd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;calculate pointer to
the second parameter</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;r0,a </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;a,_bp </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;a,#0xfc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;calculate pointer to
the rightmost parameter</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;r1,a </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;a,@r0</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;a,@r1</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;add&nbsp;&nbsp;a,r2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;calculate the result
(= sum of all three parameters)</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;dpl,a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;return value goes into
dptr (cast into int)</TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;dph,#0x00 </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;mov&nbsp;&nbsp;sp,_bp </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;pop&nbsp;&nbsp;_bp </TT>&nbsp;
<BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;ret</TT>

</BLOCKQUOTE>
The compiling and linking procedure remains the same, however note
the extra entry &amp; 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>
 &nbsp; <B>  <A NAME="tex2html2002"
  HREF="node1.html">Contents</A></B> 
 &nbsp; <B>  <A NAME="tex2html2004"
  HREF="node191.html">Index</A></B> 
<!--End of Navigation Panel-->
<ADDRESS>

2011-03-20
</ADDRESS>
</BODY>
</HTML>