SLAU132V October 2004 – February 2020
The C/C++ compiler can embed assembly language instructions or directives directly into the assembly language output of the compiler. This capability is an extension to the C/C++ language implemented through the __asm keyword. The __asm keyword provides access to hardware features that C/C++ cannot provide.
The alternate keyword, "asm", may also be used except in strict ANSI C mode. It is available in relaxed C and C++ modes.
Using __asm is syntactically performed as a call to a function named __asm, with one string constant argument:
__asm("assembler text"); |
The compiler copies the argument string directly into your output file. The assembler text must be enclosed in double quotes. All the usual character string escape codes retain their definitions. For example, you can insert a .byte directive that contains quotes as follows:
__asm("STR: .byte \"abc\"");
The naked function attribute can be used to identify functions that are written as embedded assembly functions using __asm statements. See Section 5.17.2.
The inserted code must be a legal assembly language statement. Like all assembly language statements, the line of code inside the quotes must begin with a label, a blank, a tab, or a comment (asterisk or semicolon). The compiler performs no checking on the string; if there is an error, the assembler detects it. For more information about the assembly language statements, see the MSP430 Assembly Language Tools User's Guide.
The __asm statements do not follow the syntactic restrictions of normal C/C++ statements. Each can appear as a statement or a declaration, even outside of blocks. This is useful for inserting directives at the very beginning of a compiled module.
The __asm statement does not provide any way to refer to local variables. If your assembly code needs to refer to local variables, you will need to write the entire function in assembly code.
For more information, refer to Section 6.6.5.
NOTE
Avoid Disrupting the C/C++ Environment With asm Statements
Be careful not to disrupt the C/C++ environment with __asm statements. The compiler does not check the inserted instructions. Inserting jumps and labels into C/C++ code can cause unpredictable results in variables manipulated in or around the inserted code. Directives that change sections or otherwise affect the assembly environment can also be troublesome.
Be especially careful when you use optimization with __asm statements. Although the compiler cannot remove __asm statements, it can significantly rearrange the code order near them and cause undesired results.