简体   繁体   English

GCC编译器执行asm volatile(“ nop” /::)指令需要多长时间?

[英]How long it takes to GCC compiler execute an asm volatile (“nop”/::) instruction?

I have a C code in UNIX where I need to keep my processor doing nothing for 2 seconds. 我在UNIX中有一个C代码,我需要保持处理器2秒钟不执行任何操作。 In order to do that, I found the assembly instruction asm volatile("nop"::) . 为此,我找到了汇编指令asm volatile(“ nop”::) I've searched a lot but I couldn't find anywhere explaining how can I calculate the necessary number of NOPs to execute my function for exactly 2 seconds. 我进行了很多搜索,但找不到任何地方可以解释如何计算2秒钟内执行我的功能所需的NOP数。 Can anyone help me? 谁能帮我?

for(i = 0; i < COUNTER; i++){
    asm volatile ("nop"::);
}

The NOP instruction takes one cycle in most microprocessors, so do the math: 在大多数微处理器中,NOP指令占用一个周期,因此数学公式也是如此:

eg, on a 8 MHz processor, one cycle takes 1 / 8 MHz = 125 ns. 例如,在8 MHz处理器上,一个周期占用1/8 MHz = 125 ns。 You then have to add few cycles more for the management of the loop. 然后,您必须再增加几个循环来管理循环。

If you are in an environment with an OS you should not rely on NOP instructions to wait for seconds and should not use an active wait. 如果您所在的操作系统是OS,则不应依靠NOP指令等待几秒钟,也不应使用主动等待。 On POSIX systems (like UNIX), use POSIX nanosleep functions. 在POSIX系统(如UNIX)上,使用POSIX nanosleep函数。

只需包含unistd.h并使用:

unsigned int sleep(unsigned int seconds);

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

相关问题 Windows上的`__asm nop`是否等同于GCC编译器的`asm volatile(“ nop”);`? - Is `__asm nop` the Windows equivalent of `asm volatile(“nop”);` from GCC compiler __asm __volatile 如何执行? - How does __asm __volatile execute? asm(“nop”)怎么样; 作品? - How does asm(“nop”); works? 如何在C编译器的ASM输出中启用DIV指令 - How to enable the DIV instruction in ASM output of C compiler 为什么这个内联汇编不能为每条指令使用单独的asm volatile语句? - Why is this inline assembly not working with a separate asm volatile statement for each instruction? Windows 中 asm(&quot;nop&quot;) 的实现 - Implementations for asm("nop") in windows GCC内存屏障__sync_synchronize vs asm volatile(“”:::“memory”) - GCC memory barrier __sync_synchronize vs asm volatile(“”: : :“memory”) 为什么 `asm volatile(""::: "memory")` 可以用作编译器屏障? - Why can `asm volatile("" ::: "memory")` serve as a compiler barrier? Linux上的这个GCC错误是什么,我该如何解决? gcc:内部编译器错误:非法指令(程序为) - What is this GCC error on Linux, and how do I solve it? gcc: internal compiler error: Illegal instruction (program as) 如何调试包装在 __asm volatile 修饰符中的代码? - How to debug code wrapped in __asm volatile modifier?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM