printf的是否需要在X86-64额外栈空间? [英] Does printf require additional stack space on the x86-64?

查看:272
本文介绍了printf的是否需要在X86-64额外栈空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道这是最好使用编译器内在函数,而对于这个问题, printf_chk ,也把数据 .RODATA 部分,我期待在获得汇编语言更深入的了解,并有兴趣在紧凑code。也有一些是关于的printf 我不明白。我知道放在哪里的参数,我知道如何使用%人的可变参数,但它似乎需要,我可以不占额外的堆栈空间。

While I know it is best to use compiler intrinsics, and for that matter, printf_chk, and also to put data in .rodata sections, I'm looking at gaining a deeper understanding of assembly language and am interested in compact code. There is something about printf I don't understand. I know where to put the parameters, and I know how to use %al for varargs, but it appears to be requiring additional stack space that I cannot account for.

这短节目

        .text
        .globl  main
main:
        movsd   value(%rip), %xmm0    # value to print
        movl    $format, %edi         # format string
        movl    $1, %eax              # one floating-point arg
        call    printf
        movl    $0, %eax              # return 0 from main
        ret
        .align 8
value:  .double 74.321 
format: .asciz "%g\n"

给出了一个段错误。

gives a segfault.

然而,当我添加额外的堆栈空间的框架,它工作正常:

However, when I add additional stack space to the frame, it works fine:

        .text
        .globl  main
main:
        subq    $8, %rsp              # ADD SOME STACK SPACE TO FRAME (WHY?)
        movsd   value(%rip), %xmm0    # value to print
        movl    $format, %edi         # format string
        movl    $1, %eax              # one floating-point arg
        call    printf
        movl    $0, %eax              # return 0 from main
        addq    $8, %rsp              # REMOVE ADDED STACK SPACE
        ret
        .align 8
value:  .double 74.321 
format: .asciz "%g\n"

难道是对齐问题? (我得到了同样的问题时,格式是在 .RODATA 部分。)

推荐答案

的堆栈必须对齐的16字节,根据www.x86-64.org/documentation/abi.pdf并且还微软的 http://msdn.microsoft.com/en-us/library/ms235286 (v = VS.80)的.aspx

The stack must be 16-byte aligned, according to the www.x86-64.org/documentation/abi.pdf and also Microsoft's http://msdn.microsoft.com/en-us/library/ms235286(v=vs.80).aspx

这篇关于printf的是否需要在X86-64额外栈空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆