Main Content

AUTOSAR C++14 Rule A7-4-1

The asm declaration shall not be used

Since R2020a

Description

Rule Definition

The asm declaration shall not be used.

Rationale

The asm declaration is a method to include assembly instructions directly within C++ source code. Support and implementation of the asm declaration is inconsistent across environments. The asm declaration interacts differently with C++ source code in different environments. To avoid restricting the portability of your code, do not use the asm declaration and do not include assembly instructions in your C++ source code.

Polyspace Implementation

Polyspace® flags the use of the asm declaration anywhere in C++ source code.

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

#include <cstdint>
using namespace std;
const char* p = "hello world";

void Fn1(void)
{
    asm("movq p, %rdi\n" // Noncompliant
        "call puts");
}

#define _debug() asm volatile("debug":::"memory")  // Noncompliant    

void Fn2(void)
{
    _debug();
}

main()
{
    //
}

Polyspace flags the use of asm declaration in Fn(1) because the assembly instructions following the declaration are environment-specific. For example, if you use a gcc compiler in a x64 Linux® environment, Fn1() produces the string hello world when called. In other environments, the output of the call to Fn1() is unpredictable. Polyspace also flags the use of the asm declaration in creating the _debug() macro.

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2020a