MISRA C:2023 Dir 4.3
Description
Directive Definition
Assembly language shall be encapsulated and isolated.
Rationale
Encapsulating assembly language is beneficial because:
Encapsulating assembly instructions in a C source files clearly marks the boundary between C and assembly, making the code easier to read.
The name, and documentation, of the encapsulating macro or function makes the intent of the assembly language clear.
All uses of assembly language for a given purpose can share encapsulation, which improves maintainability.
You can easily substitute the assembly language for a different target or for static analysis purposes.
Polyspace Implementation
Polyspace® reports a violation of this rule if assembly language code is used without encapsulation. It is not a violation if the assembly code is encapsulated in:
Assembly language functions. Consider this code:
The functionasm int h(int tt) //Compliant { % reg val; mtmsr val; return 3; }; void f(void) { int x; x = h(3); }
h()
is declared as anasm
function which encapsulate the assembly code. Polyspace does not report a violation.#pragma
directives. For example, in this code:The#pragma inline_asm(h) int h(int tt) //Compliant { % reg val; mtmsr val; return 3; }; void f(void) { int x; x = h(3); }
#inline_asm
pragma designates the functionh()
as an assembly language function. Polyspace does not report a violation.Macros. For instance:
The macro#define FUNC_H\ asm\ {\ % reg val; \ mtmsr val;\ return 3; \ }; \ void f(void) { int x; x = FUNC_H(3); }
FUNC_H
encapsulates the assembly code. Polyspace does not report a violation. You can also define such macros at the command line during the Polyspace analysis by using the optionsPreprocessor definitions (-D)
.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Code design |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2024a