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.1

Rationale

The asm declaration is available in all C++ implementations2 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 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.

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