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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

16-Bit C <strong>Compiler</strong> User’s Guide<br />

Acceptable control flow:<br />

asm("cp0 _foo\n\t"<br />

"bra nz, eek\n\t"<br />

"; some assembly\n\t"<br />

"bra eek_end\n\t"<br />

"eek:\n\t"<br />

"; more assembly\n"<br />

"eek_end:" : /* outputs */<br />

: /* inputs */<br />

: "cc");<br />

/* next statement */<br />

This is acceptable, but may not function as expected, (i.e., the next statement is always<br />

executed, regardless of any branch inside the asm statement). See further in<strong>for</strong>mation<br />

regarding labels in asm statements. Note that the code indicates that the status flags<br />

are no longer valid in this statement by identifying cc as clobbered.<br />

Lables <strong>and</strong> Control Flow:<br />

Additionally, labels inside assembly statements can behave unexpectedly with certain<br />

optimization options. The inliner may cause labels within asm statements to be defined<br />

multiple times.<br />

Also the procedural aggragator tool (-mpa) does not accept the local label syntax. See<br />

the following example:<br />

inline void foo() {<br />

asm("do #6, loopend");<br />

/* some C code */<br />

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

return;<br />

}<br />

This is bad <strong>for</strong> a number of reasons. First, the asm statements introduce an implied<br />

control flow that the compiler does not know about. Second, if foo() is inlined the label<br />

loopend will be defined many times. Third, the C code could be badly optimized<br />

because the compiler cannot see the loop structure. This example breaks the rule that<br />

the asm statement should not interfere with the program flow; asm("loopend:") will<br />

not always flow to the next statement.<br />

A solution would be to use the local label syntax as described in the “<strong>MPLAB</strong> ® Assembler,<br />

Linker <strong>and</strong> Utilties <strong>for</strong> <strong>PIC24</strong> <strong>MCUs</strong> <strong>and</strong> <strong>dsPIC</strong> ® <strong>DSCs</strong> User’s Guide” (DS51317),<br />

as in the following example:<br />

inline void foo() {<br />

asm("do #6, 0f");<br />

/* some C code */<br />

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

return;<br />

}<br />

The above <strong>for</strong>m is slightly better; at least it will fix the multiply-defined label issue.<br />

However the procedural aggregator tool (-mpa) does not accept the 0: <strong>for</strong>m of label.<br />

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

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

Saved successfully!

Ooh no, something went wrong!