Main Content

MISRA C++:2008 Rule 7-4-2

Assembler instructions shall only be introduced using the asm declaration

Description

Rule Definition

Assembler instructions shall only be introduced using the asm declaration.

Rationale

The asm declaration is available in all C++ implementations1 but other ways of introducing assembler instructions might not be available. Using the asm declaration makes your code portable across implementations.

Polyspace Implementation

The rule checker reports uses of methods other than asm declarations as rule violations.

For instance, the rule checker allows the asm declaration:

asm("ADD a0,1")
But if the same assembler instructions are introduced through a #pragma asm, the checker reports a violation:
#pragma asm
   "ADD a0,1"
#pragma endasm

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

void Delay1 ( void ) {
#pragma asm    //Noncompliant
   "ADD a0,1" 
#pragma endasm
     ;
}

void Delay2 ( void ) {
   asm("ADD a0,1"); //Compliant
}

The use of #pragma asm violates this rule.

Check Information

Group: Declarations
Category: Required

Version History

Introduced in R2013b


1 How the asm declaration is implemented might vary across compilers. See also AUTOSAR C++14 Rule A7-4-1.