【问题标题】:Difference at assembly level of printf from cstdio and iostreamprintf 汇编级别与 cstdio 和 iostream 的差异
【发布时间】:2016-12-31 00:13:27
【问题描述】:

所以这个问题只是出于好奇。 我有一些小程序:

#include <some_header>
void print(){ printf("abc"); } // don't care about main, I'm not gonna run it

然后我将它编译为程序集,一次是 some_header=>iostream,另一次是 some_header=>cstdio 和 gcc.godbolt.org(6.1 for x86_64)和 -O3 -pedantic -std=c++14。看看这个:

.LC0:
        .string "abc"
print(): (iostream) or (both included)
        movl    $.LC0, %edi
        xorl    %eax, %eax
        jmp     printf
        subq    $8, %rsp
        movl    std::__ioinit, %edi
        call    std::ios_base::Init::Init()
        movl    $__dso_handle, %edx
        movl    std::__ioinit, %esi
        movl    std::ios_base::Init::~Init(), %edi
        addq    $8, %rsp
        jmp     __cxa_atexit
print(): (cstdio)
        movl    $.LC0, %edi
        xorl    %eax, %eax
        jmp     printf

它们之间有很大的不同,前三行是相同的,那么为什么iostream 需要这么多的代码来清理或者这些行只是在做什么?或者只是说 Godbolt 在这项任务上不可靠?


此外,标准似乎并不能保证 printf 可以从 iostream 访问,是否应该依赖这一点?

【问题讨论】:

  • jmp 之后的位不是该函数的一部分。它们是另一个功能的一部分,由于某种原因没有标记。
  • @immibis 啊,我应该注意到了,但有趣的是,即使我只包含iostream,即使使用-Os,这部分代码也存在,所以我怀疑这是一些动态初始化程序或类似的东西。

标签: c++ c io


【解决方案1】:

在这两种情况下,您的打印函数都会编译成几乎相同的汇编代码。 您看到的其他行是初始化和取消初始化 iostream 库。如果您删除优化标志-O3,您可能会清楚地看到这一点。 这是包含 iostream 并关闭优化的完整列表。

std::piecewise_construct:
        .zero   1
.LC0:
        .string "abc"
print():
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $.LC0, %edi
        movl    $0, %eax
        call    printf
        nop
        popq    %rbp
        ret
__static_initialization_and_destruction_0(int, int):
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $16, %rsp
        movl    %edi, -4(%rbp)
        movl    %esi, -8(%rbp)
        cmpl    $1, -4(%rbp)
        jne     .L4
        cmpl    $65535, -8(%rbp)
        jne     .L4
        movl    std::__ioinit, %edi
        call    std::ios_base::Init::Init()
        movl    $__dso_handle, %edx
        movl    std::__ioinit, %esi
        movl    std::ios_base::Init::~Init(), %edi
        call    __cxa_atexit
.L4:
        nop
        leave
        ret
        pushq   %rbp
        movq    %rsp, %rbp
        movl    $65535, %esi
        movl    $1, %edi
        call    __static_initialization_and_destruction_0(int, int)
        popq    %rbp
        ret

【讨论】:

  • 每当我包含iostream时,这是强制性的吗?
  • @YiFei 这就是库的特定实现所做的。另一种实现可能表现不同。 C++ 标准没有指定这些实现细节。
猜你喜欢
  • 2012-03-28
  • 2018-12-20
  • 2012-07-31
  • 2019-09-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2021-01-15
  • 1970-01-01
相关资源
最近更新 更多