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

jimg

Would someone like to share how to get ATOFLt to work in masm (uasm)?  I'm having no luck at all.

jj2007

include \masm32\MasmBasic\MasmBasic.inc
  SetGlobals MyFloat:float
  Init
  Dll "ucrtbased"
  Declare void _atoflt, C:3
  _atoflt(addr MyFloat, Chr$("12345.67890"), 0)
  Inkey Str$("The result is %9f", MyFloat)
EndOfCode

Output: The result is 12345.6787

You can use ucrtbase (without the "d"), too. If you don't have MasmBasic installed, LoadLibrary and GetProcAddress are your friends.
 

NoCforMe

So how does that work for a non-MasmBasic user, JJ?
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: NoCforMe on February 28, 2024, 12:28:40 PMSo how does that work for a non-MasmBasic user, JJ?

As written above, LoadLibrary and GetProcAddress are your friends. Go ahead, don't be lazy, code it. I am sure you are able to do it, now that I gave you all the necessary info.

NoCforMe

Look, Bub, I'm a little tired of you accusing me of being "lazy".

Look: This is the Campus. Where presumably users who don't have all that much experience with assembly language ask questions.

The OP specifically asked about MASM or uasm, not MasmBasic.

LoadLibrary() and GetProcAddress()? OK, fine: just what library (DLL) can that function be found in? You didn't give that anywhere.

Why don't you just answer the damn question simply so the asker can actually use your information?
Assembly language programming should be fun. That's why I do it.

jj2007

Quote from: NoCforMe on February 28, 2024, 12:53:59 PMso the asker can actually use your information

Since Jim is one of the most experienced users here, I am sure he will briefly look at my code and say "Oh, that's it, so simple!".

Anyway, you seem to always have time to criticise others, why don't you sit down and code the campus style example in "pure" Masm32? It won't take you more than 10 minutes.

NoCforMe

Would you care to at least tell me where I can find this function? What DLL?
Assembly language programming should be fun. That's why I do it.

TimoVJL

ucrtbase.dll comes in ucrt packages and Windows 10 have it by default.
May the source be with you

jimg

Well, I'm really rusty at this bit.
I spent several hours trying to get this to work with no luck.
I stripped most of the crud from my test for you guys to look at.
I'm sure it's something simple I'm just can't see, so if someone would take a look, I'd appreciate it.

.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

.listall

.data?
hWin  dd ?
buff  db 100 dup (?)
buff2 db 100 dup (?)
sfloat dd ?
atoflt dd ?
.data
xfloat dq 123.45677
.code
 
Program:
   inv LoadLibrary,soff("ucrtbase.dll")
   .if eax==0
      msgx "loadlibrary ng"
      jmp wrap
   .endif
   inv GetProcAddress,eax,soff("_atoflt")
   .if eax==0
      msgx "GetProcAddress ng"
      jmp wrap
   .endif
   mov atoflt,eax
   
   push 123  ; for testing to make sure I did the stack right
   push offset sfloat
   push soff("12345.67")
   call atoflt
   .if eax
      msgx "error from atoflt"
      jmp wrap
   .endif
   pop eax  ; c call so clear stack
   pop eax
   pop eax
   .if eax != 123
       msgx "stack prob"
       jmp wrap
   .endif
  inv crt_sprintf,addr buff2,soff("results= %f"),sfloat
  inv MessageBox,0,addr buff2,0,0
   
wrap:   
    inv ExitProcess, eax

end Program

_japheth

Quote from: jimg on February 29, 2024, 03:32:46 AMI'm sure it's something simple I'm just can't see, so if someone would take a look, I'd appreciate it.

If JJ's sample works, the float address argument is supposed to be first. Since in C the arguments are pushed from right to left, the float address should be pushed as last one, just before the call.
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

raymond

You may also want to have a peek at the FpuAtoFL function which is part of the fpu.lib available within the MASM32 package. The source codes of all the functions within the FPULIB are also available for reference if you ever want to write your own functions from scratch (to avoid some of the overhead and/or modify the output for whatever reason).

However, I don't know if 32-bit code can still function on 64-bit systems.
Whenever you assume something, you risk being wrong half the time.
https://masm32.com/masmcode/rayfil/index.html

jj2007

Quote from: raymond on February 29, 2024, 04:30:52 AMI don't know if 32-bit code can still function on 64-bit systems.

16-bit is gone but 32-bit code works fine on 64-bit systems.

@Jim: Japheth is correct.

jimg

Yes, I tried that and variations many times.  Still doesn't work.  Something subtle still wrong.

jimg

Thank you raymond.  I should have remembered that.  Haven't coded asm in about 4 years with a lots of medical problems in between.  Brain still a little foggy.

Vortex

Hi jimg,

It's not recommended to link directly to the import library ucrtbase.lib. You would rather prefer ucrt.lib :

How to correctly link to UCRT (and why it works that way) :

https://mingwpy.github.io/ucrt.html

QuoteCorrectly linking to the UCRT via "API sets" does not require any new features in the toolchain; it's just some clever usage of old, well-supported features.
The only "secret sauce" contained in the ucrt.lib file is the list of (DLL name, symbol name) pairs; given these, we can straightforwardly construct a new .lib file from scratch, and then any binaries that we link using our new .lib file will be linked to the UCRT in a safe, forwards-compatible manner.

https://masm32.com/board/index.php?topic=11525.0

Here is a quick example :

include \masm32\include\masm32rt.inc

_atoflt  PROTO C :DWORD,:DWORD

.data

format  db 'The result is %f',0
str1    db '3.141592',0

.data?

pi      real4 ?
result  real8 ?

.code

start:

    invoke  _atoflt,ADDR pi,ADDR str1

    fld     pi
    fstp    QWORD PTR result     
    invoke  crt_printf,ADDR format,result
           
    invoke  ExitProcess,0

END start