flat assembler
Message board for the users of flat assembler.

Index > Windows > How to print a float value?

Author
Thread Post new topic Reply to topic
MikeHart



Joined: 31 May 2005
Posts: 7
MikeHart 30 Jun 2005, 15:44
Hi all,

I try to get this to work:

Code:
format PE CONSOLE 4.0
entry start

Include 'win32a.inc'

section ".code" code readable writeable executable

        string   db "Float value:",0
        szEnv    db "%s %4.2f",0
        valu     dq 6.6

start:
        cinvoke printf,szEnv, string, dword [valu]
        cinvoke getchar
        invoke ExitProcess,0

section '.idata' import data readable writeable

  library kernel32,'kernel32.dll',\
          msvcrt,'msvcrt.dll'

  import kernel32,\
         ExitProcess,'ExitProcess'

  import msvcrt,\
         getchar,'getchar',\
         printf,'printf'                               


As you can see, it doesn't print 6.6 on the console. What mistake did I do?

Cheers
Michael
Post 30 Jun 2005, 15:44
View user's profile Send private message Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 489
Location: Norway
Tommy 30 Jun 2005, 16:31
change dq 6.6 (qword) to dd 6.6 (dword)
Post 30 Jun 2005, 16:31
View user's profile Send private message Visit poster's website Reply with quote
MikeHart



Joined: 31 May 2005
Posts: 7
MikeHart 30 Jun 2005, 18:11
Nope, that didn't help, still get that huge number that has nothing to do with 6.6
Post 30 Jun 2005, 18:11
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8351
Location: Kraków, Poland
Tomasz Grysztar 30 Jun 2005, 18:14
It should be qword, but you didn't pass the whole quad word to the "printf", only the lower half. To pass the whole quad word, do it like:
Code:
        cinvoke printf,szEnv, string, dword [valu], dword [valu+4]    
Post 30 Jun 2005, 18:14
View user's profile Send private message Visit poster's website Reply with quote
MikeHart



Joined: 31 May 2005
Posts: 7
MikeHart 30 Jun 2005, 18:25
Thanks Tomasz very much. That worked. I have to learn a lot but I wanna thank you for building such a great assembler!!!

Michael Hartlef
Post 30 Jun 2005, 18:25
View user's profile Send private message Reply with quote
MikeHart



Joined: 31 May 2005
Posts: 7
MikeHart 24 Dec 2006, 19:50
Ok, how do I display a float value that is stored inside EAX?

Code:
format PE CONSOLE 4.0
entry start 

Include 'win32a.inc' 

section ".code" code readable writeable executable 

        string   db "Float value:",0 
        szEnv    db "%s %4.2f",13,10,0
        valu     dq 6.6 

start: 
        cinvoke printf,szEnv, string, dword [valu], dword [valu+4]
        mov eax , dword [valu]
        cinvoke printf,szEnv, string, eax

        cinvoke getchar 
        invoke ExitProcess,0 

section '.idata' import data readable writeable 

  library kernel32,'kernel32.dll',\ 
          msvcrt,'msvcrt.dll' 

  import kernel32,\ 
         ExitProcess,'ExitProcess' 

  import msvcrt,\ 
         getchar,'getchar',\ 
         printf,'printf'    
Post 24 Dec 2006, 19:50
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade 25 Dec 2006, 11:39
You'll have to use 2 registers:

Code:
mov eax , dword [valu]
mov ebx, dword [valu+4]
cinvoke printf,szEnv, string, eax, ebx    


Otherwise you have the same problem as before, where you were only passing 4 of the 8 bytes in the quadword.
Post 25 Dec 2006, 11:39
View user's profile Send private message Reply with quote
MikeHart



Joined: 31 May 2005
Posts: 7
MikeHart 25 Dec 2006, 17:39
Thank you gunblade and merry chrismas to you!
Post 25 Dec 2006, 17:39
View user's profile Send private message Reply with quote
Garthower



Joined: 21 Apr 2006
Posts: 158
Location: Ukraine
Garthower 07 Feb 2007, 20:51
And how about manual converting float number to string? Somebody has examples? Or ideas? Smile
Post 07 Feb 2007, 20:51
View user's profile Send private message Visit poster's website MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 07 Feb 2007, 21:26
tomasz told to work on it for FASMLIB .... no idea how far it is
Post 07 Feb 2007, 21:26
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 07 Feb 2007, 21:56
Garthower wrote:
And how about manual converting float number to string? Somebody has examples? Or ideas?

This is something I'm still trying to finish, but it goes like this:
(If anything looks wrong please tell me Very Happy)
Code:
;Let D = double (qword)
;
;1) Is D < 0 ? Yes: bMinus = True
;2) D = |D| (Abs)
;3) E10 = Floor(Log10(D))
;4) Is E10 < 0: Yes: bEMinus = True
;5) P10 = 10 ^ -E10
;6) E10 = |E10| (Abs)
;7) D2 = D × P10 (Exponent = 0)
;Cool rbx = D2
;9) rbx --> dst ASCII

fstp qword [value] ;-Part 8
mov rbx,[value]    ;/
mov rsi,[Dst]

@@:

mov rax,rbx
mov rcx,0x199999999999999A
mul rcx
; rdx = rax / 10
mov rax,rbx ; restore rax
mov ebx,rdx ; Required for loop
mov rcx,rdx

shl rcx,1   ;\
shl rdx,3   ; rdx = rdx * 10
add rdx,rcx ;/
sub rax,rdx ; rax = last decimal digit value

add al,'0'  ; ASCII digit base
mov [rsi],al

inc rsi

jif rbx,ne,0,@b ; Done?
; Swap digits back
mov rax,rsi
mov rdi,[dst]
sub rax,rdi
mov rcx,rax
shr rax,1
dec rcx

@@:

dec rsi

mov dh,[rsi]
mov dl,[rdi]
mov [rsi],dl
mov [rdi],dh

inc rdi
dec rax

jif rax,ne,0,@b

;10) Subtract rcx from E10
;11) Do same as 8 & 9 for E10 into dstE
;12) If bMinus = True print "-"
;    print dst
;    print "E"
;    If bEMinus = True print "-"
;    print dstE
    


Maybe I should try finishing it and let a 64bitter try it Cool
Post 07 Feb 2007, 21:56
View user's profile Send private message Reply with quote
Reverend



Joined: 24 Aug 2004
Posts: 408
Location: Poland
Reverend 08 Feb 2007, 20:26
http://board.flatassembler.net/topic.php?t=4377&start=53
There are my converting routines. Just convert float value and save it as a string in buffer, then display it.
Post 08 Feb 2007, 20:26
View user's profile Send private message Visit poster's website Reply with quote
3D_FASM



Joined: 11 May 2023
Posts: 24
3D_FASM 25 May 2023, 13:10
Or use double prefix
Code:
val dq 1234.56769 

...

invoke sprintf, message, format, double [var]
    


https://board.flatassembler.net/topic.php?t=11513
Post 25 May 2023, 13:10
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.