Next Previous Contents

21. Interfacing with assembly routines.

21.1 Global registers used for parameter passing.

By default the compiler uses the global registers "DPL,DPH,B,ACC" to pass the first parameter to a routine, the second parameter onwards is either allocated on the stack (for reentrant routines or --stack-auto is used) or in the internal / external ram (depending on the memory model).

Assembler routine non-reentrant

In the following example the function cfunc calls an assembler routine asm_func, which takes two parameters.

extern int asm_func( unsigned short, unsigned short);

 
int c_func (unsigned short i, unsigned short j) 
{ 
        return
 asm_func(i,j); 
} 
int main() 
{ 
   return c_func(10,9); 
}
 

The corresponding assembler function is:-

        .globl _asm_func_PARM_2 
        .globl _asm_func 
        .area
 OSEG 
_asm_func_PARM_2:       .ds      1 
        .area CSEG 
_asm_func: 
       
 mov     a,dpl 
        add     a,_asm_func_PARM_2 
        mov     dpl,a 
       
 mov     dpl,#0x00 
        ret
 

Note here that the return values are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for two byte values. 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','b' & 'acc' for four byte values.

The parameter naming convention is _<function_name>_PARM_<n>, where n is the parameter number starting from 1, and counting from the left. The first parameter is passed in "dpl" for One bye parameter, "dptr" if two bytes, "b,dptr" for three bytes and "acc,b,dptr" for four bytes, the varaible name for the second parameter will be _<function_name>_PARM_2.

Assemble the assembler routine with the following command.

asx8051 -losg asmfunc.asm
 

Then compile and link the assembler routine to the C source file with the following command,

sdcc cfunc.c asmfunc.rel
 

Assembler routine is reentrant

In this case the second parameter onwards will be passed on the stack , the parameters are pushed from right to left i.e. after the call the left most parameter will be on the top of the stack. Here is an example.

extern int asm_func( unsigned short, unsigned short);

 int c_func (unsigned short i, unsigned short j) reentrant 
{ 
       
 return asm_func(i,j); 
} 
int main() 
{ 
   return c_func(10,9);
 
}
 

The corresponding assembler routine is.

        .globl _asm_func 
_asm_func: 
        push  _bp 
        mov  _bp,sp
 
        mov  r2,dpl
        mov  a,_bp 
        clr  c 
        add  a,#0xfd
 
        mov  r0,a 
        add  a,#0xfc
        mov  r1,a 
        mov 
 a,@r0 
        add  a,r2
        mov  dpl,a 
        mov  dph,#0x00 
       
 mov  sp,_bp 
        pop  _bp 
        ret
 

The compiling and linking procedure remains the same, however note the extra entry & 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.

21.2 With --noregparms option.

When the source is compiled with --noregparms option , space is allocated for each of the parameters passed to a routine.

Assembler routine non-reentrant.

In the following example the function cfunc calls an assembler routine asm_func, which takes two parameters.

extern int asm_func( unsigned short, unsigned short); 
int c_func (unsigned short i, unsigned short j) 
{ 
        return
 asm_func(i,j); 
} 
int main() 
{ 
   return c_func(10,9); 
}
 

The corresponding assembler function is:-

        .globl _asm_func_PARM_1 
        .globl _asm_func_PARM_2 
       
 .globl _asm_func 
        .area OSEG 
_asm_func_PARM_1:       .ds     1 
_asm_func_PARM_2:      
 .ds      1 
        .area CSEG 
_asm_func: 
        mov     a,_asm_func_PARM_1
 
        add     a,_asm_func_PARM_2 
        mov     dpl,a 
        mov    
 dpl,#0x00 
        ret
 

Note here that the return values are placed in 'dpl' - One byte return value, 'dpl' LSB & 'dph' MSB for two byte values. 'dpl', 'dph' and 'b' for three byte values (generic pointers) and 'dpl','dph','b' & 'acc' for four byte values.

The parameter naming convention is _<function_name>_PARM_<n>, where n is the parameter number starting from 1, and counting from the left. i.e. the left-most parameter name will be _<function_name>_PARM_1.

Assemble the assembler routine with the following command.

asx8051 -losg asmfunc.asm
 

Then compile and link the assembler routine to the C source file with the following command,

sdcc cfunc.c asmfunc.rel
 

Assembler routine is reentrant.

In this case the parameters will be passed on the stack , the parameters are pushed from right to left i.e. after the call the left most parameter will be on the top of the stack. Here is an example.

extern int asm_func( unsigned short, unsigned short);

 int c_func (unsigned short i, unsigned short j) reentrant 
{ 
       
 return asm_func(i,j); 
} 
int main() 
{ 
   return c_func(10,9);
 
}
 

The corresponding assembler routine is.

        .globl _asm_func 
_asm_func: 
        push  _bp 
        mov  _bp,sp
 
        mov  a,_bp 
        clr  c 
        add  a,#0xfd 
        mov 
 r0,a 
        mov  a,_bp 
        clr  c 
        add  a,#0xfc 
       
 mov  r1,a 
        mov  a,@r0 
        add  a,@r1 
        mov  dpl,a 
       
 mov  dph,#0x00 
        mov  sp,_bp 
        pop  _bp 
        ret
 

The compiling and linking procedure remains the same, however note the extra entry & 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.


Next Previous Contents