Starting from a small snippet of c-code this example shows for the MCS51 how to use inline assembly, access variables, a function parameter and an array in xdata memory. The example uses an MCS51 here but is easily adapted for other architectures. This is a buffer routine which should be optimized:
unsigned char __far __at(0x7f00) buf[0x100];
If the code snippet (assume it is saved in buffer.c) is compiled with SDCC then a corresponding buffer.asm file is generated. We define a new function to_buffer_asm() in file buffer.c in which we cut and paste the generated code, removing unwanted comments and some ':'. Then add ''_asm'' and ''_endasm;''3.5 to the beginning and the end of the function body:
/* With a cut and paste from the .asm file, we have something to start with.
The new file buffer.c should compile with only one warning about the unreferenced function argument 'c'. Now we hand-optimize the assembly code and insert an #define USE_ASSEMBLY (1) and finally have:
unsigned char __far __at(0x7f00) buf[0x100];
The inline assembler code can contain any valid code understood by the assembler, this includes any assembler directives and comment lines. The assembler does not like some characters like ':' or ''' in comments. You'll find an 100+ pages assembler manual in sdcc/as/doc/asxhtm.html or online at http://sdcc.svn.sourceforge.net/viewvc/*checkout*/sdcc/trunk/sdcc/as/doc/asxhtm.html . The compiler does not do any validation of the code within the _asm ... _endasm; keyword pair. Specifically it will not know which registers are used and thus register pushing/popping has to be done manually. It is recommended that each assembly instruction (including labels) be placed in a separate line (as the example shows). When the --peep-asm command line option is used, the inline assembler code will be passed through the peephole optimizer. There are only a few (if any) cases where this option makes sense, it might cause some unexpected changes in the inline assembler code. Please go through the peephole optimizer rules defined in file SDCCpeeph.def before using this option.
Next: 3.12.2 Naked Functions Up: 3.12 Inline Assembler Code Previous: 3.12 Inline Assembler Code Contents Index 2008-02-29 |