Rule 2.1

6.2 Language Extensions

Moderators: misra-c, david ward

nikunj
Posts: 4
Joined: Wed Feb 01, 2006 11:08 am
Location: Bangalore, INDIA

Rule 2.1

Postby nikunj » Thu Feb 16, 2006 7:22 am

What are the possible scenarios in which Rule 2.1 will be violated?

Rule allows for Macros, C-functions and Assembly functions.

If any assembly code is used directly it will be reported as an compliler error for undefined identifier etc.

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Postby misra-c » Thu Apr 27, 2006 5:51 pm

C statements and assembler statements should not co-exist in a function unless the assembler is encapsulated in a macro.

The following example violates this rule.

Code: Select all

static void foo ( void )
{
   asm { \"CLI\" };
   foobar();
   asm { \"SEI\" };
}
---
Posted by and on behalf of
the MISRA C Working Group

nikunj
Posts: 4
Joined: Wed Feb 01, 2006 11:08 am
Location: Bangalore, INDIA

Postby nikunj » Wed May 03, 2006 4:00 pm

Do the following code snippets violate Rule 2.1?

1. macro encapsulation:
#pragma asm
MOV A,B
....
....
#pragma endasm

2.function call
asm(\"CLI\");

or

move(\"MOV A,B\");


Also statements like

asm{ \"CLI\" };

seem to be compiler specific.
What are the other possible ways of violating this rule (compiler specific/not specific).

Also, please can you give examples where the rule is not violated.

lv
Posts: 9
Joined: Mon Oct 31, 2005 12:47 pm

Postby lv » Fri May 05, 2006 12:18 pm

Also, please can you give examples where the rule is not violated.


In my understanding, there are the three solutions compliant with MISRA 2.1:

a) Assembler function:
nop.h:

Code: Select all

extern void nop(void);


nop.asm:

Code: Select all

NOP   PROC
   nop
   ret
NOP  ENDP


foo.c

Code: Select all

#include \"nop.h\"

static void function(void)
{
   ... /* do somthing */
    nop();
   ... /* do somthing */

}



b) C-function encapsulation
nop.h:

Code: Select all

extern void nop(void);



nop.c:

Code: Select all

#include \"nop.h\"

void nop(void)
{
/* No code between bracket and pragma */
#pragma ASM
   nop
#pragma ENDASM
/* No code between pragam and bracket */
}

/* No other function in nop.c, except other ASM encapsulation. */


foo.c:

Code: Select all

#include \"nop.h\"

static void function(void)
{
   ... /* do somthing */
    nop();
   ... /* do somthing */

}



In this solution nop.c is not MISRA but the code is isolated.
Foo.c is MISRA.


c) macro-function

nop.h:

Code: Select all

#define nop()\   asm(\"nop\")

/* No other definition in nop.h file, except other assembleur encapsulation */


foo.c:

Code: Select all

#include \"nop.h\"

static void function(void)
{
   ... /* do somthing */
    nop();
   ... /* do somthing */

}


The last solution could have an impact on OBJ file generation.
Some (most of?) compiler requier to compile with a specific option when assembler code is use in C file.
This last solution should compliant with MISRA 2.1 but not with MISRA 1.1
(inline assembler is not C standard).

misra-c
Posts: 547
Joined: Thu Jan 05, 2006 1:11 pm

Postby misra-c » Wed Aug 23, 2006 12:22 pm

MISRA-C meeting 23-8-2006

The previous reply by lv is consistent with our views.
---
Posted by and on behalf of
the MISRA C Working Group


Return to “6.2 Language Extensions”

Who is online

Users browsing this forum: No registered users and 1 guest