NASM printf print 64 bit double segfaults

The amateur programmer

I'm trying to get my head around printing doubles in assembly but I'm failing miserably. I'm getting segfault when calling my own function that I'm planning to use as a helper to print doubles for debugging purposes. I was following these printf examples: https://www.csee.umbc.edu/portal/help/nasm/sample.shtml

My code currently looks like this:

section .data
    formatStrf: db `The number is %f\n`,0

section .text

extern printf

printfcallfloat:

    ;Pass in RDI
    PUSH RDI ;Preserve value of rdi
    PUSH RAX ;Preserve value of RAX

    PUSH RDI;The value we want to print
    PUSH DWORD formatStrf
    CALL printf ;Segfault

    POP RAX;Pop the stack back (too lazy to manually change the RSP value)
    POP RAX

    POP RAX;Restore the RAX and RDI
    POP RDI
    RET

I'm passing the floating point value to the RDI reg as follows:

MOVSD QWORD [RSP], XMM0 ;Copy to stack
MOV RDI, QWORD [RSP]
CALL printfcallfloat

EDIT: I'm running this on linux.

Chris Dodd

On x86_64, arguments are passed in registers, and not on the stack (the stack is used only if the size of the arguments to too large to fit in the registers). All the gory details1 are laid out in the SYSV ABI for x86_64

The basics of that is that the first 6 integer/pointer arguments are passed in RDI/RSI/RDX/RCX/R8/R9, while the first 8 float/double arguments are passed in XMM0..XMM7. In addition, you need to specify the number of XMM registers used for arguments in AL2. So in your case, you want the format in RDI, the double value in XMM0 and 1 in AL

The wikipedia page also has lots of good (concise) info about this.


1For non-Microsoft systems -- MS being MS they do things in their own incompatible way

2You only actually need to set this for varargs functions that use at least one XMM register. For non-varargs functions it will be ignored, and if it is set too large for a varargs function, the result will be a few wasted cycles (saving uneeded XMM regs), but wont actually break anything.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Segmentation fault on printf - NASM 64bit Linux

Segmentation fault when using printf in nasm 64bit

Print ARGC in NASM without printf

x64 NASM Assembly extern printf doesn't print anything

NASM - printf doesn't print the first character

NASM issue on OSX 64-bit

NASM x86 print integer using extern printf

Printf float with LLVM segfaults

How to printf a 64-bit integer as hex?

Portable printf format specifier for 64 bit hexadecimal?

Print a variable number of digits of a double using printf

Linking error for 64-bit NASM code with MinGW 32

Assembling 64-bit instructions to raw machine code with nasm

Unable to access array pointer passed to NASM 64 bit DLL

How to use AAD instruction and group in 64-bit NASM?

Convert integer to string in assembly (64-bit, NASM) as fast as possible

On a 64 bit JVM is reading and writing of a double atomic?

How to Print 64-bit Value in GDB

Print hello in 64-bit masm

What's the bit pattern for minimal value in double 64 bit

how a 32 bit processor process 64 bit double value?

multiplying two 32-Bit Numbers and printing the 64 bit result as decimal NASM assembly

Mach-O 64-bit format does not support 32-bit absolute addresses. NASM

How to link 32-bit Nasm assembly object code on a 64-bit windows computer

Issue with Linking a 32-bit NASM file on a 64-bit machine

Recieving 32-bit registers from 64-bit nasm code

Loop with printf in NASM

Linking 64-bit NASM with MinGW-64 causes "File format not recognized" error

Comparing a character in nasm x86-64 using 64 bit registers