Does printf require additional stack space on the x86-64? [duplicate]

寵の児 提交于 2019-11-28 09:22:17

问题


This question already has an answer here:

  • Printing floating point numbers from x86-64 seems to require %rbp to be saved 1 answer

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.

This short program

        .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"

Could it be an alignment issue? (I get the same problem when value and format are in an .rodata section.)


回答1:


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



来源:https://stackoverflow.com/questions/10324333/does-printf-require-additional-stack-space-on-the-x86-64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!