flat assembler
Message board for the users of flat assembler.

Index > Main > Convert string to number

Author
Thread Post new topic Reply to topic
6a00



Joined: 11 Sep 2009
Posts: 5
6a00 04 Nov 2009, 15:45
Who knows how convert string like this '-35.027' into floating point number and reverse?
Post 04 Nov 2009, 15:45
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 04 Nov 2009, 18:10
Quote:

`FILD' loads an integer out of a memory location, converts it to a
real, and pushes it on the FPU register stack. `FIST' converts `ST0'
to an integer and stores that in memory; `FISTP' does the same as
`FIST', but pops the register stack afterwards.
Post 04 Nov 2009, 18:10
View user's profile Send private message Visit poster's website Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 04 Nov 2009, 18:19
rugxulo wrote:
Quote:

`FILD' loads an integer out of a memory location, converts it to a
real, and pushes it on the FPU register stack. `FIST' converts `ST0'
to an integer and stores that in memory; `FISTP' does the same as
`FIST', but pops the register stack afterwards.

He wants to take a string and turn it into a Floating point number.



If you are programming for windows, I think there is a function for this. If not(or if there is no function), you would need to parse the string yourself.

EDIT: There's another topic on this subject here:
http://board.flatassembler.net/topic.php?t=9338

EDIT 2: http://board.flatassembler.net/topic.php?t=7717

_________________
----> * <---- My star, won HERE


Last edited by windwakr on 04 Nov 2009, 20:37; edited 2 times in total
Post 04 Nov 2009, 18:19
View user's profile Send private message Reply with quote
kohlrak



Joined: 21 Jul 2006
Posts: 1421
Location: Uncle Sam's Pad
kohlrak 04 Nov 2009, 20:11
If you ask me, windwakr, unless he understand BCD, that link has very, very little to work with.

anyway, 6a00, i ended up giving up on this a long, long time ago and most people do. The FPU is such a painful subject (you'll find it's slow and SSE is faster but still painful to use), so really most people don't use it unless they have functions already available for it like atof and ftoa.
Post 04 Nov 2009, 20:11
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger Reply with quote
6a00



Joined: 11 Sep 2009
Posts: 5
6a00 05 Nov 2009, 19:44
I found that my code gives inaccurate result
Code:
proc _ascii2float           ;in edi pointer to data in ecx number of characters
    push ebx edx eax

          xor eax,eax
          push word[edi]
          ffree st7
          ffree st6
          ffree st5
          fld1
          fldz
          fldz

.loop:    dec ecx
          js .endloop
          mov al,byte[edi]
          inc edi
          cmp al,'.'
          je .loop2
          xor al,30h
          cmp al,9
          jg .loop
          mov [_tmp],ax
          fimul word[_ten]
          fiadd word[_tmp]
          jmp .loop

.loop2:   dec ecx
          js .endloop
          mov al,byte[edi]
          inc edi
          xor al,30h
          cmp al,9
          jg .loop2
          mov [_tmp],ax
          fxch st2
          ;st0 = 1
          fidiv word[_ten] ;there is problem
          ;st0 =0.1000000000000000056
          fxch st2
          fxch st1
          fincstp
          ffree st7
          fldz
          fiadd word[_tmp]
          fmul st,st2
          fxch st1
          fadd st,st1
          jmp .loop2

.endloop: fstp st1
          fstp st1
          pop cx
          cmp cl,'-'
          jnz @f
          fchs

@@: pop eax edx ebx
ret
_ten dw 10
_tmp dw 0
endp 
    
Post 05 Nov 2009, 19:44
View user's profile Send private message Reply with quote
farrier



Joined: 26 Aug 2004
Posts: 274
Location: North Central Mississippi
farrier 05 Nov 2009, 20:12
6a00,

Raymong Filiatreault has a site:

http://www.ray.masmcode.com/

with libraries and tutorials on using the FPU and BCD numbers. In the FPU library are routines to convert from decimal to FPU and FPU to decimal. Very robust but not necessarily optimized for speed. For my own use I converted--with Raymond's permission--his library to a DLL. The code is in Masm but easy to understand +/or use.

hth,

farrier

_________________
Some Assembly Required
It's a good day to code!
U.S.Constitution; Bill of Rights; Amendment 1:
... the right of the people peaceably to assemble, ...
The code is dark, and full of errors!
Post 05 Nov 2009, 20:12
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 06 Nov 2009, 11:57
I also posted (unpublished) FASNMLIB code for ascii2float (with all sorts of input and range checking) sometime ago on this board.
Post 06 Nov 2009, 11:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number 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.