【发布时间】:2013-11-18 08:43:26
【问题描述】:
我写了以下代码:
int main()
{
int i;
int arr[4];
for (i = 0; i < 4; i++)
arr[i] = 0;
return 0;
}
将其保存在两个文件中:test.c 和 test.cpp
我运行以下命令:
gcc -O0 test.c
objdump -Mintel -d a.out > decompilec
g++ -O0 test.cpp
objdump -Mintel -d a.out > decompilecpp
我将 decompilec 和 decompilecpp 都编辑为只包含 main 函数。
现在,我运行 diff decompilec decompilecpp 并得到以下输出:
12,21c12,19
< 80483fe: 7e eb jle 80483eb <main+0xf>
< 8048400: b8 00 00 00 00 mov eax,0x0
< 8048405: c9 leave
< 8048406: c3 ret
< 8048407: 66 90 xchg ax,ax
< 8048409: 66 90 xchg ax,ax
< 804840b: 66 90 xchg ax,ax
< 804840d: 66 90 xchg ax,ax
< 804840f: 90 nop
<
---
> 80483fe: 0f 9e c0 setle al
> 8048401: 84 c0 test al,al
> 8048403: 75 e6 jne 80483eb <main+0xf>
> 8048405: b8 00 00 00 00 mov eax,0x0
> 804840a: c9 leave
> 804840b: c3 ret
> 804840c: 66 90 xchg ax,ax
> 804840e: 66 90 xchg ax,ax
任何机构都可以解释这种差异吗?
这里是文件:
反编译
080483dc <main>:
80483dc: 55 push ebp
80483dd: 89 e5 mov ebp,esp
80483df: 83 ec 20 sub esp,0x20
80483e2: c7 45 ec 00 00 00 00 mov DWORD PTR [ebp-0x14],0x0
80483e9: eb 0f jmp 80483fa <main+0x1e>
80483eb: 8b 45 ec mov eax,DWORD PTR [ebp-0x14]
80483ee: c7 44 85 f0 00 00 00 mov DWORD PTR [ebp+eax*4-0x10],0x0
80483f5: 00
80483f6: 83 45 ec 01 add DWORD PTR [ebp-0x14],0x1
80483fa: 83 7d ec 03 cmp DWORD PTR [ebp-0x14],0x3
80483fe: 7e eb jle 80483eb <main+0xf>
8048400: b8 00 00 00 00 mov eax,0x0
8048405: c9 leave
8048406: c3 ret
8048407: 66 90 xchg ax,ax
8048409: 66 90 xchg ax,ax
804840b: 66 90 xchg ax,ax
804840d: 66 90 xchg ax,ax
804840f: 90 nop
反编译cpp
080483dc <main>:
80483dc: 55 push ebp
80483dd: 89 e5 mov ebp,esp
80483df: 83 ec 20 sub esp,0x20
80483e2: c7 45 ec 00 00 00 00 mov DWORD PTR [ebp-0x14],0x0
80483e9: eb 0f jmp 80483fa <main+0x1e>
80483eb: 8b 45 ec mov eax,DWORD PTR [ebp-0x14]
80483ee: c7 44 85 f0 00 00 00 mov DWORD PTR [ebp+eax*4-0x10],0x0
80483f5: 00
80483f6: 83 45 ec 01 add DWORD PTR [ebp-0x14],0x1
80483fa: 83 7d ec 03 cmp DWORD PTR [ebp-0x14],0x3
80483fe: 0f 9e c0 setle al
8048401: 84 c0 test al,al
8048403: 75 e6 jne 80483eb <main+0xf>
8048405: b8 00 00 00 00 mov eax,0x0
804840a: c9 leave
804840b: c3 ret
804840c: 66 90 xchg ax,ax
804840e: 66 90 xchg ax,ax
根据要求,这是gcc -v的输出:
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
【问题讨论】:
-
那么您希望 C 和 C++ 生成 相同 机器指令吗?如果是这样,为什么?机器指令必须是等价的,而不是完全相同的。 所以问题是,它们是否等效?。但是,你知道足够的汇编来理解它们吗?
-
我不敢相信一年半前还有一个几乎相同的问题:stackoverflow.com/questions/9338164/…
-
这似乎是一个完全合理的问题,其中包含足够的代码来重现问题和遇到的具体问题。我希望它重新开放!
-
另外,这个 GCC 错误报告(已修复)可能与此有关:gcc.gnu.org/bugzilla/show_bug.cgi?id=42027
-
伙计们,你们抽过什么东西吗?这是一个零初始化循环,具有恒定的迭代次数(最好是在一个不使用的变量上,然后立即超出范围)。打开
-O1几乎肯定会去掉整个代码并将程序简化为int main(){return 0;},无论使用什么编译器或它可能有什么错误。甚至尝试从一个空的main函数中预测某些东西有什么意义? :-)
标签: c++ c disassembly