next up previous contents index
Next: 3.13.3 Assembler Routine (reentrant) Up: 3.13 Interfacing with Assembler Previous: 3.13.1 Global Registers used   Contents   Index

3.13.2 Assembler Routine (non-reentrant)

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

extern int asm_func(unsigned char, unsigned char); 
 
int c_func (unsigned char i, unsigned char 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    dph,#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 a one byte parameter, ``dptr'' for two bytes, ``b,dptr'' for three bytes and ``acc,b,dptr'' for a four bytes parameter. The variable 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


next up previous contents index
Next: 3.13.3 Assembler Routine (reentrant) Up: 3.13 Interfacing with Assembler Previous: 3.13.1 Global Registers used   Contents   Index
2008-04-19