C语言 asm("nop"); 作品?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24359632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:11:29  来源:igfitidea点击:

How does asm("nop"); works?

cassembly

提问by Alexandru Cimpanu

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

我将http://en.wikipedia.org/wiki/NOP 中的定义红色化,但我仍然需要一个更简单的定义。

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;
    }

回答by Dwayne Towell

nopis 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.

nop是一条什么都不做的汇编指令——就像你可以做的几乎什么都不做,但仍然执行机器指令,这意味着(可能)真的会流逝一点点时间(这在某些实时应用程序中可能具有有限的价值。

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.

在这种情况下,该语句asm("nop");对程序没有语义差异。我认为它可能存在的唯一原因是“强制”编译器不折叠代码路径,如果您查看目标代码或反汇编机器代码或查看它,则使 switch 语句的机器结构可见调试器。

回答by kuroi neko

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

由于没有人提到它,nop在临界区期间“让出”也很有用,即允许其他中断发生以减少临界区引起的中断延迟。

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/cliclose together, you won't allow any interrupt to occur.

在许多 RISC 处理器(ARM、AVR)上,紧跟在中断取消屏蔽之后的指令仍将被屏蔽,因此如果您只是将sei/cli关闭在一起,则不会允许任何中断发生。

Each nopadded between seiand cliallows one more interrupt to occur before the critical section resumes.

每个nop添加之间seicli允许在临界区恢复之前再发生一个中断。

回答by T?nu Samuel

NOPis useful for debugging. Even when doing nothing because means "no operation", NOPis 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

NOP对调试很有用。即使什么都不做,因为意味着“无操作”,NOP是关于您可以在调试器中设置断点的说明。我相信程序员想通过这种方式学习“某物”的价值。看起来很愚蠢,但一位有经验的程序员花了几个小时才找出原因

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/iPhonebut __asm__("nop");would do same too.

总是进入范围内而不取决于Something. 我怀疑现在有人对 switch() 有类似的问题。如果程序员省略NOP,编译器可能更容易删除整个switch()语句。我还用于__asm__("#start");IOS/iPad/iPhone 的最大速度中的某些代码制作清晰的边界,但__asm__("nop");也会这样做。

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

同样如前所述,它可以用于实时应用程序。例如http://www.rickard.gunee.com/projects/video/pic/tetris.php可能会使用它。

回答by sir psycho sexy

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.

NOP 是一条汇编指令,意思是(无操作),正如它所说的它只是不做任何事情,但它像任何其他指令一样由 CPU 处理。所以这意味着它将从内存中读取,将增加指令指针,但在指令解码后的执行阶段不会做任何其他事情。它在反向工程中经常被饼干使用,但不知道它还有其他用途。我认为没有必要在 C 编程中使用 NOP 指令。

回答by Dare The Darkness

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.

如前所述,它在嵌入式世界中有用途。有时需要等待一两个周期,才能真正将引脚设置为某个状态,尤其是在某些传感器等上……在 6581 cpu 的旧石器时代,使用 nop 来生成延迟,因为时钟大约是0.7mhz...关于中断的说法也是正确的。

回答by Ketan Kanishka

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.

至少在 Intel x86 上, nop 是 的助记符xchg eax,eax,因此它会与自身交换 eax 寄存器。还有其他形式可以做同样的事情但占用更多空间(例如lea esi,[esi + eiz]-将 0 添加到 esi),但这些不是“官方” nop 指令。

回答by Per Johansson

A nopis 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 asmstatements.

Anop是一条什么都不做的计算机指令。在这种特殊情况下,它可能是为了防止开关被优化掉。这充其量是特定于编译器的实现,因为没有什么可以阻止它解析和优化asm语句。

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

为什么有人会这样做?它可能以某种方式测试机器代码生成。从功能上讲,它没有用。