Convert String to floating point number

Started by amrac, January 05, 2011, 01:19:25 AM

Previous topic - Next topic

amrac

I have a procedure that converts a string to a number but I'm not shure if I'm doing things correctly. The first part of the procedure checks if the characters are valid, that is, if there are only digits, the '.' character and the '-' character. But then I load the string and the number addresses:

lea     di,temp_bcd+9   ;beginning of number
   lea    si,szdecimal   ;beginning of string


These variables are declared in the data segment as:

temp_bcd     dt   ?         ;number
szdecimal    db   32 dup( ), '$'   ;string


I've debugged this code step by step and I can garanty that the string isn't empty.
Then I have the code to write the characters to the number:

copia:   mov    al,[si]         ;coloca o caracter mais à esquerda da string
      cmp    al,'-'
      je       negativo
      cmp    al,'.'
      je       ponto
      cmp    al,0
      je       sai
      and    AL,0Fh
      stosb
      inc    si
      jmp    copia
ponto:   mov    al,[si]
      stosb
      inc    si
      jmp    copia
negativo:
      mov    al,[si]
      stosb
      inc    si
      jmp    copia
sai:   pop     di
      pop     si
      ret 
input_error:
      xor     ax,ax        ;to return with error code
      pop     di
      pop     si
      ret

a2bcd ENDP



Then in the main program I just do a:

      fld    temp_bcd
      fstp   temp_bcd

just to see the number's value in the stack. But what goes to the stack is a zero. What am I doing wrong?

dedndave

i see a lot of problems here

right off....
if the value is negative - endless loop because SI never gets incremented
well - the loop will end eventually, because STOSB does increment DI   :P
the same thing happens if it is a decimal point

to load a BCD value into the FPU, use FBLD
the value has to be packed BCD - two digits stored in each byte - 20 decimal digits (10 bytes) max
with no decimal point and no negative sign

i could write some code for you
but - use the forum search tool
this thing has been done to death
google, too


dedndave

is there a reason for using 16-bit code ?
things are so much better with 32-bit   :U

oh - assuming you want an integer result, use FIST or FISTP to store the value as integer
Ray has a great FPU tutorial...

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

if you poke around on his site, you will also find a library - probably already has the code written for you   :U

raymond

As Dave mentioned, you can learn a lot from the link he gave you if you really want to learn how to use FPU code.

The library of FPU functions obviously has one to convert ascii to float which, based on 32-bit code, would not be directly usable to you if you insist on programming in 16-bit code. However, the library is also supplied with the source code of each function. You could then study that source code and modify it as required for use with 16-bit code.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Gunther

Hi amrac,

you shouldn't invent the wheel again. Have a look at the link, which dedndave provided. Raymond's FPU material is a well written and good maintained library and it is part of the MASM32 project. You should give it a try.

Gunther
Forgive your enemies, but never forget their names.

raymond

Quoteyou shouldn't invent the wheel again

I may agree partly with that statement. However, compared to reinventing the wheel to write an OS or an assembler (which a few members here have attempted), this would be a very small undertaking and could be an excellent exercise to improve his programming and logic skills. Numerous details must be covered if this function is to perform correctly 100% of the time.

If he can tackle this exercise, he will be better prepared to write more demanding functions or programs which may not be readily available. The material in the link is there to provide him with the basic tools to understand what he's doing and guide him through some of the process.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com