Main Content

AUTOSAR C++14 Rule M7-4-3

Assembly language shall be encapsulated and isolated

Description

Rule Definition

Assembly language shall be encapsulated and isolated.

Rationale

Assembler instructions can be difficult to port across implementations. Encapsulating and isolating them in a function helps to make your code portable. You can easily track down assembler instructions or provide alternative implementations without changing the rest of the code.

Polyspace Implementation

The checker flags asm statements unless they are encapsulated in a function call.

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 DoSomething(void);

void Delay ( void ) {
    asm( "NOP"); //Compliant
    asm( "NOP"); //Compliant
}
void DoSomethingAndDelay (void) {
    DoSomething();
    Delay();// Assembler is encapsulated
    DoSomething();
    asm("NOP"); //Noncompliant
    DoSomething();
}

The asm statement in the function DoSomethingAndDelay() is mixed with regular C code and violates the rule. The asm statements in the function Delay() are compliant because they form the only instructions in this function. The Delay() function thus effectively encapsulates the assembler instructions.

Check Information

Group: Declaration
Category: Required, Automated

Version History

Introduced in R2019a