News:

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

getting real numbers from console ..

Started by ishakteyran, March 31, 2008, 04:17:05 AM

Previous topic - Next topic

ishakteyran

can anyone please tell me how i can get real number inputs from console .. ? i use MASM 6.1.5 ..

hutch--

You can't, you can get a string and convert it to a REAL number which is how its normally done.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ishakteyran

so , being an unexperienced newbie to assembly and telling that now i use the latest MASM32 on www.masm32.com and still need to do the conversion to read and print to console with real numbers.. how can i do that.. can any one please provide a few lines of code on how can i do that?

hutch--

Get the STRING from the console, convert it to a REAL number, do whatever FP maths you need to do on it then convert it back to a STRING to display it at the console.

ALL you can enter at the console is characters, not FP numbers. Look at Ray Filitreault's library for conversions both ways and you are in business.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ishakteyran

so may i ask where that library resides ? inside MAsm or is it an external library to be included? if so where can i find it?

hutch--

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

MichaelW

And the long version is:

Raymond Filiatreault's FPULIB version 2.1 is included in the MASM32 package, with the library, include file, and source in the masm32\fpulib directory, and the help in the masm32\help directory. To get the string from the console, display the converted string on the console, and exit the program, you will also need to use several Win32 API functions and/or several MASM32 library functions and/or macros. For help on the MASM32 library functions or macros see the masm32\help directory. For example code see the masm32\examples directory. I'll assume you already know where to get help on the Win32 API.
eschew obfuscation

ishakteyran

thank you man .. but now i am confused :S
because i opened those directories u told about.. but in fpulib directory there are fpu commands such as FpuAdd, FpuSub..etc... but in fputute directory the commands such as fadd, fsub..... are briefly explained ....

which command set is better to use.. and how can i get the include or lib files of those comands to my asm file.. because both the inc file  in fpulib directory and include directory of masm32 are same :S and contains definitions for the commands in FpuAdd, FpuSub forms.. also these commands include the conversion methods from Ascii to FP and Fp to Ascii.. but they seem to have too many parameters .. can someone please help me on how to use these libraries and conversion methods?

Ghirai

Read the documentation associated with FPULib, it's very thorough.
MASM32 Project/RadASM mirror - http://ghirai.com/hutch/mmi.html

Jimg

Okay, all these guys think you're not really trying and have forgotten what a pain it is to get started on something new without a decent example.

Here is a simple program to input a floating point number, square it, and print out the results.

This program could be much much smaller and simpler with the use of some of the macros available in the Masm32 system.  I did this so you could see everything that was going on and maybe learn something.  I'll leave it to the others to show you the easy way.

.586
.MODEL flat, stdcall

OPTION CASEMAP:NONE   ;Case sensitive
.nolist
Include windows.inc
Include kernel32.inc
Include masm32.inc
Include fpu.inc
.listall

IncludeLib kernel32.lib
IncludeLib masm32.lib
IncludeLib fpu.lib

.DATA
Msg1    DB "Please enter a floting point number: ",0
Msg2    DB "You Typed: ",0
Msg4    DB "Press Enter to Exit ",0
CRLF    DB 0dH,0aH,0
Msg5    db "The number entered to 6 decimals was: ",0
Msg6    db "The number squared is: ",0
errin   db "Error converting input number",0dh,0ah,0
errmul  db "Error multiplying number",0dh,0ah,0
lbraket db "<",0
rbraket db ">",0
.DATA?
inbuf   DB 100 DUP (?)
outbuf  DB 100 DUP (?)
fnum    tbyte ?
.CODE

Program:

  Call Main
  Invoke ExitProcess,0

Main Proc
    XOR eax,eax
    invoke StdOut,addr Msg1
    invoke StdIn,addr inbuf,LengthOf inbuf
    mov esi,offset inbuf
    .repeat     ; strip off the crlf
        lodsb
        cmp al,0dh  ; search for cr
    .until zero?
    dec esi
    mov byte ptr [esi], 0
       
    invoke StdOut,addr Msg2
    invoke StdOut,addr lbraket
    invoke StdOut,addr inbuf
    invoke StdOut,addr rbraket
    invoke StdOut,addr CRLF
    invoke StdOut,addr CRLF
   
    ; look in FPULIB.HLP for information on the floating point functions
    invoke FpuAtoFL,addr inbuf,addr fnum,DEST_MEM
    .if eax==0
        invoke StdOut,addr errin
        jmp wrap
    .endif
    invoke FpuFLtoA,addr fnum,6,addr outbuf,SRC1_REAL or SRC2_DIMM       
    ; should test for failure here
       
    invoke StdOut,addr Msg5
    invoke StdOut,addr outbuf
    invoke StdOut,addr CRLF
   
    invoke FpuMul,addr fnum,addr fnum,addr fnum,SRC1_REAL or SRC2_REAL or DEST_MEM
    .if eax==0
        invoke StdOut,addr errin
        jmp wrap
    .endif
    invoke FpuFLtoA,addr fnum,6,addr outbuf,SRC1_REAL or SRC2_DIMM       
    ; should test for failure here
       
    invoke StdOut,addr Msg6
    invoke StdOut,addr outbuf
    invoke StdOut,addr CRLF

wrap:
    invoke StdOut,addr CRLF
    invoke StdOut,addr Msg4
    invoke StdIn,addr inbuf,Lengthof inbuf
    RET
Main EndP

End Program

[attachment deleted by admin]

ishakteyran

thank you man.. this made many things clearer on my mind... i will have one more question if you dont mşnd whilke invoking FP and Ascii conversion methods there is an immediate value parameter ,6, what is that for? and those SRC1_REAL and other last ORed parameters r they defined in fpu.lib and what their functions are?

Jimg

Okay, now you're just trying to get to me.
Did you see the line that said:
    ; look in FPULIB.HLP for information on the floating point functions
?

FpuFLtoA

The FpuFLtoA function converts an 80-bit REAL number to its alphanumeric decimal representation (either in decimal format or scientific notation) and returns the result as a null-terminated string at the specified destination.

FpuFLtoA (

lpSrc1 //pointer to an 80-bit REAL number
lpSrc2 //pointer to, or value of, number of decimal digits to return

lpszDest //pointer to destination of result
uID //ID flags for sources and format
)

Parameters

lpSrc1

This points to an 80-bit REAL number. (This parameter is ignored if uID indicates that the value is already on the FPU.)

LpSrc2

This either points to a DWORD integer in memory, or is an immediate DWORD integer according to the content of uID.

The low byte of the low word of the integer indicates the number of decimal digits to be returned in the result. The total number of integer digits and decimal digits cannot exceed 16. Otherwise, the number of decimal digits will be reduced accordingly. (Note that at least one integer digit is always returned.)

The high byte of the low word of the integer indicates the number of characters before the decimal point, spaces being used for padding. This number cannot exceed 17 (16 digits + sign); it would otherwise be reduced to 17. (This byte is ignored if uID indicates that the result must be returned in the scientific format.)

The high word of this parameter is ignored.

lpszDest

This points to a memory location where the result will be returned. It must be large enough to hold the number of characters returned. At most, it may have 17 characters before the decimal point, one decimal point, 15 decimal digits, and the terminating zero (for a maximum of 34 characters).

uID

One of the SRC1_? flags must be OR'ed with only one of the SRC2_? flags and OR'ed with one of the STR_? flags. (The STR_REG flag does not need to be OR'ed if the string must be returned in decimal format; that is the default.)

uID Flag Meaning


SRC1_FPU Src is already on the FPU
SRC1_REAL Src is a pointer to an 80-bit REAL number

SRC2_DMEM Src2 is a pointer to a 32-bit unsigned integer
SRC2_DIMM Src2 is a 32-bit unsigned integer

STR_REG The string must be returned in regular decimal format
STR_SCI The string must be returned in scientific notation

Return Values:

If the source is considered a valid REAL number, the converted string will be transferred to the specified destination and EAX will be non-zero.
If the function fails, EAX will be zero and the null-terminated string ERROR will be placed at the specified destination.
The source is never modified, whether it is in memory or on the FPU.

If the number is greater than or equal to 10^16, the result will be returned in scientific notation (with the specified number of decimals) regardless of the STR flag. If the number is equal to zero, it is simply returned as an ASCII "0" and a terminating zero regardless of the STR flag.

All the FPU registers and all the CPU registers (except EAX) are preserved.


Example:

invoke FpuFLtoA, 0, 4, ADDR Dest, SRC1_FPU or SRC2_DIMM

In this example, the 80-bit REAL number is taken from the FPU (ignoring the 1st parameter) and the null-terminated alphanumeric decimal string is written in regular decimal format with 4 decimal digits at the destination identified as the memory variable labeled Dest.

kermit

hello,

after quite some time i came back to asm/masm  :wink.

because i'm still a newbie with asm this looked  like an interesting exercise for me too.
( thats actually my first program with fpu commands )
i'm calculating an annual/monthly amount for a given ( console) capital and rate. The code could be shorter, but i wanted to have a copy of the entered real values in the data section.
( i used real8 values because fmul /fdiv can only work directly with 8byte memory operands )
the a2r8 macro is described in /help/hlhelp.hlp - c-Runtime conversions ( it is using  the MSVCRT.dll)
(there is no error checking yet)

maybe u find something useful ...

include /masm32/include/masm32rt.inc

.data
  k100 dq 100.0 ; constant, define floats always with .0 !
  k12 dd 12.0
 
.data?
  capital dq ?
  rate dq ?
  result dq ?
  month dq ?
.code
start:
  mov eax,input("capital ?")
  mov esi,a2r8(eax)
  mov edi,offset capital ; copy qword from buffer to var
  movsd
  movsd
  print real8$(capital),13,10

  mov eax,input("rate ?")
  mov esi,a2r8(eax)
  mov edi,offset rate
  movsd
  movsd
  print real8$(rate),13,10
  fld capital ;load real into st(0)
  fdiv k100 ;st(0)=st(0)/100.0
  fmul rate ;st(0)=st(0)*rate
  fst result ; mov result,st(0)
  fdiv k12 ;st(0)=st(0)/12
  fstp month;mov month,st(0) , pop /clear st(0)
  print "result: "
  print  real8$(result),13,10
  print "per month: "
  print real8$(month),13,10

  exit
end start




ishakteyran

hey yes man it worked.. buyt tell me why those commands are for only 8 bit reals .. ? and for larger FP numbers what commands will i use ?

MichaelW

The code that jimg posted is using 10-byte real numbers. The 10-byte real number format was intended mostly for use within the FPU, or for storing intermediate results in memory. For most calculations the initial values and final results will readily fit in an 8-byte, or even 4-byte, real number.
eschew obfuscation