Go to single file .html
[fw/sdcc] / doc / SDCCUdoc-21.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2 <HTML>
3 <HEAD>
4  <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.7">
5  <TITLE>SDCC Compiler User Guide: Interfacing with assembly routines.</TITLE>
6  <LINK HREF="SDCCUdoc-22.html" REL=next>
7  <LINK HREF="SDCCUdoc-20.html" REL=previous>
8  <LINK HREF="SDCCUdoc.html#toc21" REL=contents>
9 </HEAD>
10 <BODY>
11 <A HREF="SDCCUdoc-22.html">Next</A>
12 <A HREF="SDCCUdoc-20.html">Previous</A>
13 <A HREF="SDCCUdoc.html#toc21">Contents</A>
14 <HR>
15 <H2><A NAME="Interface_asm"></A> <A NAME="s21">21. Interfacing with assembly routines.</A> </H2>
16
17 <H2><A NAME="ss21.1">21.1 Global registers used for parameter passing.</A>
18  </H2>
19
20 <P>By default the compiler uses the global registers "DPL,DPH,B,ACC" to pass
21 the first parameter to a routine, the second parameter onwards is either allocated
22 on the stack (for reentrant routines or --stack-auto is used) or in the internal
23 / external ram (depending on the memory model). 
24 <H3>Assembler routine non-reentrant </H3>
25
26 <P>In the following example the function<B> cfunc</B> calls an assembler routine
27 <B>asm_func</B>, which takes two parameters.
28 <P>extern int asm_func( unsigned short, unsigned short);
29 <P>
30 <PRE>
31  
32 int c_func (unsigned short i, unsigned short j) 
33
34         return
35  asm_func(i,j); 
36
37 int main() 
38
39    return c_func(10,9); 
40 }
41  
42 </PRE>
43 <P>The corresponding assembler function is:-
44 <P>
45 <PRE>
46         .globl _asm_func_PARM_2 
47         .globl _asm_func 
48         .area
49  OSEG 
50 _asm_func_PARM_2:       .ds      1 
51         .area CSEG 
52 _asm_func: 
53        
54  mov     a,dpl 
55         add     a,_asm_func_PARM_2 
56         mov     dpl,a 
57        
58  mov     dpl,#0x00 
59         ret
60  
61 </PRE>
62 <P>Note here that the return values are placed in 'dpl' - One byte return
63 value, 'dpl' LSB &amp; 'dph' MSB for two byte values. 'dpl', 'dph' and 'b'
64 for three byte values (generic pointers) and 'dpl','dph','b' &amp; 'acc' for
65 four byte values.
66 <P>The parameter naming convention is <B>_&lt;function_name&gt;_PARM_&lt;n&gt;,</B>
67 where n is the parameter number starting from 1, and counting from the left.
68 The first parameter is passed in "dpl" for One bye parameter, "dptr" if two bytes,
69 "b,dptr" for three bytes and "acc,b,dptr" for four bytes, the <CODE></CODE><CODE><B>varaible name for
70 the second parameter will be _&lt;function_name&gt;_PARM_2.</B></CODE>
71 <P>Assemble the assembler routine with the following command.
72 <P>
73 <PRE>
74 asx8051 -losg asmfunc.asm
75  
76 </PRE>
77 <P>Then compile and link the assembler routine to the C source file with the
78 following command,
79 <P>
80 <PRE>
81 sdcc cfunc.c asmfunc.rel
82  
83 </PRE>
84 <H3>Assembler routine is reentrant </H3>
85
86 <P>In this case the second parameter onwards will be passed on the stack ,
87 the parameters are pushed from right to left i.e. after the call the left most
88 parameter will be on the top of the stack. Here is an example.
89 <P>extern int asm_func( unsigned short, unsigned short);
90 <P>
91 <PRE>
92  int c_func (unsigned short i, unsigned short j) reentrant 
93
94        
95  return asm_func(i,j); 
96
97 int main() 
98
99    return c_func(10,9);
100  
101 }
102  
103 </PRE>
104 <P>The corresponding assembler routine is.
105 <P>
106 <PRE>
107         .globl _asm_func 
108 _asm_func: 
109         push  _bp 
110         mov  _bp,sp
111  
112         mov  r2,dpl
113         mov  a,_bp 
114         clr  c 
115         add  a,#0xfd
116  
117         mov  r0,a 
118         add  a,#0xfc
119         mov  r1,a 
120         mov 
121  a,@r0 
122         add  a,r2
123         mov  dpl,a 
124         mov  dph,#0x00 
125        
126  mov  sp,_bp 
127         pop  _bp 
128         ret
129  
130 </PRE>
131 <P>The compiling and linking procedure remains the same, however note the
132 extra entry &amp; exit linkage required for the assembler code, _bp is the
133 stack frame pointer and is used to compute the offset into the stack for parameters
134 and local variables.
135 <H2><A NAME="ss21.2">21.2 With --noregparms option.</A>
136  </H2>
137
138 <P>When the source is compiled with --noregparms option , space is allocated
139 for each of the parameters passed to a routine.
140 <H3>Assembler routine non-reentrant. </H3>
141
142 <P>In the following example the function<B> cfunc</B> calls an assembler routine
143 <B>asm_func</B>, which takes two parameters.
144 <P>
145 <PRE>
146 extern int asm_func( unsigned short, unsigned short); 
147 int c_func (unsigned short i, unsigned short j) 
148
149         return
150  asm_func(i,j); 
151
152 int main() 
153
154    return c_func(10,9); 
155 }
156  
157 </PRE>
158 <P>The corresponding assembler function is:-
159 <P>
160 <PRE>
161         .globl _asm_func_PARM_1 
162         .globl _asm_func_PARM_2 
163        
164  .globl _asm_func 
165         .area OSEG 
166 _asm_func_PARM_1:       .ds     1 
167 _asm_func_PARM_2:      
168  .ds      1 
169         .area CSEG 
170 _asm_func: 
171         mov     a,_asm_func_PARM_1
172  
173         add     a,_asm_func_PARM_2 
174         mov     dpl,a 
175         mov    
176  dpl,#0x00 
177         ret
178  
179 </PRE>
180 <P>Note here that the return values are placed in 'dpl' - One byte return
181 value, 'dpl' LSB &amp; 'dph' MSB for two byte values. 'dpl', 'dph' and 'b'
182 for three byte values (generic pointers) and 'dpl','dph','b' &amp; 'acc' for
183 four byte values.
184 <P>The parameter naming convention is <B>_&lt;function_name&gt;_PARM_&lt;n&gt;,</B>
185 where n is the parameter number starting from 1, and counting from the left.
186 i.e. the <CODE></CODE><CODE><B>left-most parameter name will be _&lt;function_name&gt;_PARM_1.</B></CODE>
187 <P>Assemble the assembler routine with the following command.
188 <P>
189 <PRE>
190 asx8051 -losg asmfunc.asm
191  
192 </PRE>
193 <P>Then compile and link the assembler routine to the C source file with the
194 following command,
195 <P>
196 <PRE>
197 sdcc cfunc.c asmfunc.rel
198  
199 </PRE>
200 <H3>Assembler routine is reentrant. </H3>
201
202 <P>In this case the parameters will be passed on the stack , the parameters
203 are pushed from right to left i.e. after the call the left most parameter will
204 be on the top of the stack. Here is an example.
205 <P>extern int asm_func( unsigned short, unsigned short);
206 <P>
207 <PRE>
208  int c_func (unsigned short i, unsigned short j) reentrant 
209
210        
211  return asm_func(i,j); 
212
213 int main() 
214
215    return c_func(10,9);
216  
217 }
218  
219 </PRE>
220 <P>The corresponding assembler routine is.
221 <P>
222 <PRE>
223         .globl _asm_func 
224 _asm_func: 
225         push  _bp 
226         mov  _bp,sp
227  
228         mov  a,_bp 
229         clr  c 
230         add  a,#0xfd 
231         mov 
232  r0,a 
233         mov  a,_bp 
234         clr  c 
235         add  a,#0xfc 
236        
237  mov  r1,a 
238         mov  a,@r0 
239         add  a,@r1 
240         mov  dpl,a 
241        
242  mov  dph,#0x00 
243         mov  sp,_bp 
244         pop  _bp 
245         ret
246  
247 </PRE>
248 <P>The compiling and linking procedure remains the same, however note the
249 extra entry &amp; exit linkage required for the assembler code, _bp is the
250 stack frame pointer and is used to compute the offset into the stack for parameters
251 and local variables.
252 <HR>
253 <A HREF="SDCCUdoc-22.html">Next</A>
254 <A HREF="SDCCUdoc-20.html">Previous</A>
255 <A HREF="SDCCUdoc.html#toc21">Contents</A>
256 </BODY>
257 </HTML>