【发布时间】:2017-03-19 12:02:19
【问题描述】:
在我写完这个简单的程序后,当我在 Visual Studio 中进入反汇编模式时,我注意到了一些奇怪的事情:编译器添加了一条左移 0 位的指令。
为什么要这样做?
这是 C++ 代码:
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
if (argc != 3)
return 0;
if (strcmp(argv[1], "-r") == 0) {
printf("test");
}
return 0;
}
这是汇编代码:
...
return 0;
00131C94 xor eax,eax
00131C96 jmp main+57h (0131CC7h)
if (strcmp(argv[1], "-r") == 0) {
00131C98 push offset string "-r" (0138B30h)
00131C9D mov eax,4
00131CA2 shl eax,0 <------------------------- HERE
00131CA5 mov ecx,dword ptr [argv]
00131CA8 mov edx,dword ptr [ecx+eax]
00131CAB push edx
00131CAC call _strcmp (01313D9h)
00131CB1 add esp,8
00131CB4 test eax,eax
00131CB6 jne main+55h (0131CC5h)
printf("test");
00131CB8 push offset string "test" (0138BD0h)
00131CBD call _printf (01313E8h)
00131CC2 add esp,4
...
【问题讨论】:
-
你启用优化了吗?
-
似乎只为 x86 添加了移位,而不是 x64。 Release 版本似乎也消失了。
标签: c++ assembly visual-c++