No Op

NoOp is a computer instruction that:

In the C language, the empty statement is one example of a no-op:

 ;

An optimizing C compiler eliminates empty statements and other recognizable no-ops during optimization. It is possible for some no-ops to get by undetected, though, e.g.:

 if (sqrt(9) > 5) {
   printf("Oh, oh...\n");
 }

The if statement is a no-op when a standard-compliant "sqrt" (square root) function is used since 3 is never greater than 5. However, most compilers are either not smart enough to recognize this, or they deliberately leave the code in place, because the user might link the resulting object file against an object file or library that contains a different "sqrt" function, i.e. it cannot be established at CompileTime that it is indeed a no-op.

No-ops can be helpful for removing debugging output in release versions when ConditionalCompilation? is used. For example:

 #if _DEBUG
 /* debugging version */
   #define SAY(x) printf(#x)
 #else
 /* release version */
   #define SAY(x)
 #endif

int main() { SAY(entering foo()); foo(); SAY(left foo()); }


A NoOp function can be useful in a function vector. Many implementations of ForthLanguage contain a NOOP word for this purpose. Sample code from a Forth chess program illustrates one use:

 0 CONSTANT Empty  1 CONSTANT Pawn  ( ... )  6 CONSTANT King
 CREATE genVector  \ each word is ( sq -- sq )
   ' NOOP , ' genPawn , ' genKnight , ' genBishop ,
   ' genRook , ' genQueen , ' genKing , ' genError ,

: genSquare ( sq -- sq ) DUP piece@ CELLS genVector + @ EXECUTE ; : genMoves ['] genSquare forEachSq ;


EditText of this page (last edited May 5, 2005) or FindPage with title or text search