News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Read string, convert it to decimal number and print the result

Started by elquau, July 08, 2023, 07:26:39 AM

Previous topic - Next topic

elquau

Hi everyone, I'm a human from Mexico.

I'm stuck with this simple but out of my league problem: I need to prompt for two numbers and then sum such numbers and display the result, I already make it work with "mov value1, sval(input("First value: "))" but it only sum the integer part of the number disregarding the decimal part of it, can you please help me?

Thanks in advance.  :thumbsup:

zedd151

Hello, and welcome to the forum!  :thumbsup:
Post the code that you already have, so we may better help you.


Quote from: elquau on July 08, 2023, 07:26:39 AM
but it only sum the integer part of the number disregarding the decimal part of it, can you please help me?

Sounds like you want to add two floating point numbers?

jj2007

Hi elquau,

Welcome to the forum :thup:

The sval() returns an integer - no good if you expect decimals. You need the a2r8() macro:

include \masm32\include\masm32rt.inc

.data?
result REAL8 ?

.code
start:
  mov eax, a2r8(input("First value:  "))
  fld REAL8 ptr [eax]
  mov eax, a2r8(input("Second value: "))
  fld REAL8 ptr [eax]
  fadd
  fstp result
  inkey real8$(result)
  exit

end start


I suppose this is homework. You should study \Masm32\macros\macros.asm, especially the a2r8 and real8$ macros, just in case somebody asks to explain what you did ;-)

NoCforMe

That's certainly one way.

Another way, for folks like me who dislike using macros (yes, we exist!) would be to use Raymond Filiatreaut's excellent FPU library which is part of the MASM32 package. It's in \masm32\fpulib, and includes a help file (Fpulib.chm) as well as all the libraries and include files.

It's a little more complicated than what JJ showed us here, so if you want super-simple, use his example. Ray's library includes functions to convert between ASCII representations of floating-point numbers and actual floating-point #s (like REAL10s), as well as math and trig functions.
Assembly language programming should be fun. That's why I do it.

elquau

Thanks everyone.

jj2007 Tha'ts exactly what I was looking for. Thank very much.