简体   繁体   English

Windows上的`__asm nop`是否等同于GCC编译器的`asm volatile(“ nop”);`?

[英]Is `__asm nop` the Windows equivalent of `asm volatile(“nop”);` from GCC compiler

In Windows, can __asm nop be swapped for asm volatile("nop"); 在Windows中,可以将__asm nop交换为asm volatile("nop"); (used in GCC compiler) and yield the same result? (用于GCC编译器)并产生相同的结果?

I have read that volatile() (in GCC) guarantees the call will not be optimized away. 我已经读过volatile() (在GCC中)确保不会优化调用。 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. 但是,它不能直接移植到Windows,我很好奇它是否可以简单地删除或是否需要用类似的结构替换。

The __asm keyword implementation is quite simplistic in MSVC. __asm关键字的实现在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. 对于__asm之后的机器状态,它也不做任何假设,因为它具有击败其他优化的诀窍。

So, no, nothing similar to volatile() is required, it can't disappear. 因此,不需要,不需要任何类似于volatile()东西,它不会消失。 Plain __asm { nop } will always survive unscathed and is equivalent to the GCC assembly. 普通__asm { nop }将始终毫发无损,并且等同于GCC程序集。

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. 请记住,内联汇编不是一个好的长期策略,在x64编译器中完全取消了对内联汇编的支持,而且不太可能再出现。 You'll have to fall back to intrinsics or link code written in assembly and compiled with, say, ml64.exe. 您将不得不使用内在函数或链接到用汇编语言编写并使用例如ml64.exe进行编译的代码。 That does defeat NOP injection, but code alignment is already well taken care of by the optimizer and doesn't need help. 那确实击败了NOP注入,但是优化器已经很好地处理了代码对齐问题,并且不需要帮助。 Also the reason you probably should not do this at all. 这也是您可能根本不应该这样做的原因。

For the Microsoft compiler, use the __nop() intrinsic function to emit a nop instruction without handicapping the compiler's optimizer. 对于Microsoft编译器,可以使用__nop()内在函数来发出nop指令,而不会影响编译器的优化器。 This would also be cross-platform across all Windows targets (32 bit ARM V7, 64 bit ARM V8, IA32, X64). 在所有Windows目标(32位ARM V7、64位ARM V8,IA32,X64)上,这也将是跨平台的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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