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