Usage of MASM32 macros for number to string conversion "ustr$"/"sstr$"

Started by frktons, September 04, 2010, 04:14:39 PM

Previous topic - Next topic

frktons

According to MASM32 documentation:

ustr$  Convert unsigned 32 bit integer to a zero terminated string
sstr$  Convert signed 32 bit integer to a zero terminated string


and:


ustr$


mov lpResult, ustr$(uInteger)


Description

Convert an unsigned 32 bit integer to a zero terminated string.


Parameters

1. uInteger An unsigned 32 bit integer.


Return Value

The return value is the address of the zero terminated string that holds the result as an OFFSET.


Comments

Each macro call in the assembler source code provides a 20 byte long buffer in the data section to hold the results of the conversion.


I've some doubts on how to use this two macros.
Let's say I want to convert the value in a integer variable into a string
and store the string to a variable called MyString, how do I proceed?

If I do this:



include \masm32\include\masm32rt.inc

.data
MyString      DB   "                    ",0
PtrMyString   DD  MyString
NumToFormat   DD 3123456789

.code
start:

mov PtrMyString, ustr$(NumToFormat)

print "The number "
print ustr$(NumToFormat)
print " is string: "
print PtrMyString,13,10,13,10

inkey " -- ok --"

exit

end start


I get this result:


The number 3123456789 is string: 3123456789

-- ok --


what happens? How the string MyString is filled? or PtrMyString
after calling the macro points to a dynamically allocated area?

Because if I change the prog this way:


print "The number "
print ustr$(NumToFormat)
print " is string: "
print offset MyString,13,10,13,10


I see that MyString is still filled with spaces.

How do I get the string from the allocated area to MyString?

Thanks

Frank


Mind is like a parachute. You know what to do in order to use it :-)

oex

MemCopy?

Look at the macro in macros.asm to understand what it is doing.... You are trying to replicate half the functionality yourself....

      sstr$ MACRO DDvalue   ;; signed integer from string
        LOCAL rvstring
        .data
          rvstring db 20 dup (0)
        align 4
        .code
        invoke dwtoa,DDvalue,ADDR rvstring
        ;; invoke ltoa,DDvalue,ADDR rvstring
        EXITM <OFFSET rvstring>
      ENDM

rvstring is the same as:
MyString      DB   "                    ",0



To place the value in MyString call:

        invoke dwtoa,NumToFormat,ADDR MyString

(To Print):

   print ADDR MyString
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

Quote from: oex on September 04, 2010, 04:19:57 PM
lea eax, MyString
mov eax, sstr$(NumToFormat)

Doesn't seem to work. It is the same result: MyString is still space filled.  ::)
Mind is like a parachute. You know what to do in order to use it :-)

oex

yeah sorry I misunderstood, I was answering your previous query, see updated reply you checked back too quick :bg
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

OK. I'd like the macro ustr$ do what I need, it fills the string
that I pass through a pointer with the converted number.
The macro is:

      ustr$ MACRO DDvalue   ;; unsigned integer from string
        LOCAL rvstring
        .data
          rvstring db 20 dup (0)
        align 4
        .code
        ;; invoke dwtoa,DDvalue,ADDR rvstring
        invoke crt__ultoa,DDvalue,ADDR rvstring,10
        EXITM <OFFSET rvstring>
      ENDM


If I want to build a new macro, so that I pass it the number to format and the address
of the area to fill, still 20 bytes long, how should I proceed?
Something like:


      ustrv$ MACRO DDvalue1, DDvalue2   ;; unsigned integer to string
          invoke crt__ultoa,DDvalue1,DDvalue2,10
          EXITM <DDvalue2>
      ENDM


OR what?
Mind is like a parachute. You know what to do in order to use it :-)

oex

I think you are still confusing the Address with the Memory Space but this macro would accept

ustrv$ MACRO DDvalue1, DDvalue2   ;; unsigned integer to string
          invoke crt__ultoa,DDvalue1,DDvalue2,10
          EXITM <OFFSET DDvalue2>
ENDM

mov eax, ustrv$(NumToFormat, OFFSET MyString)
print eax

Note However: rvstring db 20 dup (0)
Your 'MyString' memory space should be long enough for the value you are writing to it (ie the value returned by the crt__ultoa function
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

Quote from: oex on September 04, 2010, 04:38:54 PM
I think you are still confusing the Address with the Memory Space but this macro would accept

ustrv$ MACRO DDvalue1, DDvalue2   ;; unsigned integer to string
          invoke crt__ultoa,DDvalue1,DDvalue2,10
          EXITM <OFFSET DDvalue2>
ENDM

mov eax, ustrv$(NumToFormat, ADDR MyString)
print eax

Sorry oex, The new macro supposes that I pass the number to format that is a DD value
and the pointer to the area to fill, that is a DD value as well.

I don't understand if the macro returns the address of the pointer or the address  of the string
in your version.
Mind is like a parachute. You know what to do in order to use it :-)

frktons

Well, it works this way:

mov eax, ustrv$(NumToFormat, offset MyString)

Mind is like a parachute. You know what to do in order to use it :-)

oex

I slightly modified the post (see red)

The function crt__ultoa accepts the dword number value and the ADDRESS of the memory to write the ascii to.... so....

mov ebx, 1234 <---- DWORD Number
lea eax, MyString <---- DWORD ADDRESS in eax (MyString is 21 byte data block)
invoke crt__ultoa, ebx, eax

MyString refers to a data block which is 21 bytes long not to it's DWORD Address pointer
MyString      DB   "                    ",0
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

frktons

Quote from: oex on September 04, 2010, 04:47:30 PM
I slightly modified the post (see red)

The function crt__ultoa accepts the dword number value and the ADDRESS of the memory to write the ascii to.... so....

mov ebx, 1234 <---- DWORD Number
lea eax, MyString <---- DWORD ADDRESS in eax (MyString is 21 byte data block)
invoke crt__ultoa, ebx, eax

MyString refers to a data block which is 21 bytes long not to it's DWORD Address pointer
MyString      DB   "                    ",0


Thanks for your explanations oex  :U

I need the pointer to the string, so instead of the register to receive the address of the
modified string, I use the pointer I defined for this purpose:


include \masm32\include\masm32rt.inc

ustrv$ MACRO DDvalue1, DDvalue2   ;; unsigned integer to string
     invoke crt__ultoa,DDvalue1,DDvalue2,10
     EXITM <offset DDvalue2>
ENDM

.data
MyString      DB  20 dup (0),0
PtrMyString   DD  MyString
NumToFormat   DD  3123456789

.code
start:

print "The number "
print ustr$(NumToFormat)
print " is string: "

mov PtrMyString, ustrv$(NumToFormat, offset MyString)

print PtrMyString,13,10,13,10
print offset MyString,13,10,13,10


inkey " -- ok --"

exit

end start


Working.  :U
Mind is like a parachute. You know what to do in order to use it :-)

hutch--

Frank,

Its simple enough to do. The two macros return an address that has the result stored there. Just do a zero terminated string copy to another string buffer that is large enough to hold the result. The library has a procedure "szCopy" that will do it and its easy enough to roll your own, just get the two addresses, the result and the buffer address and copy the first to the second stopping after the zero terminator.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: hutch-- on September 05, 2010, 12:44:37 PM
Frank,

Its simple enough to do. The two macros return an address that has the result stored there. Just do a zero terminated string copy to another string buffer that is large enough to hold the result. The library has a procedure "szCopy" that will do it and its easy enough to roll your own, just get the two addresses, the result and the buffer address and copy the first to the second stopping after the zero terminator.

Thanks Steve.

I think that for my actual need the solution I'm using is good enough.
I don't need to copy the string from a memory buffer to another var
o memory buffer, because  crt__ultoa already works on the string
that I've allocated for the purpose, just passing to the MACRO ustrv$
the address of the string.

For didactical objectives I could do what you suggest, but as far as I can
understand this solution is not as efficient as the one I'm already using.

And moreover this is my first macro  :lol

Frank

Mind is like a parachute. You know what to do in order to use it :-)

hutch--

rank,

This is the macro that does it if you are just plug lazy.


    ; --------------------------
    ; szstring to szstring copy
    ; --------------------------
      cst MACRO arg1,arg2
        invoke szCopy,reparg(arg2),tstarg(arg1)
      ENDM
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

frktons

Quote from: hutch-- on September 06, 2010, 01:18:30 AM
Frank,

This is the macro that does it if you are just plug lazy.


    ; --------------------------
    ; szstring to szstring copy
    ; --------------------------
      cst MACRO arg1,arg2
        invoke szCopy,reparg(arg2),tstarg(arg1)
      ENDM


Thanks Steve.

This section of Assembly, MACROS, is brand new for me,
and because many of you are using them, I'll have to learn something
about them as well. So this other example is a good one to learn.
I've to confess that looking at it I don't fully grasp the nature of
the parameters and instructions involved though.

arg1 and arg2 are usually addresses of something, or should be
in my imagination, so I presume they are the Source and Destination
string addresses.

invoke szCopy should be something like a CALL to a C function, or the
like.

And here the difficult part: reparg(arg2),tstarg(arg1) that I don't have
a clue about their meaning, why the parameters are "order inverted" and what
the use of parenthesis means. This is a syntax I really need to understand.

So for didactical purposes, trying to understand it, I rewrite it this way:

    ; --------------------------
    ; szstring to szstring copy
    ; --------------------------
      CopyString MACRO Source, Destination
        invoke szCopy,reparg(Destination),tstarg(Source)
      ENDM


And I ask myself these questions:

1.) Is it the meaning of the parameters or is it the reverse?

     Is arg1 the Source or the Destination string address?

2.) szCopy is a C function inside a DLL? and sz is short for string zero terminated?

3.) Why we write the parameters in a order and they are used reverted by szCopy?
     Does it depends on the parameters passing convention that is different between
     MASM and C?
4.) And last, but not least, what does this  reparg(arg2),tstarg(arg1) mean?

So I've quite a lot to search the Internet and the DOCS to fully understand what's
going on here.  :lol

Frank







Mind is like a parachute. You know what to do in order to use it :-)

oex

Quote from: frktons on September 06, 2010, 04:02:17 AM
1.) Is it the meaning of the parameters or is it the reverse?

     Is arg1 the Source or the Destination string address?

2.) szCopy is a C function inside a DLL? and sz is short for string zero terminated?

3.) Why we write the parameters in a order and they are used reverted by szCopy?
     Does it depends on the parameters passing convention that is different between
     MASM and C?
4.) And last, but not least, what does this  reparg(arg2),tstarg(arg1) mean?

So I've quite a lot to search the Internet and the DOCS to fully understand what's
going on here.  :lol

Frank



szCopy is part of the MASM32 package.... Answers to all your questions can be found in the MASM32 library reference help file:

\masm32\help\masmlib.chm

reparg and tstarg are macros.... search in macros.asm for what they do
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv