Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

NOP

Name: Anonymous 2010-05-13 18:05

What are the differences between
while (condition);
and
while (condition) asm("nop");
?

Name: Anonymous 2010-05-13 18:10

>>1
The second one is portable, and will introduce a redundant nop (unless the compiler peephole-optimizes it away, which is unlikely for asm statements).

Generated code could be:

while (...);

loop:
...condition.../test/cmp
jcc loop

vs

while (condition) __asm { nop }

loop:
...condition.../test/cmp
jcc end_loop
nop
jmp loop
end_loop

Of course, the compiler is free to invert the jcc's as it sees fit if it reduces instruction count or makes the code faster, so those are just rough guesses of possible outputs from the compiler.

Name: Anonymous 2010-05-13 18:10

s/is portable/is not portable//

Name: Anonymous 2010-05-13 19:02

How is what OP posted no different from an infinite loop/thread killer if it is entered?

Name: Anonymous 2010-05-13 19:12

void f(bool c){ return c ? f(c) : return; }

Name: Anonymous 2010-05-13 21:31

>>4
A function can return different results each time it is called (that mean's it's not a functional function... that is, it relies of side-effects), of course, the arguments themselves can change within the condition as well. Here's a practical example:
while(is_data_processed()) sleep(1);
That could would pause/yield(not wasting much CPU cycles) the current thread, until some data is done processing. Of course, real OSes might have more specialized APIs for async events (here's a Windows example):
WaitForSingleObject(hEvent,INFINITE)
In that case, the thread would be yielded indefinetly until the event is signaled (which would cause that thread to be switched to (eventually) and that function returning). The second example is slightly less wasteful on CPU cycles, but the first one isn't that bad either (unless is_data_processed() is something very CPU intensive, which it shouldn't be).

Name: Anonymous 2010-05-13 22:48

>>6
It's used as a method of thread synchronization.  Thanks.

Don't change these.
Name: Email:
Entire Thread Thread List