News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

acii to float algorithm

Started by masmforummem, April 02, 2011, 11:23:10 AM

Previous topic - Next topic

masmforummem

Hi,

I do have some source code for this algorithm, but can anyone please give me the psedo code. For example, to covert a decimal to binary we divide it by 2 repeatedly. I mean I need the algorithm. I do not need the implementation.
I am new to assembly, so the algorithm is really important.

Thank you very much.


FORTRANS

Hi,

   Nice little problem, so I coded up something.  I should have
done it long ago.  Here are the code comments.  Stuff in curly
brackets are sort of optional?  HTH

Cheers,

Steve N.


; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
; 1.)  Zero INumber and IDivide.  FINIT to initialize FPU.  {Option, set
;      maximum character count ICount.}
; 2.)  Get character.
; 3.)  Test character.
; 3.a    End? => Go to Exit1 (5.).
; 3.b    Decimal point? => Move one into IDivide (optional error if not
;                          first time).  Go to 4.
; 3.c    Digit? => Mul INumber and IDivide by ten.  Convert digit to binary
;                  and add to INumber.  Go to 4.
; 3.d    Other => Barf, {warn,} or ignore.
; 4.)  {Decrement ICount, Jump Zero Exit1.}  Go to 2.
; 5.)  Exit1 label:  FILD INumber, {test IDivide for non-zero,}
;      FIDIV IDivide.
; 6.)  Store floating point number or whatever.

dedndave

some time ago, Jochen (JJ2007) did a a lot of work on this subject
the forum search tool should yield complete routines to use, with timing comparisons

jj2007

Here are my teststrings, in case you want to develop your own algo. Otherwise, check the MasmBasic documentation in \masm32\MasmBasic\MbGuide.rtf for
Val():    mov eax, Val(Any$)
MovVal:   MovVal MyVar, Any$ where MyVar can be just about everything including REAL4/8/10, qwords and xmm regs.


.data ; Teststrings
TestStrings dd Ts0, Ts1, Ts2, Ts3, Ts4, Ts5, Ts6, Ts7
MyString db "00000987654321.01234567800000", 0
MyPI db "3.141592653589793238", 0
MyPI_Int db "314159265", 0
Ts0 db "3.141592653589793238", 0
Ts1 db "00987654321.01234567800000000", 0
Ts2 db " -1.234567890 (leading tab)", 0
Ts3 db "-123456789012345678", 0
Ts4 db "12345678901234567890.12345678901234567890", 0
Ts5 db "123456.7890123456789e118", 0
Ts6 db "-123456.7890123456789e-118", 0
Ts7 db "$FB7E0E41", 0
Ts8 db "0FB7E0E41h", 0
Ts9 db "0xFB7E0E41", 0
Ts10 db "10101010101b", 0
Ts11 db "10101010101y", 0

masmforummem

Thanks to you all for the quick replies.

And I did figure out how to do it by carefully reading and testing the code I have. However, I would appreciate it if Steve could make your pseudo code clearer. For example, what are INumber and IDivide? and at 3.b    Decimal point? => Move one into IDivide. Move what? And 3d: Other => Barf, {warn,} or ignore

In fact, I am reading the book - Programming from the Ground up. And I want to write my own routine to read a float string from the keyboard. I would want as many algorithms to do this task as possible.

P.S
I would appreciate it if you use GAS code.

I do not have any formal education in advanced math (I did not go to university). Can you advise a book that teaches you this kind of stuff we are discussing here?

dedndave

well - first, you want to understand the various number formats used by the FPU
it is important to note that you are going to see some little variations, like NaN's for example
the rest is basically base or "radix" conversion - that will give you some search terms for google
as for the FPU operation and instructions, Ray has a nice tutorial, as well as some libraries, etc

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

jj2007

Quote from: masmforummem on April 03, 2011, 06:12:17 AM
P.S
I would appreciate it if you use GAS code.

You mean we should rewrite our code for you?

masmforummem

Thank you all.

@jj2007. No. That's why I said I would  :bg

FORTRANS


Quote from: masmforummem on April 03, 2011, 06:12:17 AM
Thanks to you all for the quick replies.

And I did figure out how to do it by carefully reading and testing the code I have. However, I would appreciate it if Steve could make your pseudo code clearer. For example, what are INumber and IDivide?

Hi,

   INumber and IDivide are binary representations of an integer.
INumber is the input number scaled up to an integer.  I.e if
you enter 12.34 it would be 1234 in (probably) a DWORD.
IDivide is the scale factor to create the fraction.  In this example
it would be 100.

Quoteand at 3.b    Decimal point? => Move one into IDivide. Move what?

   At that time you have entered an integer and are about to
enter the fraction the character is a decimal point or a period.
So you put the number one (1) into IDivide as the scale factor
for the current integer ( 1:1 ).  It was set to zero at the start
so you could multiply it by ten with no side effects until you need
to scale the fraction.  As you enter the fraction, I needed to
prepare a scaling factor to convert the unscaled integer I am
creating into the desired integer plus the fraction.

Quote
And 3d: Other => Barf, {warn,} or ignore

   If you enter a character that is not part of a legal number,
then what do you want to happen?  Choices that occurred to
me were, abort the program, disallow the character and notify
the user, or ignore it and carry on.  Loosely translated into slang?
Since this was a simple exercise in coding, I ended up ignoring
it as an easy way out.  For a real bit of a usable program that
sounds just a bit tacky to me.  But...  What would you want?

Quote
In fact, I am reading the book - Programming from the Ground up. And I want to write my own routine to read a float string from the keyboard. I would want as many algorithms to do this task as possible.

P.S
I would appreciate it if you use GAS code.

   You asked for pseudo-code.  So that is what I tried to
give you.  I coded it in MASM for MS-DOS, so it is not GAS
compatible.  What I gave you was essentially the notes I
made as I coded up (and tested) the routine.

Quote
I do not have any formal education in advanced math (I did not go to university). Can you advise a book that teaches you this kind of stuff we are discussing here?

   This is not advanced mathematics.  It is simple arithmetic.
It is the conversion from an algorithm to working code that
might be of some difficulty for a beginner.

   Well, a book on starting to program in assembly.  Or more
accurately an intermediate level book.  All mine are quite old,
and none do this exact task.  So, your book is probably okay
for "learning the ropes".  Or learning how to code a program.

   You need to also learn to think out how to come up with an
algorithm to solve your problems.  Pseudo-code is good if you
can follow the logic.  Write out what you think is required to
convert a string of characters to a valid floating point number.

   (Really, get a pen and paper.  What is your input?  What does
it look like?  What is the output supposed to be?  How do you
get from here to there?  Back in my earlier posting is what I
came up with.  Give or take debugging.  Post what you come
up with?)

   I came up with about six steps.  As I coded things, step three
got complex enough to break into sub-steps.  And some things
changed as they were found to be wanting.

   If what I am describing is still confusing, ask Dave or jj2007
for an exact pointer to the thread Dave mentioned for real
code to do the conversion.  I made a naive search without
much luck.  Or ask me for more information to clarify, though
this is normally about my limit.  As the others will attest, I
don't always make much sense to them.

Good luck,

Steve N.



masmforummem

Many thanks for your time and kindness. I'll come back if I should have any further question.