views:

44

answers:

1

I am trying to convert a double to a string on the stack from x86_64 assembly code:

        bs.code += isa.movsd(registers.xmm0, MemRef(registers.rsp))
        bs.code += isa.pop(registers.rax)

        bs.code += isa.push(registers.rbp)
        bs.code += isa.mov(registers.rbp, registers.rsp)

        bs.code += isa.sub(registers.rsp, 100)
        bs.code += isa.and_(registers.rsp, -16)

        bs.code += isa.mov(registers.rdi, registers.rsp)
        bs.code += isa.mov(registers.rsi, <address of "%i\0">)
        bs.code += isa.mov(registers.rax, <address of sprintf in libc>)
        bs.code += isa.call(registers.rax)

The program segfaults at call(rax) with

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6a2919b in *__GI___overflow (f=0x7fffffffb5d0, ch=9698128) at genops.c:248
warning: Source file is more recent than executable.
248   return _IO_OVERFLOW (f, ch);

I think sprintf has to be called specially because it uses variable arguments, so can anybody advise on the proper way to do this from assembly?

+1  A: 

Would it help if you write a simple call to sprintf in C and use gcc -s foo.c ?

CaseyJones
I tried this but had to do -fno-stack-protector to get simple code.
Forrest