nop

This is a discussion on nop within the Tech Board forums, part of the Community Boards category; I was writing a sleep function earlier.. and I was wondering, would there be any difference between an empty loop ...

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    nop

    I was writing a sleep function earlier.. and I was wondering, would there be any difference between an empty loop running until the time elapsed or a loop with some inline assembler and the command nop (no operation)? What exactly does nop do anyway? No operation for one CPU cycle?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786

    Re: nop

    Originally posted by Eibro
    I was writing a sleep function earlier.. and I was wondering, would there be any difference between an empty loop running until the time elapsed or a loop with some inline assembler and the command nop (no operation)? What exactly does nop do anyway? No operation for one CPU cycle?
    NOP is just an "ignore me......I do nothing" instruction....Usefull if your hex editing and you need to erase a section of code

    Most sleep functions (Sleep() in WINAPI for instance) dont work with a loop, they tell the system to not preempt the thread that called the sleep function for the duration that was passed in the function...

    IE Sleep(1000).....roughly means dont issue this thread any CPU time for 1 second after this call

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    NOP's opcode is the same as AND ax,ax. i'm not sure how much time, if any, the instruction takes up, or how the computer handles the ambiguity.

  4. #4
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    What exactly does nop do anyway? No operation for one CPU cycle?
    NOP is a mnemonic placeholder. Many times in code, particularly assembler, you might want some scratch space to work with, without allocating a RAMblock. You could just have a small block of NOPs and the address to that RAM and use that. It makes it very convenient for Drivers.

    Or, for some reason, you might require an instruction to begin on a cpu-word boundary-- in such case you'd have to pad the code with NOPs.

    A NOP wastes 1 cycle.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NOP in 'C'?
    By danlee58 in forum C Programming
    Replies: 18
    Last Post: 01-26-2009, 11:56 AM
  2. dll to encrypt packet sends
    By splintter in forum C++ Programming
    Replies: 22
    Last Post: 04-20-2008, 09:00 PM
  3. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 08:23 PM
  4. nop - revisited
    By oioi in forum Tech Board
    Replies: 4
    Last Post: 12-28-2004, 09:18 AM
  5. Is there anything wrong with my LCD code?
    By ooicheesan in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2004, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21