1

I have a problem with write a call of printf on x86_64 linux assembler. If i try print a double value when rsp % 0x10 != 0, printf a fall down with segfault.

Look to my code (nasm syntax):

[bits 64]
global _start
extern printf
extern pow

section .data
    printf_format db '%lf', 10, 0
section .text
    _start:
    mov rbp, rsp
    sub rsp, MEM_VAL

    mov rax, 0x4000000000000000
    mov qword [rsp], rax

    movsd xmm0, qword [rsp]
    mov rdi, printf_format
    mov rax, 1
    call printf

    mov rax, 60
    mov rdi, 0
    syscall

If MEM_VAL = 0x10, 0x20, ... , 0x100, ... all is good. Code work. But with another value code is down with segfault. Why?

I compile and link with this:

nasm -f elf64 ex.asm
ld -lc -lm -m elf_x86_64 -I/lib/ld-linux-x86-64.so.2 ex.o -o ex

Taking this opportunity, I will ask you, where I can read a spec to C calling convention in x86_64 linux? I know that it is an fastcall, but i can't understand a thinness like this.

  • 3
    Probably because the call requires stack alignment. See: http://stackoverflow.com/questions/8691792/how-to-write-assembly-language-hello-world-program-for-64-bit-mac-os-x – Diego Basch Dec 22 '12 at 05:52
  • Also, (and I might have told you this already) if you use libc you should use `main` as entry point and `gcc` to link, so that the proper initialization happens. Similarly, don't use the exit syscall just return from `main` or call the `exit` libc function if you really must. – Jester Dec 22 '12 at 14:01
  • http://stackoverflow.com/questions/10324333/does-printf-require-additional-stack-space-on-the-x86-64 – Ciro Santilli OurBigBook.com Jul 08 '15 at 14:28

0 Answers0