The MASM Forum

General => The Campus => Topic started by: Technos on March 16, 2020, 06:16:52 PM

Title: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 16, 2020, 06:16:52 PM
I copied the doc string of dwtoa below which seems to indicate that it should handle DWORD and convert to string. What I have observed is that 4294967295 is converted to "-1". Reading the source code tells me that it is checking if the number is negative which should not apply for DWORD.

Am I correct to assume that dwtoa should only handle DWORD? If yes, how can I send a proposed fix for dwtoa?

dwtoa proc dwValue:DWORD, lpBuffer:DWORD

    ; -------------------------------------------------------------
    ; convert DWORD to ascii string
    ; dwValue is value to be converted
    ; lpBuffer is the address of the receiving buffer
    ; EXAMPLE:
    ; invoke dwtoa,edx,ADDR buffer
    ;
    ; Uses: eax, ecx, edx.
    ; -------------------------------------------------------------
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: hutch-- on March 16, 2020, 06:23:06 PM
A DWORD is unsigned, nothing to fix. I gather you want a signed conversion.
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 16, 2020, 06:37:00 PM
My apologies if my post is confusing....

dwtoa is assuming that dwValue  is signed - which to me is wrong. I copied portion of dwtoa.asm below (lines 37 to 48). My un-trained eyes tell me that it is checking for negative numbers  :undecided:

    test eax,eax
    jnz sign

  zero:
    mov word ptr [edi],30h
    jmp dtaexit

  sign:
    jns pos
    mov byte ptr [edi],'-'
    neg eax
    add edi, 1
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 17, 2020, 03:58:32 PM
Do we have a repository somewhere where I can send a pull request?
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: jj2007 on March 17, 2020, 11:28:41 PM
No. Simply zip your code and attach it here (if you need more than the 512kB permitted, you are doing something wrong) :cool:
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: hutch-- on March 18, 2020, 12:29:08 AM
I did not write the procedure and it is 15 years old, what I have tended to use are 2 macros that call the respective MSVCRT functions.

unsigned  invoke crt__itoa,number,ADDR buffer,10
signed    invoke crt__ltoa,number,ADDR buffer,10
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 18, 2020, 02:36:23 PM
Just curious, who is maintaining masm32 (masm32v11r.zip) installer? Is there plans to grow or make it more current?
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: hutch-- on March 18, 2020, 02:44:58 PM
There is not much to do in it as 32 bit OS versions will not have much added to them, I currently spend the time I have on 64 bit MASM as it has a much longer future.
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 18, 2020, 04:52:04 PM
The projects (MFC/C++) that i am involved with are all 32-bit applications running on 64-bit OS. It will never be converted to 64-bit. I think what I am trying to say is that 32-bit applications will be here for a long time.

Should there be volunteers does anyone wiling to mentor someone to maintain masm32 version?
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: hutch-- on March 18, 2020, 05:11:53 PM
No doubt they will be with us for years but there appears to be little that is new at the OS level. It is reasonably straight forward to write 32 bit MASM modules so custom libraries are a good way to enhance C/C++ apps. If you know how to write them, doing a custom version of FASTCALL is no big deal in 32 bit MASM, you use the first 3 arguments in eax, ecx & edx then put any others on the stack and it works fine and is reliable.

If called from C/C++ you will have to stay within either C or STDCALL but its no big deal to do.

I don't see any problems for anyone who wants to write custom libraries and post them for other people to use, many have done this over time and many people have used them.
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: HSE on March 18, 2020, 09:27:57 PM
Quote from: Technos on March 18, 2020, 02:36:23 PM
Just curious, who is maintaining masm32 (masm32v11r.zip) installer? Is there plans to grow or make it more current?
Most of growth was (and still is) trough complementary packages and libraries like MasmBasic, ObjAsm32, SmplMath, ModernUI, assemblers like JWasm, AsmC and UASM, and others tools, examples, etc. Now the big package is the forum, and you can take what you need.

Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Vortex on March 18, 2020, 11:30:18 PM
Hi Technos,

Here is a quick example :

include     \masm32\include\masm32rt.inc

.data?

buffer      db 16 dup(?)

.code

start:

    invoke  dwtoa,-5,ADDR buffer
    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start


Build the code as a console output, you should see -5 after running the executable.
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: TimoVJL on March 19, 2020, 05:00:48 PM
msvcrt.dll _ultoa is for unsigned long conversion

char * _ultoa( unsigned long value, char *buffer, int radix );
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: daydreamer on March 21, 2020, 08:21:29 PM
Quote from: Vortex on March 18, 2020, 11:30:18 PM
Hi Technos,

Here is a quick example :

include     \masm32\include\masm32rt.inc

.data?

buffer      db 16 dup(?)

.code

start:

    invoke  dwtoa,-5,ADDR buffer
    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start


Build the code as a console output, you should see -5 after running the executable.
any return of length of ascii?eax?

Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Vortex on March 22, 2020, 05:33:11 AM
include     \masm32\include\masm32rt.inc

.data

s1          db 'Buffer = %s',13,10
            db 'eax = 0x%X',0

.data?

buffer      db 16 dup(?)

.code

start:

    invoke  dwtoa,-5,ADDR buffer
    invoke  crt_printf,ADDR s1,ADDR buffer,eax

    invoke  ExitProcess,0

END start


Buffer = -5
eax = 0x3535
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: Technos on March 23, 2020, 03:43:28 PM
Quote from: Vortex on March 18, 2020, 11:30:18 PM
Hi Technos,

Here is a quick example :

include     \masm32\include\masm32rt.inc

.data?

buffer      db 16 dup(?)

.code

start:

    invoke  dwtoa,-5,ADDR buffer
    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start


Build the code as a console output, you should see -5 after running the executable.

Hi Vortex,

I think I may have confused a lot of people with my post - non-native English speaker here... Anyway, my understanding with dwtoa (DWORD to o ASCII?) is that it should only handle DWORD (positive numbers). For example, the following will return -1, I am expecting it to print 4294967295.

include     \masm32\include\masm32rt.inc

.data?

buffer      db 16 dup(?)

.code

start:

    invoke  dwtoa,4294967295,ADDR buffer
    invoke  StdOut,ADDR buffer

    invoke  ExitProcess,0

END start

Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: jj2007 on March 23, 2020, 11:48:07 PM
Hi Technos,

It's just a little glitch in the documentation, which should say SDWORD, that's all.

But thanks to your post I discovered a weird little glitch in my own Str$() (http://www.jj2007.eu/MasmBasicQuickReference.htm#Mb1186) implementation:
include \masm32\MasmBasic\MasmBasic.inc
  Init
  Cls
  or ecx, -1 ; 4294967295 as DWORD, 18446744073709551615 as QWORD
  PrintLine Str$("%i=", ecx), Str$("%u", ecx)
  PrintLine Str$("%i=", ecx), Str$("%u ", ecx)
EndOfCode


Output:
-1=18446744073709551615
-1=4294967295


The %u format prints a QWORD, surprise! However, if followed by a blank, it prints a DWORD. Will have to investigate :tongue:
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: hutch-- on March 24, 2020, 12:16:06 AM
Timo posted the one that works on unsigned integers greater than 2 gig.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    LOCAL pvar   :DWORD
    LOCAL buffer[32]:BYTE

    lea edx, buffer
    mov pvar, edx

    invoke crt__ultoa,1024*1024*1024*3,pvar,10      ; 3 gig

    print pvar,13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Title: Re: dwtoa is checking if the value to be converted is negative number
Post by: raymond on March 24, 2020, 05:13:16 AM
Hi Technos

You may want to have a look at the smdtoa, smqtoa, umdtoa and umqtoa procedures included in the FPULIB available on this site (or on my site at ray.masm.com). The 's' and 'm' prefixes are for signed and unsigned dwords/qwords.

The "m" in the procedure name is to indicate that this procedure is based on the principal of multiplying by the reciprocal of 10 instead of dividing by 10, i.e. the use of "magic numbers".

Note: The source code of all the functions are part of the downloadable fpulib package.