简体   繁体   中英

assembly x86 nasm outputting float values

I"m working on a project and I need to output floating point values using printf from C.

I'm able to print out the values right now, but I'm not really sure how the printing is taking place.

For printing int values, I just push the data type and location and it prints fine.

But for float its a whole different story.

Here is code for printing floats

mov qword rdi, float_format                          
push qword 0                                         
push r14                                            
movsd xmm0, [rsp]                                    
mov qword rax, 1                                     
call printf                                          
pop rax                                               
pop rax

That will effectly print out a value that was inputed from the keyboard. However, when I manipulate the number with float commands like fadd or fdiv , in order to print out, prior to calling the print float function I have to mov 1, rax

Ideas? To make it simpler? Or explanation as to why I"m using xmm0?

You need to know the calling conventions in use. Linux on AMD64 uses the System V AMD64 ABI . From that document we learn that:

  • integer arguments are passed in rdi, rsi, rdx, rcx, r8 and r9
  • floats are passed in xmm0 to xmm7
  • for varargs functions the number of SSE registers used is put in rax

So for the call

printf (format, float_value)

you have rdi=format, xmm0=float_value and rax=1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM