2

I tried to use printf within assembly code to print some floats (doubles to be precise), but unfortunately it doesn't work.

Code (Intel syntax)

global start
extern exit
extern printf

section .data

x dq 3.14
format db '%f',0

section .text

start:
    default rel ;64-bit uses realtive addressing
    
    mov rbp, rsp
    and rsp, 0xFFFFFFFFFFFFFFF0 ;stack alignment
    
    movsd xmm0, [x]
    lea rdi, [format]
    mov rax, 1
    call printf
        
    mov qword[rsp], 0
    call exit

To build I use

nasm seven.asm -fmacho64 --prefix _
ld seven.o -o seven -arch x86_64 -lc -e _start -macosx_version_min 10.6

And the output is simply 0.000000

I have Macbook Pro 2013 with Intel Core i5 processor, OS X El Capitan

EDIT

Thanks to you guys, the answer is nasm bug, which appears somewhere in 2.11.06-2.11.08 version, causing everrything in data section to be overwritten except last instruction. And the soulution was to revert to 2.11.05.

Community
  • 1
  • 1
Vergir
  • 21
  • 3
  • How do you know the number to be printed is passed in `xmm0`? Maybe compile a simple C application that does just that, disassemble it and take a look to get to know the ABI. – user4520 Oct 31 '15 at 17:49
  • Works on my machine. – fuz Oct 31 '15 at 17:50
  • 1
    @szczurcio I consulted [AMD64 ABI](http://www.x86-64.org/documentation/abi.pdf), and every guide out there insist on this ABI, so I'm pretty sure it is right to pass doubles in `xmm0`. By the way, Disassembling leads to (almost) same code. – Vergir Oct 31 '15 at 21:22
  • @FUZxxl Could you please share your nasm version (`nasm -v`) and ld version (`ld -v`)? – Vergir Oct 31 '15 at 21:35
  • nasm 2.11.05 and GNU ld 2.25. I tested this on Linux, so the result might be different. – fuz Oct 31 '15 at 21:59
  • 1
    Any chance you are encountering this bug? (What version of NASM are you using) http://stackoverflow.com/questions/30385380/awkward-data-section-behavior-with-nasm – Michael Petch Oct 31 '15 at 22:26
  • If you're using nasm 2.11.08, this might be the [known bug](http://stackoverflow.com/a/31736983/224132) with data-section labels. Since FUZxxl reports that it works on Linux, this seems likely. I think the OS X function-call ABI is *exactly* the same as the Linux 64bit ABI, including passing the first 8 float/double args in xmm registers. And passing the number of vector regs used as arguments to a varargs function in `al` – Peter Cordes Oct 31 '15 at 22:30

0 Answers0