1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
3 <!--Converted with LaTeX2HTML 99.1 release (March 30, 1999)
4 original version by: Nikos Drakos, CBLU, University of Leeds
5 * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan
6 * with significant contributions from:
7 Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
10 <TITLE>4.4 Interfacing with Assembly Routines</TITLE>
11 <META NAME="description" CONTENT="4.4 Interfacing with Assembly Routines">
12 <META NAME="keywords" CONTENT="sdccman">
13 <META NAME="resource-type" CONTENT="document">
14 <META NAME="distribution" CONTENT="global">
16 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
17 <META NAME="Generator" CONTENT="LaTeX2HTML v99.1 release">
18 <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">
20 <LINK REL="STYLESHEET" HREF="sdccman.css">
22 <LINK REL="next" HREF="node43.html">
23 <LINK REL="previous" HREF="node41.html">
24 <LINK REL="up" HREF="node38.html">
25 <LINK REL="next" HREF="node43.html">
29 <!--Navigation Panel-->
32 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A>
35 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A>
38 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A>
41 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A>
44 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A>
46 <B> Next:</B> <A NAME="tex2html895"
47 HREF="node43.html">4.5 External Stack</A>
48 <B> Up:</B> <A NAME="tex2html889"
49 HREF="node38.html">4. SDCC Technical Data</A>
50 <B> Previous:</B> <A NAME="tex2html883"
51 HREF="node41.html">4.3 <pending: this is</A>
52   <B> <A NAME="tex2html891"
53 HREF="node1.html">Contents</A></B>
54   <B> <A NAME="tex2html893"
55 HREF="node61.html">Index</A></B>
58 <!--End of Navigation Panel-->
59 <!--Table of Child-Links-->
60 <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
63 <LI><A NAME="tex2html896"
64 HREF="node42.html#SECTION00054100000000000000">4.4.1 Global Registers used for Parameter Passing</A>
65 <LI><A NAME="tex2html897"
66 HREF="node42.html#SECTION00054200000000000000">4.4.2 Assembler Routine(non-reentrant)</A>
67 <LI><A NAME="tex2html898"
68 HREF="node42.html#SECTION00054300000000000000">4.4.3 Assembler Routine(reentrant)</A>
70 <!--End of Table of Child-Links-->
73 <H2><A NAME="SECTION00054000000000000000">
74 4.4 Interfacing with Assembly Routines</A>
79 <H3><A NAME="SECTION00054100000000000000">
80 4.4.1 Global Registers used for Parameter Passing</A>
84 The compiler always uses the global registers <I>DPL,DPH,B</I> and
85 <I>ACC</I> to pass the first parameter to a routine. The second parameter
86 onwards is either allocated on the stack (for reentrant routines or
87 if -stack-auto is used) or in the internal / external ram (depending
92 <H3><A NAME="SECTION00054200000000000000">
93 4.4.2 Assembler Routine(non-reentrant)</A>
97 In the following example the function cfunc calls an assembler routine
98 asm_func, which takes two parameters.
102 <TT>extern int asm_func(unsigned char, unsigned char);</TT>
105 <TT>int c_func (unsigned char i, unsigned char j)</TT>
109 <TT> return asm_func(i,j);</TT>
114 <TT>int main()</TT>
118 <TT> return c_func(10,9);</TT>
123 The corresponding assembler function is:
127 <TT>.globl _asm_func_PARM_2 </TT>
129 <TT> .globl _asm_func </TT>
131 <TT> .area OSEG </TT>
133 <TT>_asm_func_PARM_2:</TT>
135 <TT> .ds 1 </TT>
137 <TT> .area CSEG </TT>
139 <TT>_asm_func: </TT>
141 <TT> mov a,dpl </TT>
143 <TT> add a,_asm_func_PARM_2 </TT>
145 <TT> mov dpl,a </TT>
147 <TT> mov dpl,#0x00 </TT>
149 <TT> ret</TT>
152 Note here that the return values are placed in 'dpl' - One byte return
153 value, 'dpl' LSB & 'dph' MSB for two byte values. 'dpl', 'dph' and
154 'b' for three byte values (generic pointers) and 'dpl','dph','b' &
155 'acc' for four byte values.
158 The parameter naming convention is _<function_name>_PARM_<n>,
159 where n is the parameter number starting from 1, and counting from
160 the left. The first parameter is passed in ``dpl'' for One bye
161 parameter, ``dptr'' if two bytes, ``b,dptr'' for three bytes
162 and ``acc,b,dptr'' for four bytes, the varible name for the second
163 parameter will be _<function_name>_PARM_2.
167 Assemble the assembler routine with the following command:
171 <I><B>asx8051 -losg asmfunc.asm</B></I>
174 Then compile and link the assembler routine to the C source file with
175 the following command:
179 <I><B>sdcc cfunc.c asmfunc.rel</B></I>
183 <H3><A NAME="SECTION00054300000000000000">
184 4.4.3 Assembler Routine(reentrant)</A>
188 In this case the second parameter onwards will be passed on the stack,
189 the parameters are pushed from right to left i.e. after the call the
190 left most parameter will be on the top of the stack. Here is an example:
194 <TT>extern int asm_func(unsigned char, unsigned char);</TT>
197 <TT>int c_func (unsigned char i, unsigned char j) reentrant </TT>
201 <TT> return asm_func(i,j); </TT>
206 <TT>int main() </TT>
210 <TT> return c_func(10,9); </TT>
216 The corresponding assembler routine is:
220 <TT>.globl _asm_func </TT>
222 <TT>_asm_func: </TT>
224 <TT> push _bp </TT>
226 <TT> mov _bp,sp </TT>
228 <TT> mov r2,dpl</TT>
230 <TT> mov a,_bp </TT>
232 <TT> clr c </TT>
234 <TT> add a,#0xfd </TT>
236 <TT> mov r0,a </TT>
238 <TT> add a,#0xfc</TT>
240 <TT> mov r1,a </TT>
242 <TT> mov a,@r0 </TT>
244 <TT> add a,r2</TT>
246 <TT> mov dpl,a </TT>
248 <TT> mov dph,#0x00 </TT>
250 <TT> mov sp,_bp </TT>
252 <TT> pop _bp </TT>
254 <TT> ret</TT>
257 The compiling and linking procedure remains the same, however note
258 the extra entry & exit linkage required for the assembler code, _bp
259 is the stack frame pointer and is used to compute the offset into
260 the stack for parameters and local variables.
264 <!--Navigation Panel-->
265 <A NAME="tex2html894"
267 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A>
268 <A NAME="tex2html888"
270 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A>
271 <A NAME="tex2html882"
273 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A>
274 <A NAME="tex2html890"
276 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A>
277 <A NAME="tex2html892"
279 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A>
281 <B> Next:</B> <A NAME="tex2html895"
282 HREF="node43.html">4.5 External Stack</A>
283 <B> Up:</B> <A NAME="tex2html889"
284 HREF="node38.html">4. SDCC Technical Data</A>
285 <B> Previous:</B> <A NAME="tex2html883"
286 HREF="node41.html">4.3 <pending: this is</A>
287   <B> <A NAME="tex2html891"
288 HREF="node1.html">Contents</A></B>
289   <B> <A NAME="tex2html893"
290 HREF="node61.html">Index</A></B>
291 <!--End of Navigation Panel-->
294 <BR><I>2001-07-13</I>