10

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?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
AAI
  • 290
  • 1
  • 3
  • 20
  • Some embedded compilers provide an intrinsic function, `__delay_cycles(constant)` that emit code to wait a number of cycles. I'm not sure the Keil compile do, however. – Lindydancer Sep 10 '14 at 10:22
  • What target? - Keil's ARM tools use ARM's compiler (unless it is really old, from before Keil were aquired by ARM), while for other targets they use their own compilers. The proprietary extensions differ between the two. However all will have a means of embedding in-line assembler; refer to the user manual for inline-assembly syntax, and refer to your processor's instruction set to the appropriate no-op instruction. – Clifford Sep 10 '14 at 19:21

3 Answers3

7

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.

Leeor
  • 19,260
  • 5
  • 56
  • 87
5

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

Community
  • 1
  • 1
Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • 2
    Inline assembler is actually standardized in C++, using the syntax that is in the original question. Sadly, C has yet to standardize it. Because of this, it is probably best to hide inline assembler inside functions or macros, so that you encapsulate it. If you port the code to a different compiler or CPU in the future, you only need to rewrite the encapsulated part. – Lundin Sep 11 '14 at 08:45
1

If you are using Keil for ARM Cortex target (e.g. stm32), you are most probably also using CMSIS library. It has portable macros and inline functions for all assembly instructions written like this: __NOP().

Amomum
  • 6,217
  • 8
  • 34
  • 62