News:

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

FPU: load a number from keyboard

Started by thiagoFragoso, May 14, 2006, 02:04:31 PM

Previous topic - Next topic

thiagoFragoso

I just want that the user, may type a real number (1.342,for instance) and I can store this number this way (as a real number, because I'm using int 16h for store each number typed, but if there's a ".", the result fails, because of ascii code of "."),and then use it in an operation (like a calculator) in FPU, but I don't know how to do this: How can I receive a number from keyboard and remodel it for FPU use?? :'(
Thanks in advance!!!! :U

EduardoS

You can do by the easy way,
Get the number as a integer ignoring the point, load it in the fpu and divide by 10^(number of digits after point)

raymond

There are numerous ways to cater to that situation.

Although the following tutorial was prepared for 32-bit assembly, you can still gather the necessary information if you really want to learn and you are up to the task. The available FPU library is also for 32-bit assembly. After you become familiar with FPU instructions (most of them are the same for 16-bit and 32-bit assembly), you could adapt the source code of the FpuAtoFL function to your needs.

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

The other option is to use fixed point math. You may want to learn a bit more on this subject from another page on the same site:

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

Or, you could handle all numbers as you would with pencil-and-paper, using Binary Coded Decimals (BCDs). Yet another page dealing with that subject is available on the same site.

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

Simply ask if you need any clarification. DON'T GIVE UP.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

thiagoFragoso

Thanks, now I know the way to represen a float number!But the operations with FPU are returning 0. Following code is the macro I've created. What colud be wrong???

Thanks very much.


soma macro x,y
      finit
      fld   x
      fld   y
      fadd   st(1),st(0)
      fst   resultado
      endm
this is correct, is-nt this???

raymond

The addition would be correct if x and y are valid numbers in the floating point format. Otherwise, the result of the addition could be anything. Read Chapter 2 in the FPU tutorial on what the floating point format is expected to be.

Furthermore, even if the above was correct, the result of the addition would be in the st(1) register. The "fst   resultado" would copy the content of the st(0) register (i.e. the value of the y variable) to the "resultado" variable.

You will have to study a few more chapters of the tutorial.
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

ChrisLeslie

Hi ThiagoFragoso

This should do it:
soma macro x,y,result
  local x
  local y
  local result
  finit
  fld x
  fld y
  fadd st(0),st(1)
  fwait
  fstp result
endm

... with x and y being preserved in this case.
For inputing from the keyboard, my preference, for flexibility, would be to input the whole number as a string, then convert the string to float for processing. Finally, convert your result to a string for display.
Using 16 bit, I think that it is a good idea to initally write your own keyboard string input procedure or macro for all subsequent use.
For the conversions you can find lots of ATOF/FTOA type of procedures using Google.

Good luck

Chris

raymond

Chris,

If ThiagoFragoso (or anyone else) would continue to perform FPU additions according to your suggested code without the finit preceeding each and every addition (or other instructions to free registers), he would start getting garbage results after the 7th addition because not enough registers would be free.

That is why I am suggesting strongly that he gets familiar with the ins and outs of the FPU before he gets totally frustrated with it. The FPU is a complex monster which can be tamed only when you know how. :dance:

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

asmfan

Reverse polish notation still usefull, isn't it. It's better to use faddp st(1), st(0). it pops unneede second trashed register.
Russia is a weird place

FuLL_MaSSa

i'm in same trouble as thiago...
i want read floats from keyboard, work with them and print the results on screen.
i've seeked ATOF/FTOA documents and found some macros and libraries...
but i don't know how to use (include) these libraries in my projetct.
i'm trying to use fpu.lib and float.lib... if i could use them, i think my problems should be over.. and i bet thiago's too ;)

macros: http://www2.dsu.nodak.edu/users/mberg/ASSEMBLY/Numbers/numbers.html

please, somebody can help me (or us)?

thanks!  :U

PS: sorry about the 16bit post, i'm new at assembly programming... :'(

ChrisLeslie

FuLL_MaSSa and thiagoFragoso

I am just re-iterating Ray Filiatreault's suggestion earlier. If you download the masm32 package you will find a floating point package by Ray. The procedures FpuFLtoA and FpuAtoFL will do the conversions in the masm32 environment. If you study the package you will see that you should be able to adapt it to your requirements.

A better solution is to just use the masm32 package, including the fpulib, for your assembler work. Why do you want to do it with 16bit code anyway?

If you are really serious about fpu then try google more information about how fpu works and then spend a few weeks tearing your hair out trying to write your own conversion programs. You will really learn a great deal that way!

Hope that helps you
Chris

raymond

QuoteIt's better to use faddp st(1), st(0)

When using MASM  (either 16-bit or 32-bit), it's even easier than that. Simply use fadd without any parameter. :8) It gets coded as the above quoted instruction.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com