24.4.16 In-Line Assembly

The asm() statement can be used to insert assembly code in-line with C code. The argument is a C string literal that represents a single assembly instruction. Obviously, the instructions contained in the argument are device specific.

Use the native keywords discussed in the Differences section to look up information on the semantics of this statement.

Example

The following shows a MOVLW 8-bit PIC MCU instruction being inserted in-line.
asm("MOVLW _foobar");

Differences

When targeting PIC devices with MPLAB XC8, the asm() or #asm ... #endasm constructs have been used to insert in-line assembly code.

The MPLAB XC16, XC-DSC and XC32 compilers, as well as the MPLAB XC8 compiler for AVR MCUs use the same syntax as the CCI.

Migration to the CCI

When building with the MPLAB XC8 compiler for PIC MCUs, change any instance of #asm ... #endasm so that each instruction in this #asm block is placed in its own asm() statement, for example, from:

#asm
	MOVLW		20
	MOVWF _i
	CLRF		Ii+1
#endasm

to:

asm("MOVLW		20");
asm("MOVWF _i");
asm("CLRF		Ii+1");

No migration is required for MPLAB XC8 when targeting AVR MCUs or when using the other compilers.

Caveats

None.