Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 ユーザーズマニュアル

製品コード
SW006021-1
ページ / 518
MPLAB
®
 XC8 C Compiler User’s Guide
DS52053B-page 202
 2012 Microchip Technology Inc.
Here is an example of the complete routine for a mid-range device which could be 
placed into an assembly file and added to your project. The GLOBAL and SIGNAT direc-
tives do not generator code, and hence do not need to be inside the mytext psect, 
although you can place them there if you prefer. The BANKSEL directive and BANKMASK 
macro have been used to ensure that the correct bank was selected and that all 
addresses are masked to the appropriate size.
#include <xc.inc>
GLOBAL _add        ; make _add globally accessible
SIGNAT _add,4217   ; tell the linker how it should be called
; everything following will be placed into the mytext psect
PSECT mytext,local,class=CODE,delta=2
; our routine to add to ints and return the result
_add:
; W is loaded by the calling function;
BANKSEL
(PORTB)
; select the bank of this object
ADDWF
BANKMASK(PORTB),w
; add parameter to port
; the result is already in the required location (W)so we can
; just return immediately
RETURN
To compile this, the assembly file must be preprocessed as we have used the C pre-
processor #include directive. See Section 4.8.11 “-P: Preprocess Assembly 
Files”
.
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 
parameters and return values.
Here is a C code snippet that declares the operation of the assembler routine, then calls 
the routine.
// declare the assembly routine so it can be correctly called
extern unsigned char add(unsigned char a);
void main(void) {
  volatile unsigned char result;
  result = add(5);  // call the assembly routine
}
5.12.2
#asm, #endasm and asm()
Assembly instructions may also be directly embedded inline 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 block is not syntactically part of the C program, and thus it does not obey 
normal C flow-of-control rules. This means that you should not use this form of inline 
assembly inside C constructs like if(), while() and for() statements. However, 
this is the easiest means of adding multiple assembly instructions.
The asm(); statement is used to embed assembler instructions inline with C code. 
This form looks and behaves like a C statement. The instructions are placed in a string 
inside what look like function call brackets, although no call takes place. Typically one 
instruction is placed in the string, but you can specify more than one assembly instruc-
tion by separating the instructions with a \n character, (e.g., asm(
MOVLW 
55\nMOVWF _x”);
) Code will be more readable if you one place one instruction in 
each statement and use multiple statements.