Search code examples
cembeddedinline-assemblykeilno-op

Equivalent for NOP in C for Embedded?


I use KEIL to compile a program.

The program uses the code

asm("NOP");

Unfortunately KEIL compiler does not accept the statement.

The idea is to introduce a delay by using NOP (no operation) assembly code.

What is the actual equivalent of this in C ? Does this vary with the embedded controller that I use?


Solution

  • Does this vary with the embedded controller that I use?

    Yes. Inline assembly is not part of the C standard (yet), it varies from compiler to compiler and sometimes even between different target architectures of the same compiler. See Is inline asm part of the ANSI C standard? for more information.

    For example, for the C51 Keil compiler, the syntax for inline assembly is

    ...
    #pragma asm
          NOP
    #pragma endasm
    ...
    

    while for ARM, the syntax is something like

    ...
    __asm  {
              NOP
           }
    ...
    

    You will need to check the manual for the actual compiler you are using.

    For some of the more common opcodes, some compilers provide so-called intrinsics - these can be called like a C function but essentially insert assembly code, like _nop_ ().