1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
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>
11 <A HREF="SDCCUdoc-22.html">Next</A>
12 <A HREF="SDCCUdoc-20.html">Previous</A>
13 <A HREF="SDCCUdoc.html#toc21">Contents</A>
15 <H2><A NAME="Interface_asm"></A> <A NAME="s21">21. Interfacing with assembly routines.</A> </H2>
17 <H2><A NAME="ss21.1">21.1 Global registers used for parameter passing.</A>
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>
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);
32 int c_func (unsigned short i, unsigned short j)
43 <P>The corresponding assembler function is:-
46 .globl _asm_func_PARM_2
50 _asm_func_PARM_2: .ds 1
55 add a,_asm_func_PARM_2
62 <P>Note here that the return values are placed in 'dpl' - One byte return
63 value, 'dpl' LSB & 'dph' MSB for two byte values. 'dpl', 'dph' and 'b'
64 for three byte values (generic pointers) and 'dpl','dph','b' & 'acc' for
66 <P>The parameter naming convention is <B>_<function_name>_PARM_<n>,</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 _<function_name>_PARM_2.</B></CODE>
71 <P>Assemble the assembler routine with the following command.
74 asx8051 -losg asmfunc.asm
77 <P>Then compile and link the assembler routine to the C source file with the
81 sdcc cfunc.c asmfunc.rel
84 <H3>Assembler routine is reentrant </H3>
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);
92 int c_func (unsigned short i, unsigned short j) reentrant
104 <P>The corresponding assembler routine is.
131 <P>The compiling and linking procedure remains the same, however note the
132 extra entry & 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
135 <H2><A NAME="ss21.2">21.2 With --noregparms option.</A>
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>
142 <P>In the following example the function<B> cfunc</B> calls an assembler routine
143 <B>asm_func</B>, which takes two parameters.
146 extern int asm_func( unsigned short, unsigned short);
147 int c_func (unsigned short i, unsigned short j)
158 <P>The corresponding assembler function is:-
161 .globl _asm_func_PARM_1
162 .globl _asm_func_PARM_2
166 _asm_func_PARM_1: .ds 1
171 mov a,_asm_func_PARM_1
173 add a,_asm_func_PARM_2
180 <P>Note here that the return values are placed in 'dpl' - One byte return
181 value, 'dpl' LSB & 'dph' MSB for two byte values. 'dpl', 'dph' and 'b'
182 for three byte values (generic pointers) and 'dpl','dph','b' & 'acc' for
184 <P>The parameter naming convention is <B>_<function_name>_PARM_<n>,</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 _<function_name>_PARM_1.</B></CODE>
187 <P>Assemble the assembler routine with the following command.
190 asx8051 -losg asmfunc.asm
193 <P>Then compile and link the assembler routine to the C source file with the
197 sdcc cfunc.c asmfunc.rel
200 <H3>Assembler routine is reentrant. </H3>
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);
208 int c_func (unsigned short i, unsigned short j) reentrant
211 return asm_func(i,j);
220 <P>The corresponding assembler routine is.
248 <P>The compiling and linking procedure remains the same, however note the
249 extra entry & 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
253 <A HREF="SDCCUdoc-22.html">Next</A>
254 <A HREF="SDCCUdoc-20.html">Previous</A>
255 <A HREF="SDCCUdoc.html#toc21">Contents</A>