C/C++代码被 VS 2010 优化掉了,如何关闭优化?

来源:互联网 发布:powershell 知乎 编辑:程序博客网 时间:2024/05/17 23:08

作为一个疯狂的汇编C/C++爱好者,写完代码看看被编译成什么自然很好奇,

打开反汇编调试,Debug给的东西很乱:


比如断点指令一个劲填充栈区防止程序跑飞了。

int main(void){00351350  push        ebp  00351351  mov         ebp,esp  00351353  sub         esp,160h  00351359  push        ebx  0035135A  push        esi  0035135B  push        edi  0035135C  lea         edi,[ebp-160h]  00351362  mov         ecx,58h  00351367  mov         eax,0CCCCCCCCh  0035136C  rep stos    dword ptr es:[edi]  
还比如程序运行完成后要清理环境:

return 0;00351488  xor         eax,eax  }0035148A  pop         edi  0035148B  pop         esi  0035148C  pop         ebx  0035148D  mov         esp,ebp  0035148F  pop         ebp  00351490  ret  --- No source file ---------------------00351491  int         3  

这样总是给人一种分心的感觉

所以打开看看Release 版本看看也是很自然的原因。

因为main函数内部的操作对外界没有影响,所以被浓缩为下面两句话也是可以理解的:

01381000  xor         eax,eax  }01381002  ret  

但是问题却来了,怎么看编译结果呢?(在工程名后点属性,跟着蓝线紫线走)



点确定继续

再次编译,是不是比Debug版本干净多了呢?

--- d:\cpp_loves_asm\2_arithmetic\2_arithmetic.cpp -----------------------------/////////////////////////////////////////////////////////////////    2_arithmetic.cpp////    This is a deliberate to demonstrate how //        C++ and ASM as well as C are connected.////    Mighten Dai//    22:32//    Jul 13, 2015/////////////////////////////////////////////////////////////////// ---Part 2: Arithmetic operations#include <iostream>using namespace std;int main(void){00FA1000  push        ebp  00FA1001  mov         ebp,esp  00FA1003  sub         esp,38h  // the unsigned integerunsigned int   ui_arg_1;unsigned int   ui_arg_2;unsigned int   ui_result = 0;00FA1006  mov         dword ptr [ui_result],0  // the signed integersigned int  si_arg_1;signed int  si_arg_2;signed int  si_result = 0;00FA100D  mov         dword ptr [si_result],0  // the floating point numberfloat   f_arg_1;float   f_arg_2;float   f_result = 0;00FA1014  fldz  00FA1016  fstp        dword ptr [f_result]  // the others typeslong intli_test = 0;00FA1019  mov         dword ptr [li_test],0  long long intlli_test = 0;00FA1020  mov         dword ptr [lli_test],0  00FA1027  mov         dword ptr [ebp-1Ch],0  boolb_test = 0;00FA102E  mov         byte ptr [b_test],0  charc_test = 0;00FA1032  mov         byte ptr [c_test],0  ///////////////////////////////////////////////////// the unsigned integer operationsui_arg_1 = 10;00FA1036  mov         dword ptr [ui_arg_1],0Ah  ui_arg_2 = 5;00FA103D  mov         dword ptr [ui_arg_2],5  ui_result = ui_arg_1 + ui_arg_2;00FA1044  mov         eax,dword ptr [ui_arg_1]  00FA1047  add         eax,dword ptr [ui_arg_2]  00FA104A  mov         dword ptr [ui_result],eax  ui_result = ui_arg_1 - ui_arg_2;00FA104D  mov         ecx,dword ptr [ui_arg_1]  00FA1050  sub         ecx,dword ptr [ui_arg_2]  00FA1053  mov         dword ptr [ui_result],ecx  ui_result = ui_arg_1 * ui_arg_2;00FA1056  mov         edx,dword ptr [ui_arg_1]  00FA1059  imul        edx,dword ptr [ui_arg_2]  00FA105D  mov         dword ptr [ui_result],edx  ui_result = ui_arg_1 / ui_arg_2;00FA1060  mov         eax,dword ptr [ui_arg_1]  00FA1063  xor         edx,edx  00FA1065  div         eax,dword ptr [ui_arg_2]  00FA1068  mov         dword ptr [ui_result],eax  ui_result = ui_arg_1 % ui_arg_2;00FA106B  mov         eax,dword ptr [ui_arg_1]  00FA106E  xor         edx,edx  00FA1070  div         eax,dword ptr [ui_arg_2]  00FA1073  mov         dword ptr [ui_result],edx  ///////////////////////////////////////////////////// the signed integer operationssi_arg_1 = -10;00FA1076  mov         dword ptr [si_arg_1],0FFFFFFF6h  si_arg_2 = -5;00FA107D  mov         dword ptr [si_arg_2],0FFFFFFFBh  si_result = si_arg_1 + si_arg_2;00FA1084  mov         eax,dword ptr [si_arg_1]  00FA1087  add         eax,dword ptr [si_arg_2]  00FA108A  mov         dword ptr [si_result],eax  si_result = si_arg_1 - si_arg_2;00FA108D  mov         ecx,dword ptr [si_arg_1]  00FA1090  sub         ecx,dword ptr [si_arg_2]  00FA1093  mov         dword ptr [si_result],ecx  si_result = si_arg_1 * si_arg_2;00FA1096  mov         edx,dword ptr [si_arg_1]  00FA1099  imul        edx,dword ptr [si_arg_2]  00FA109D  mov         dword ptr [si_result],edx  si_result = si_arg_1 / si_arg_2;00FA10A0  mov         eax,dword ptr [si_arg_1]  00FA10A3  cdq  00FA10A4  idiv        eax,dword ptr [si_arg_2]  00FA10A7  mov         dword ptr [si_result],eax  si_result = si_arg_1 % si_arg_2;00FA10AA  mov         eax,dword ptr [si_arg_1]  00FA10AD  cdq  00FA10AE  idiv        eax,dword ptr [si_arg_2]  00FA10B1  mov         dword ptr [si_result],edx  // the floating point number operationsf_arg_1 = 2.43E+7; // 2.43 times 10 to plus 700FA10B4  fld         dword ptr [__real@4bb964f0 (0FA20E8h)]  00FA10BA  fstp        dword ptr [f_arg_1]  f_arg_2 = 1.62E-5; // 1.62 times 10 to minus 500FA10BD  fld         dword ptr [__real@3787e53c (0FA20E4h)]  00FA10C3  fstp        dword ptr [f_arg_2]  f_result = f_arg_1 + f_arg_2;00FA10C6  fld         dword ptr [f_arg_1]  00FA10C9  fadd        dword ptr [f_arg_2]  00FA10CC  fstp        dword ptr [f_result]  f_result = f_arg_1 - f_arg_2;00FA10CF  fld         dword ptr [f_arg_1]  00FA10D2  fsub        dword ptr [f_arg_2]  00FA10D5  fstp        dword ptr [f_result]  f_result = f_arg_1 * f_arg_2;00FA10D8  fld         dword ptr [f_arg_1]  00FA10DB  fmul        dword ptr [f_arg_2]  00FA10DE  fstp        dword ptr [f_result]  f_result = f_arg_1 / f_arg_2;00FA10E1  fld         dword ptr [f_arg_1]  00FA10E4  fdiv        dword ptr [f_arg_2]  00FA10E7  fstp        dword ptr [f_result]  // the others types operationsli_test = 3000000000;00FA10EA  mov         dword ptr [li_test],0B2D05E00h  lli_test = 30000000000000;00FA10F1  mov         dword ptr [lli_test],0EB57E000h  00FA10F8  mov         dword ptr [ebp-1Ch],1B48h  b_test = 0;00FA10FF  mov         byte ptr [b_test],0  b_test = 1;00FA1103  mov         byte ptr [b_test],1  c_test = 'A';00FA1107  mov         byte ptr [c_test],41h  return 0;00FA110B  xor         eax,eax  }00FA110D  mov         esp,ebp  00FA110F  pop         ebp  00FA1110  ret  


0 0
原创粉丝点击