News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

atoflt

Started by jimg, February 28, 2024, 11:43:08 AM

Previous topic - Next topic

Vortex

Nidud's stdlib.inc defining some structures :

_CRT_DOUBLE struct
    x real8 ?
_CRT_DOUBLE ends
_CRT_FLOAT struct
    f real4 ?
_CRT_FLOAT ends
_LONGDOUBLE struct
    x real10 ?
_LONGDOUBLE ends

https://github.com/nidud/asmc/blob/master/include/stdlib.inc

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/atodbl-atodbl-l-atoldbl-atoldbl-l-atoflt-atoflt-l?view=msvc-170

int _atodbl( _CRT_DOUBLE * value, char * str );
int _atodbl_l ( _CRT_DOUBLE * value, char * str, _locale_t locale );
int _atoldbl( _LDOUBLE * value, char * str );
int _atoldbl_l ( _LDOUBLE * value, char * str, _locale_t locale );
int _atoflt( _CRT_FLOAT * value, const char * str );
int _atoflt_l( _CRT_FLOAT * value, const char * str, _locale_t locale );


jimg

Thank you.
I got
atoflt3.obj : error LNK2001: unresolved external symbol __atoflt
so something amiss with the whole system on this computer (my wife's laptop, we're on vacation right now).
Will run down the problem, or worse case wait 'till I get home.

Probably why I couldn't get any of the other attempts to work also.

jimg

Thank you Erol, your file totally works if built from a command prompt.
My machine is still screwed up, so I still get the error when building from my normal environment.
I'll keep working on it :)

jimg

Just to perpetuate this farce, it works if I remember to include-

includelib \masm32\lib\ucrt.lib

DUH!

jimg

 And the real solution to my original problem is here

Yes, again I forgot sprintf format %f wants a double.  All this time wasted on a triviality.  It was just my checkprint in error, not the actual return value.  Duh again.  Hopefully someone sometime will be able to get some good out of this mess.

jj2007

Quote from: jimg on February 29, 2024, 09:34:50 AMI forgot sprintf format %f wants a double

There is _atodbl, same parameters. Or use fld MyFloat & fstp MyDouble.

jimg

Yes indeed.


For anyone hanging around this long to find a solution, here is my final test program.
I did it three ways, once with atofl, once with the fpulib, and once with the fpulib but straight to a double.
After all that, I'm going with the fpulib. In my original case, I needed a single, so I'll use that.
Thanks to everyone that contributed and had great patience with my fumbling.




.686p
.model  flat, stdcall
.nolist
include windows.inc

inv equ invoke
; some miscellaneous macros
soff Macro QuotedText:Vararg    ; returns offset to a string
Local LocalText
.data
LocalText db QuotedText,0
.code
EXITM <offset LocalText>
Endm

msgx Macro qtext:VARARG
    pusha
    inv MessageBox,0,soff(qtext),0,0
    popa
endm

uselib  MACRO   libname
    include     libname.inc
    includelib  libname.lib
ENDM

uselibs macro libs:vararg
    for nlib,<libs>
        include nlib.inc
        includelib nlib.lib
    endm
endm
uselibs user32,kernel32,comctl32,msvcrt,fpu
includelib \masm32\lib\ucrt.lib
.listall

.data?
hWin  dd ?
buff  db 100 dup (?)
buff2 db 100 dup (?)
sfloat dd ?
dfloat dq ?
atoflt dd ?
.data
xfloat dq 123.45677
.code

_atoflt  PROTO C :DWORD,:DWORD

Program:

   inv _atoflt,addr sfloat,soff("12345.67")
   fld sfloat
   fstp dfloat
   inv crt_sprintf,addr buff2,soff("results= %f"),dfloat
   inv MessageBox,0,addr buff2,0,0
 
   ; try fpu
   inv FpuAtoFL,soff("12345.67"),addr sfloat,DEST_MEM4
   .if eax==0
      msgx "error in fpuatofl"
      jmp wrap
   .endif
   fld sfloat
   fstp dfloat
  inv crt_sprintf,addr buff2,soff("results= %f"),dfloat
  inv MessageBox,0,addr buff2,0,0
 
  ;might have just as well output to double to begin with
   inv FpuAtoFL,soff("6543.21"),addr dfloat,DEST_MEM8
   .if eax==0
      msgx "error in fpuatofl"
      jmp wrap
   .endif
  inv crt_sprintf,addr buff2,soff("results= %f"),dfloat
  inv MessageBox,0,addr buff2,0,0
 
wrap:   
    inv ExitProcess, eax

end Program


jimg

Hi, it's me again....

I thought I'd just tack on here since it's a similar type of question.

Would someone like to show how to use strtoull in masm/uasm?

I'd appreciate it.  I can't seem to get the appropriate declaration, and even if I did, I couldn't find where the results would be returned to in masm.

jimg

Ah, just discovered the actual name is _strtoull_l
now that I can call it, just need to figure out where the return value is.

and just discovered you can just call it with strtoull.

didn't need the underline at all.   I made it too complicated from the beginning.  Still looking for output however.

jimg

And finally guessed right.  It's returned in eax:edx

e.g.

includelib \masm32\lib\ucrt.lib

.data?
dfloat dq ?
.code
         strtoull  PROTO C :DWORD,:DWORD,:DWORD
         inv strtoull, soff("405edd3c07ee0b0b"),0,16    ; soff is my make a string function.  I think it's SADD in masm32
         mov dword ptr dfloat,eax
         mov dword ptr dfloat+4,edx
         inv crt_sprintf,addr buff2,soff("%f"),dfloat
         inv MessageBox,0,addr buff2,soff("print res"),0

prints out 123.456789

jj2007

Quote from: jimg on March 04, 2024, 05:15:29 PMjust discovered you can just call it with strtoull

Check the source of these strtoull timings. It's not in the Masm32 or Masm64 SDKs, so you have to grab it via LoadLibrary & GetProcAddress.

TimoVJL

Windows OS msvcrt.dll have _strtoui64 function too and perhaps a bit faster than ucrtbase.dll strtoull.
May the source be with you

jj2007

#27
_strtoui64 is indeed a bit faster than strtoull:
2681    cycles for 100 * Masm32 SDK hex2bin
25728   cycles for 100 * strtoull
1661    cycles for 100 * HexVal
18145   cycles for 100 * _strtoui64

EDIT: attachment removed - see reply #30

jimg

I could be wrong, but it looks to me like hex2bin does not put out a double (64 bit float).

NoCforMe

I don't know why you're calling those "floats": they aren't, just unsigned integers.
But what's weird is that you're using the floating-point format specifier ("%f") and apparently getting the right answer. ?????
Assembly language programming should be fun. That's why I do it.