11.07.2015 Views

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

PicC 9.50 dsPIC Manual.pdf

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Mixing C and Assembler CodeC Language Featuresextern 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()<strong>dsPIC</strong> 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 whichare to be embedded into the assembly output of the code generator. The #asm and #endasm constructis not syntactically part of the C program, and thus it does not obey normal C flow-of-control rules,however you can easily include multiple instructions with this form of in-line assembly.The asm() statement is used to embed a single assembler instruction. This form looks and behaveslike a C statement, however each instruction must be encapsulated within an asm() statement.•You should not use a #asm block within any C constructs such as if, while, do etc.In these cases, use only the asm("") form, which is a C statement and will correctlyinteract with all C flow-of-control structures.The following example shows both methods used:52unsigned int var;void main(void){var = 1;#asm// like this...mov.w _var,w0sl.w w0,w0mov.w w0,_var#endasm// or like this

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!