Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 795
AAI Avatar asked Sep 10 '14 10:09

AAI


2 Answers

There's an intrinsic nop in most compilers, Keil should have this as well - try __nop()

See - https://www.keil.com/support/man/docs/armcc/armcc_chr1359124998347.htm

Intrinsic functions are usually safer than directly adding assembly code for compatibility reasons.

like image 151
Leeor Avatar answered Oct 03 '22 01:10

Leeor


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_ ().

like image 35
Andreas Fester Avatar answered Oct 02 '22 23:10

Andreas Fester