Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to implement APPROXIMATE real time delay , in nS , as required by hardware.
I found this , approximately 8nS "delay" code basically going to bygone days using
assembly "NOP".

So how does "nop\n" actually implement assembly "NOP" ?

#define Delay_8nS __asm volatile ("nop\n");

What I have tried:

Yes, I "do not do assembly " , so that is why I ask.
Updated 2-Oct-21 14:29pm
Rick York 2-Oct-21 20:45pm
If I am not mistaken, the asm keyword is not supported in Visual C++ 64-bit code. If that environment is not one of your targets then this is irrelevent. :)

1 solution

Where Delay_8ns is found in the code, it's replaced with __asm volatile ("nop\n").

Basically, it's adding a very short delay in-line with the code it appears in. A nop instruction just means "no operation". It wastes time by doing nothing.
Greg Utas 2-Oct-21 21:11pm
Further to this answer, volatile prevents the compiler from optimizing out this instruction, even if it actually realizes that it does nothing. And how much time the instruction takes will be specific to the CPU in question, so it's not portable.
Member 14968771 2-Oct-21 21:44pm
Not really what I was asking, but...
So essentially I could write __asm volatile ("LSL 1\n"). Assuming the LSL is "logical shift left one place " then the result would be such on some previous instruction.

Now I recall that in assembly it had to be "NOP" not "nop".
Would my compiler complain it it was case sensitive? Just asking.
Dave Kreskowiak 2-Oct-21 21:47pm
Yes it was. The question has since been modified.

But, the only way to tell if your compiler is going to yell about it is to try it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900