简体   繁体   English

相当于 C 语言中的 NOP for Embedded?

[英]Equivalent for NOP in C for Embedded?

I use KEIL to compile a program.我使用KEIL编译程序。

The program uses the code程序使用代码

asm("NOP"); asm("NOP");

Unfortunately KEIL compiler does not accept the statement.不幸的是 KEIL 编译器不接受该声明。

The idea is to introduce a delay by using NOP (no operation) assembly code.这个想法是通过使用 NOP(无操作)汇编代码来引入延迟。

What is the actual equivalent of this in C ? C 中 this 的实际等价物是什么? Does this vary with the embedded controller that I use?这是否因我使用的嵌入式控制器而异?

There's an intrinsic nop in most compilers, Keil should have this as well - try __nop()大多数编译器都有一个内在的nop ,Keil 也应该有这个 - 试试__nop()

See - http://www.keil.com/support/man/docs/armccref/armccref_CJABCDAD.htm见 - http://www.keil.com/support/man/docs/armccref/armccref_CJABCDAD.htm

Intrinsic functions are usually safer than directly adding assembly code for compatibility reasons.出于兼容性原因,内部函数通常比直接添加汇编代码更安全。

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.内联汇编不是 C 标准的一部分(还),它因编译器而异,有时甚至在同一编译器的不同目标体系结构之间。 See Is inline asm part of the ANSI C standard?请参阅内联汇编是 ANSI C 标准的一部分吗? for more information.想要查询更多的信息。

For example, for the C51 Keil compiler, the syntax for inline assembly is例如,对于C51 Keil 编译器,内联汇编语法

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

while for ARM , the syntax is something like而对于ARM ,语法类似于

...
__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_ () .对于一些更常见的操作码,一些编译器提供所谓的内在函数 - 这些可以像 C 函数一样调用,但本质上是插入汇编代码,如_nop_ ()

If you are using Keil for ARM Cortex target (eg stm32), you are most probably also using CMSIS library.如果您将 Keil 用于 ARM Cortex 目标(例如 stm32),您很可能也在使用 CMSIS 库。 It has portable macros and inline functions for all assembly instructions written like this: __NOP() .它具有用于所有汇编指令的可移植宏和内联函数,如下所示: __NOP()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM