I have a C code in UNIX where I need to keep my processor doing nothing for 2 seconds. In order to do that, I found the assembly instruction 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. 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:
e.g., on a 8 MHz processor, one cycle takes 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. On POSIX systems (like UNIX), use POSIX nanosleep functions.