雖然我知道最好是使用編譯器內在函數,對於這個問題,printf_chk
,也可以將數據放在.rodata
部分,我正在研究對彙編語言有更深入的理解,並對壓縮代碼感興趣。有一些關於printf
我不明白。我知道把參數放在哪裏,並且我知道如何使用%al
作爲可變參數,但它似乎需要額外的堆棧空間,我無法解釋。printf在x86-64上需要額外的堆棧空間嗎?
這套短節目
.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"
給出了一個段錯誤。
然而,當我添加額外的堆棧空間的框架,它工作正常:
.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"
難道是對齊問題? (我得到了同樣的問題時value
和format
處於.rodata
部分。)
http://stackoverflow.com/questions/8691792/how-to-write-assembly-language-hello-world-program-for-64-bit-mac-os-x – 2015-07-08 14:29:45