Added
[fw/sdcc] / doc / sdccman.html / node42.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2
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 -->
8 <HTML>
9 <HEAD>
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">
15
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">
19
20 <LINK REL="STYLESHEET" HREF="sdccman.css">
21
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">
26 </HEAD>
27
28 <BODY >
29 <!--Navigation Panel-->
30 <A NAME="tex2html894"
31  HREF="node43.html">
32 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
33 <A NAME="tex2html888"
34  HREF="node38.html">
35 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
36 <A NAME="tex2html882"
37  HREF="node41.html">
38 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
39 <A NAME="tex2html890"
40  HREF="node1.html">
41 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
42 <A NAME="tex2html892"
43  HREF="node61.html">
44 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
45 <BR>
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 &lt;pending: this is</A>
52  &nbsp <B>  <A NAME="tex2html891"
53  HREF="node1.html">Contents</A></B> 
54  &nbsp <B>  <A NAME="tex2html893"
55  HREF="node61.html">Index</A></B> 
56 <BR>
57 <BR>
58 <!--End of Navigation Panel-->
59 <!--Table of Child-Links-->
60 <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
61
62 <UL>
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>
69 </UL>
70 <!--End of Table of Child-Links-->
71 <HR>
72
73 <H2><A NAME="SECTION00054000000000000000">
74 4.4 Interfacing with Assembly Routines</A>
75 </H2>
76
77 <P>
78
79 <H3><A NAME="SECTION00054100000000000000">
80 4.4.1 Global Registers used for Parameter Passing</A>
81 </H3>
82
83 <P>
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
88 on the memory model). 
89
90 <P>
91
92 <H3><A NAME="SECTION00054200000000000000">
93 4.4.2 Assembler Routine(non-reentrant)</A>
94 </H3>
95
96 <P>
97 In the following example the function cfunc calls an assembler routine
98 asm_func, which takes two parameters.
99 <BR>
100
101 <BR>
102 <TT>extern int asm_func(unsigned char, unsigned char);</TT>&nbsp;
103 <BR>&nbsp;
104 <BR>
105 <TT>int c_func (unsigned char i, unsigned char j)</TT>&nbsp;
106 <BR>
107 <TT>{</TT>&nbsp;
108 <BR>
109 <TT>&nbsp;&nbsp;&nbsp;&nbsp;return asm_func(i,j);</TT>&nbsp;
110 <BR>
111 <TT>}</TT>&nbsp;
112 <BR>&nbsp;
113 <BR>
114 <TT>int main()</TT>&nbsp;
115 <BR>
116 <TT>{</TT>&nbsp;
117 <BR>
118 <TT>&nbsp;&nbsp;&nbsp;&nbsp;return c_func(10,9);</TT>&nbsp;
119 <BR>
120 <TT>}</TT>&nbsp;
121 <BR>&nbsp;
122 <BR>
123 The corresponding assembler function is:
124 <BR>
125
126 <BR>
127 <TT>.globl _asm_func_PARM_2 </TT>&nbsp;
128 <BR>
129 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.globl _asm_func </TT>&nbsp;
130 <BR>
131 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.area OSEG </TT>&nbsp;
132 <BR>
133 <TT>_asm_func_PARM_2:</TT>&nbsp;
134 <BR>
135 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ds      1 </TT>&nbsp;
136 <BR>
137 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.area CSEG </TT>&nbsp;
138 <BR>
139 <TT>_asm_func: </TT>&nbsp;
140 <BR>
141 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov     a,dpl </TT>&nbsp;
142 <BR>
143 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;add     a,_asm_func_PARM_2 </TT>&nbsp;
144 <BR>
145 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov     dpl,a </TT>&nbsp;
146 <BR>
147 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov     dpl,#0x00 </TT>&nbsp;
148 <BR>
149 <TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret</TT>&nbsp;
150 <BR>&nbsp;
151 <BR>
152 Note here that the return values are placed in 'dpl' - One byte return
153 value, 'dpl' LSB &amp; 'dph' MSB for two byte values. 'dpl', 'dph' and
154 'b' for three byte values (generic pointers) and 'dpl','dph','b' &amp;
155 'acc' for four byte values.
156
157 <P>
158 The parameter naming convention is _&lt;function_name&gt;_PARM_&lt;n&gt;,
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 _&lt;function_name&gt;_PARM_2.
164 <BR>
165
166 <BR>
167 Assemble the assembler routine with the following command:
168 <BR>
169
170 <BR>
171 <I><B>asx8051 -losg asmfunc.asm</B></I>
172 <BR>
173 <BR>
174 Then compile and link the assembler routine to the C source file with
175 the following command:
176 <BR>
177
178 <BR>
179 <I><B>sdcc cfunc.c asmfunc.rel</B></I>
180
181 <P>
182
183 <H3><A NAME="SECTION00054300000000000000">
184 4.4.3 Assembler Routine(reentrant)</A>
185 </H3>
186
187 <P>
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:
191 <BR>
192
193 <BR>
194 <TT>extern int asm_func(unsigned char, unsigned char);</TT>&nbsp;
195 <BR>&nbsp;
196 <BR>
197 <TT>int c_func (unsigned char i, unsigned char j) reentrant </TT>&nbsp;
198 <BR>
199 <TT>{ </TT>&nbsp;
200 <BR>
201 <TT>&nbsp;&nbsp;&nbsp;&nbsp;return asm_func(i,j); </TT>&nbsp;
202 <BR>
203 <TT>} </TT>&nbsp;
204 <BR>&nbsp;
205 <BR>
206 <TT>int main() </TT>&nbsp;
207 <BR>
208 <TT>{ </TT>&nbsp;
209 <BR>
210 <TT>&nbsp;&nbsp;&nbsp;&nbsp;return c_func(10,9); </TT>&nbsp;
211 <BR>
212 <TT>}</TT>&nbsp;
213 <BR>
214
215 <BR>
216 The corresponding assembler routine is:
217 <BR>
218
219 <BR>
220 <TT>.globl _asm_func </TT>&nbsp;
221 <BR>
222 <TT>_asm_func: </TT>&nbsp;
223 <BR>
224 <TT>&nbsp;&nbsp;&nbsp;&nbsp;push  _bp </TT>&nbsp;
225 <BR>
226 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  _bp,sp </TT>&nbsp;
227 <BR>
228 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  r2,dpl</TT>&nbsp;
229 <BR>
230 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  a,_bp </TT>&nbsp;
231 <BR>
232 <TT>&nbsp;&nbsp;&nbsp;&nbsp;clr  c </TT>&nbsp;
233 <BR>
234 <TT>&nbsp;&nbsp;&nbsp;&nbsp;add  a,#0xfd </TT>&nbsp;
235 <BR>
236 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  r0,a </TT>&nbsp;
237 <BR>
238 <TT>&nbsp;&nbsp;&nbsp;&nbsp;add  a,#0xfc</TT>&nbsp;
239 <BR>
240 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  r1,a </TT>&nbsp;
241 <BR>
242 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  a,@r0 </TT>&nbsp;
243 <BR>
244 <TT>&nbsp;&nbsp;&nbsp;&nbsp;add  a,r2</TT>&nbsp;
245 <BR>
246 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  dpl,a </TT>&nbsp;
247 <BR>
248 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  dph,#0x00 </TT>&nbsp;
249 <BR>
250 <TT>&nbsp;&nbsp;&nbsp;&nbsp;mov  sp,_bp </TT>&nbsp;
251 <BR>
252 <TT>&nbsp;&nbsp;&nbsp;&nbsp;pop  _bp </TT>&nbsp;
253 <BR>
254 <TT>&nbsp;&nbsp;&nbsp;&nbsp;ret</TT>&nbsp;
255 <BR>&nbsp;
256 <BR>
257 The compiling and linking procedure remains the same, however note
258 the extra entry &amp; 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.
261
262 <P>
263 <HR>
264 <!--Navigation Panel-->
265 <A NAME="tex2html894"
266  HREF="node43.html">
267 <IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="next_motif.gif"></A> 
268 <A NAME="tex2html888"
269  HREF="node38.html">
270 <IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="up_motif.gif"></A> 
271 <A NAME="tex2html882"
272  HREF="node41.html">
273 <IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="previous_motif.gif"></A> 
274 <A NAME="tex2html890"
275  HREF="node1.html">
276 <IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="contents_motif.gif"></A> 
277 <A NAME="tex2html892"
278  HREF="node61.html">
279 <IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="index_motif.gif"></A> 
280 <BR>
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 &lt;pending: this is</A>
287  &nbsp <B>  <A NAME="tex2html891"
288  HREF="node1.html">Contents</A></B> 
289  &nbsp <B>  <A NAME="tex2html893"
290  HREF="node61.html">Index</A></B> 
291 <!--End of Navigation Panel-->
292 <ADDRESS>
293 <I>Johan Knol</I>
294 <BR><I>2001-07-13</I>
295 </ADDRESS>
296 </BODY>
297 </HTML>