Very good... Notice that values like 123.456789 aren't exact in floating point... You can tweak a little bit and create a routine to present the value in a desired precision... Here's an example (it has a small bug, but I'll leave it to you to discover and fix it):
; main.asm
bits 64
default rel
section .text
extern printfloat
global _start
_start:
movss xmm0, [value]
xor edi, edi ; maximum precision.
call printfloat
; Prints a newline.
mov eax,1
mov edi,eax
lea rsi,[crlf]
mov edx,eax
syscall
; Exits the program with errorcode 0.
mov eax,60
xor edi,edi
syscall
section .rodata
align 4
value:
dd 123.45678 ; this isn't exact in floating point!
crlf:
db `\n`
section .note.GNU-stack noexec
; float.asm
bits 64
default rel
section .text
global printfloat
; Input: XMM0 = x; EDI = precision (maximum # of fractional digits - 0 if all).
printfloat:
cvttss2si r9, xmm0
mov byte [rsp-1], 0
mov r8d, edi
lea rsi, [rsp-1]
mov r10d, 0xcccccccd ; 1/10 scaled.
mov edx, r9d
align 4
.loop1:
mov eax, edx
mov ecx, edx
sub rsi, 1
imul rax, r10
shr rax, 35 ; EAX = n / 10.
lea edi, [rax+rax*4]
add edi, edi
sub ecx, edi ; ECX = n % 10.
add ecx, '0'
mov [rsi], cl
mov ecx, edx
mov edx, eax
cmp ecx, 9 ; reminder sill > 9?
ja .loop1 ; yes, stay in the loop.
mov eax, 1
mov rdx, rsp ; calc the string size.
sub rdx, rsi ;
mov edi, eax
syscall
mov eax,1
mov edi,eax
mov edx,eax
lea rsi, [dot]
syscall
pxor xmm1, xmm1
mov r9d, r9d
cvtsi2ss xmm1, r9
subss xmm0, xmm1
pxor xmm1, xmm1
cvttss2si rdx, xmm0
mov edx, edx
cvtsi2ss xmm1, rdx
subss xmm0, xmm1
movss xmm2, [ten]
mov r9d, r8d
lea rsi, [rsp-1]
pxor xmm3, xmm3
align 4
.loop2:
mulss xmm0, xmm2
pxor xmm1, xmm1
mov edx, edi
cvttss2si ecx, xmm0
movsx eax, cl
add ecx, '0'
cvtsi2ss xmm1, eax
mov [rsi], cl
mov eax, edi
subss xmm0, xmm1
syscall
; Precision testing...
test r8d, r8d
je .skip
sub r9d, 1
je .exit
.skip:
; if fractional part isn't zero yet...
comiss xmm0, xmm3
jne .loop2 ; stay in the loop.
.exit:
ret
section .rodata
dot:
db '.'
align 4
ten:
dd 10.0
section .note.GNU-stack noexec
$ nasm -felf64 main.asm -o main.o
$ nasm -felf64 float.asm -o float.o
$ ld -s -o main main.o float.o
$ ./main
123.45677947998046875
[]s
Fred