问题描述
在Windows中,可以将__asm nop
交换为asm volatile("nop");
(在GCC编译器中使用)并产生相同的结果吗?
In Windows, can __asm nop
be swapped for asm volatile("nop");
(used in GCC compiler) and yield the same result?
我已经阅读到volatile()
(在GCC中)保证不会优化通话.但是,它不能直接移植到Windows,我很好奇它是否可以简单地删除或是否需要用类似的结构替换.
I have read that volatile()
(in GCC) guarantees the call will not be optimized away. However, it doesn't port directly to Windows, and I was curious if it can simply be removed or if it needs to be replaced with a similar construct.
推荐答案
__asm
关键字实现在MSVC中非常简单.它始终会发出未经更改的机器代码,而优化程序不会对其进行处理.对于__asm之后的机器状态,它也不做任何假设,因为它具有击败其他优化的诀窍.
The __asm
keyword implementation is quite simplistic in MSVC. It always emits the machine code unaltered and the optimizer doesn't touch it. Nor does it make any assumptions about machine state after the __asm, that has a knack for defeating other optimizations.
因此,不需要,不需要类似于volatile()
的任何内容,它不会消失.普通__asm { nop }
将始终毫发无损地保存下来,并且等同于GCC组件.
So, no, nothing similar to volatile()
is required, it can't disappear. Plain __asm { nop }
will always survive unscathed and is equivalent to the GCC assembly.
请记住,内联汇编不是一个好的长期策略,对它的支持已在x64编译器中完全删除,并且几乎不可能再出现.您将不得不使用内在函数或链接用汇编语言编写并使用例如ml64.exe进行编译的代码.这确实击败了NOP注入,但是优化器已经很好地照顾了代码对齐,并且不需要帮助.也是您可能根本不应该这样做的原因.
Do keep in mind that inline assembly is not a good long-term strategy, support for it was removed completely in the x64 compiler and is pretty unlikely to ever come back. You'll have to fall back to intrinsics or link code written in assembly and compiled with, say, ml64.exe. That does defeat NOP injection, but code alignment is already well taken care of by the optimizer and doesn't need help. Also the reason you probably should not do this at all.
这篇关于Windows中的"__asm nop"是否等同于GCC编译器中的"asm volatile("nop"));"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!