简体   繁体   中英

How does asm(“nop”); works?

I red the definition from http://en.wikipedia.org/wiki/NOP but I still need a simpler definition.

I stumbled across a code and I don't know exactly what it does:

switch (Something)
    {

        case this_one:
            asm ("nop");
            break;
        case other_one:
            asm ("nop");
            break;
        default:
            asm ("nop");
            break;
    }

nop is an assembly instruction that does nothing--well as close to nothing as you can do and still execute a machine instruction, which means (probably) a REALLY tiny bit of time goes by (which can have limited value in certain realtime applications.

In this case, the statement asm("nop"); makes no semantic difference to the program. The only reason I can think that it might be present is to "force" the compiler to NOT collapse the code paths, making the machine structure of the switch statement visible if you look at the object code or disassemble the machine code or view it in a debugger.

NOP is useful for debugging. Even when doing nothing because means "no operation", NOP is instruction on what you can set breakpoint in debugger. I believe programmer wanted to learn value of "Something" this way. It looks stupid but it took hours for one experienced programmer to find out why

if(Something);
{
 ...
}

was always going inside scope not depending on value of Something . I suspect someone had similar problem with switch() now. If programmer omits NOP , compiler may more easily remove whole switch() statement. I also use __asm__("#start"); for making clear borders for some code in Maximum speed from IOS/iPad/iPhone but __asm__("nop"); would do same too.

Also as already told, it can be used for realtime applications. For example http://www.rickard.gunee.com/projects/video/pic/tetris.php might use this.

NOP is an assembly instruction that means (no operation) as it said It just doesn't do anything, but it is processed by the CPU like any other instruction. So this means that it will be readed from memory, will increment the instruction pointer, but in the execute phase after the instruction decode nothing else will be done. It's frequently used by crackers in reverse ingeniery, but don't know about another uses it will have. I don't think there is any need to use a NOP instruction in C programming.

Since nobody mentioned it, nop can also be useful to "yield" during a critical section, ie allow other interrupts to occur so as to reduce interrupt latency caused by the critical section.

This is typically useful in embedded applications without operating systems where you often have to poll variables and/or status registers during a critical section.

On many RISC processors (ARM, AVR), the instruction immediately following interrupt unmasking will still be masked, so if you just put sei / cli close together, you won't allow any interrupt to occur.

Each nop added between sei and cli allows one more interrupt to occur before the critical section resumes.

As already mentioned, it has uses in the embedded world. Sometimes you need to wait a cycle or two, before a pin is actually set to a certain state, especially on some sensors, etc... In the Paleolithic Era of 6581 cpu, nop was used to generate a delay, since clock was about 0.7mhz... Also what is said about the interrupts is also true.

On Intel x86 atleast, nop is a mnemonic for xchg eax,eax , so it exchanges the eax register with itself. There's other forms which do the same thing but take more space (for eg. lea esi,[esi + eiz] - adds 0 to esi), but these are not the "official" nop instruction.

A nop is a computer instruction that does nothing. In this particular case, it's probably there to prevent the switch from being optimized away. That's implementation specific to the compiler at best, since there's nothing stopping it from parsing and optimizing asm statements.

Why one would do this? It could be for testing the machine code generation somehow. Functionally, it's not useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM