How to print floating point number in assembly without library function i have try this but it it did not work for large numbers and i don't know this is correct way or not.
code:
global print_float64
section .text
print_float64:
mov r9 , 0
mov r8 , 0
pxor xmm1 , xmm1
ucomisd xmm0 , xmm1
jnbe .positive
mov r9 , 1
lea rsi , [rsp-8]
mov qword[rsi] , -1
cvtsi2sd xmm1 , [rsi]
mulsd xmm0 , xmm1
.positive:
xor rax , rax
cvttsd2si eax , xmm0
mov rdi , 10
mov rsi , rsp
dec rsi
mov byte[rsi] , '.'
.while1:
xor rdx , rdx
div rdi
add rdx , 48
dec rsi
mov [rsi] , dl
cmp rax , 0
jnbe .while1
cmp r9 , 1
jne .print
dec rsi
mov byte[rsi] , '-'
.print:
mov rax , 1
mov rdi , 1
mov rdx , rsp
sub rdx , rsi
syscall
mov rax , rsp
sub rax , rsi
add r8 , rax
;------------------------------------------------------------------------------------------
xor rax , rax
cvttsd2si eax , xmm0
cvtsi2sd xmm1 , eax
subsd xmm0 , xmm1
mov rax , 100000
cvtsi2sd xmm1 , eax
mulsd xmm0 , xmm1
xor rax , rax
cvtsd2si eax , xmm0
mov rdi , 10
mov rsi , rsp
.while2:
xor rdx , rdx
div rdi
add rdx , 48
dec rsi
mov [rsi] , dl
cmp rax , 0
jnbe .while2
mov rax , 1
mov rdi , 1
mov rdx , rsp
sub rdx , rsi
syscall
mov rax , rsp
sub rax , rsi
add rax , r8
ret
logic for this code is it first prints number before the precision point by integer conversion and it will print 5 digits after precision point by multiplying by 100000 and again convert into integer and print. it the may be works for larger number if i convert 64 bit float to 64 bit int(rax) but i did not find the instruction to do this
1)What is the best way to print floating point number in assembly without library function.
I am using nasm with ubuntu 64
Thanks.