- 为什么
printf("hello world")
最终在汇编代码中使用了比cout << "hello world"
更多的 CPU 指令(不考虑使用的标准库) ?
对于 C++,我们有:
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
对于 C:
movl $.LC0, %eax
movq %rax, %rdi
movl $0, %eax
call printf
- C++ 代码中的第 2 行和 C 代码中的第 2,3 行有什么用?
我正在使用 gcc 4.5.2 版
最佳答案
对于 Linux x86_64 上的 64 位 gcc -O3 (4.5.0),这表示:cout << "Hello World"
movl $11, %edx ; String length in EDX
movl $.LC0, %esi ; String pointer in ESI
movl $_ZSt4cout, %edi ; load virtual table entry of "cout" for "ostream"
call _ZSt16__ostream_insertIcSt11char_traits...basic_ostreamIT_T0_ES6_PKS3_l
并且,对于 printf("Hello World")
movl $.LC0, %edi ; String pointer to EDI
xorl %eax, %eax ; clear EAX (maybe flag for printf=>no stack arguments)
call printf
这意味着,您的顺序完全取决于任何特定的 编译器实现,它的版本和可能的编译器选项。 您的编辑 指出,您使用 gcc 4.5.2(相当新)。 似乎 4.5.2 引入了额外的 64 位寄存器 无论出于何种原因,此序列。它将 64 位 RAX 保存到 RDI 在将其归零之前 - 这完全没有意义(至少对我而言)。
更有趣:3 参数调用序列(g++ -O1 -S source.cpp):
void c_proc()
{
printf("%s %s %s", "Hello", "World", "!") ;
}
void cpp_proc()
{
std::cout << "Hello " << "World " << "!";
}
导致(c_proc):
movl $.LC0, %ecx
movl $.LC1, %edx
movl $.LC2, %esi
movl $.LC3, %edi
movl $0, %eax
call printf
.LCx 是字符串,不涉及堆栈指针!
对于cpp_proc:
movl $6, %edx
movl $.LC4, %esi
movl $_ZSt4cout, %edi
call _ZSt16__ostream_insertIcSt11char_traits...basic_ostreamIT_T0_ES6_PKS3_l
movl $6, %edx
movl $.LC5, %esi
movl $_ZSt4cout, %edi
call _ZSt16__ostream_insertIcSt11char_traits...basic_ostreamIT_T0_ES6_PKS3_l
movl $1, %edx
movl $.LC0, %esi
movl $_ZSt4cout, %edi
call _ZSt16__ostream_insertIcSt11char_traits...basic_ostreamIT_T0_ES6_PKS3_l
你现在明白这是怎么回事了。
问候
rbo
关于c++ - GCC/g++ cout << 与 printf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211646/