Download Manual - Digi-Key
Transcript
C Language Features
Mixing C and Assembler Code
By compiling a dummy C function with a similar prototype to how we will be calling this assembly routine, we can determine the signature value. We add a assembler
directive to make this signature value known:
SIGNAT _add,8250
Now to actually writing the function, remembering that the first byte parameter is already in the accumulator and the second paramater is already in this routine’s paramters
area – placed there by the calling function elsewhere. The result is placed back in to the
paramater area ready to be returned
add
w0,w2,w0
return
;add W0 to W2 and put the result in W0
To call an assembly routine from C code, a declaration for the routine must be provided. This
ensures that the compiler knows how to encode the function call in terms of paramters and return
values, however no other code is necessary.
If a signature value is present in the assembly code routine, its value will be checked by the linker
when the calling and called routines’ signatures can be compared.
T UT•RIAL
To continue the previous example, here is a code snippet that declares the operation of
the assembler routine, then calls the routine.
extern unsigned int add(unsigned a, unsigned b);
void main(void)
{
int a, result;
a = read_port();
result = add(5, a);
}
3.11.2
#asm, #endasm and asm()
dsPIC and PIC24 instructions may also be directly embedded “in-line” into C code using the directives #asm, #endasm or the statement asm().
The #asm and #endasm directives are used to start and end a block of assembly instructions which
are to be embedded into the assembly output of the code generator. The #asm and #endasm construct
75