07.04.2013 Views

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs ... - Microchip

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 Assembly Language <strong>and</strong> C Modules<br />

: [foo] "=r" (foo)<br />

: "0" (foo), [bar] "r" (bar));<br />

EXAMPLE 9-9: VOLATILE ASM STATEMENTS<br />

You can prevent an asm instruction from being deleted, moved significantly, or<br />

combined, by writing the keyword volatile after the asm. For example:<br />

#define disi(n) \<br />

asm volatile ("disi #%0" \<br />

: /* no outputs */ \<br />

: "i" (n))<br />

In this case, the constraint letter “i” denotes an immediate oper<strong>and</strong>, as required by the<br />

disi instruction.<br />

EXAMPLE 9-10: MAKING CONTROL FLOW CHANGES<br />

There are special precautions that must be taken when making control flow changes<br />

within inline assembly statements.<br />

There is no way, <strong>for</strong> example, to tell the compiler that an inline asm statement may<br />

result in a change of control flow. The control should enter the asm statement <strong>and</strong><br />

always proceed to the next statement.<br />

Good control flow:<br />

asm("call _foo" : /* outputs */<br />

: /* inputs */<br />

: "w0", "w1", "w2", "w3", "w4", "w5",<br />

"w6", "w7");<br />

/* next statement */<br />

This is acceptable because after calling foo, the next statement will be executed. The<br />

code tells the compiler that some registers do not survive this statement; these<br />

represent the registers that will not be preserved by foo.<br />

Bad control flow:<br />

asm("bra OV, error");<br />

/* next statement */<br />

return 0;<br />

asm("error: ");<br />

return 1;<br />

This is unacceptable as the compiler will assume that the next statement, return 0,<br />

is executed when it may not be. In this case, the asm("error: ") <strong>and</strong> following statements<br />

will be deleted because they are unreachable. See further in<strong>for</strong>mation regarding<br />

labels in asm statements.<br />

© 2008 <strong>Microchip</strong> Technology Inc. DS51284H-page 125

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

Saved successfully!

Ooh no, something went wrong!