flat assembler
Message board for the users of flat assembler.

Index > Main > Printing a Float number on the screen

Author
Thread Post new topic Reply to topic
White-spirit



Joined: 26 Mar 2008
Posts: 27
White-spirit
Hello, i've writted a little code in Asm for my OS pour display a Float number on the screen, but it displays for example 28.0710391998291015625 instead of 28.07104 Confused , here's my code :
Code:
format elf

public ftol
public print_float
extrn print
extrn putchar
extrn itoa

half dd 0.5
i dd ?
tmp dd ?
e dd ?

ftol:
fld [half]
fld dword [esp+4]
fsub st0, st1
fld dword [esp+4]
faddp st2, st0
fistp [i]
mov eax, [i]
fistp [i]
add eax, [i]
sar eax, 1
ret

print_float2:
push ebp
mov ebp, esp
mov ebx, dword [esp+8]
mov [tmp], ebx
push [tmp]
call ftol
add esp, 4
mov [e], eax
push 0
push 0
push 10
push [e]
call itoa
add esp, 16
push eax
call print
add esp, 4
push '.'
call putchar
add esp, 4
fld [tmp]
fisub [e]
fst [tmp]
.do:
push 10
fimul dword [esp]
fst [tmp]
push [tmp]
call ftol
add esp, 4
mov [e], eax
push 0
push 0
push 10
push [e]
call itoa
add esp, 16
push eax
call print
add esp, 4
fisub [e]
fst [tmp]
ftst
fstsw ax
fwait
sahf
jnz .do
leave
ret
    


Thanks for help Smile
Post 08 Apr 2008, 13:41
View user's profile Send private message Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP
Use a rounding function, I've recently used one in a high-level language course in school but I'm afraid I cannot provide it to you.
Post 08 Apr 2008, 21:12
View user's profile Send private message Visit poster's website Reply with quote
sakeniwefu



Joined: 23 Mar 2008
Posts: 29
sakeniwefu
Wait I realized I was stupid. Learn to do it in float.
OK. adding +-(5/10^precision+1) and reading it up to precision should round it arithmetically, but it doesn't look optimal. I just cannot find any web or source that will explain how to round floats without libraries or by hand.
Or just round down.


Last edited by sakeniwefu on 09 Apr 2008, 01:11; edited 3 times in total
Post 09 Apr 2008, 00:37
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
you can also try to convert from the binary image of the floating point value with the needed decimals.
decode it and dispaly it without any FP instructions. it will free the FPU that is supposed to calculate other things at the same time.

and better, it will be able to convert any FP in any format with only the general regisers.
Post 09 Apr 2008, 00:42
View user's profile Send private message Visit poster's website Reply with quote
AlexP



Joined: 14 Nov 2007
Posts: 561
Location: Out the window. Yes, that one.
AlexP
Sakeniwefu found my idea Smile
Post 09 Apr 2008, 12:55
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.