The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: newbieinfl on April 04, 2006, 02:30:51 PM

Title: Scan for comma and move within a string
Post by: newbieinfl on April 04, 2006, 02:30:51 PM
I know how to search for a comma within a string and even how to remove the comma (I've created and tested some programs and saw other posts on this) but I need to learn how to find a comma and move everything after the comma into a new string.  I'm puzzled.  Can anyone help please?
Title: Re: Scan for comma and move within a string
Post by: rags on April 04, 2006, 03:33:50 PM
Just create a buffer using GlobalAlloc of sufficient size, usually 128 -256 bytes for a normal string would do.
Use the handle from that allocation as a pointer to the new string.
Since you want everything after the comma in a new string, copy everthing from 1 byte past the comma, up to and including the terminating zero into the new string.
Dont forget to free the allocated buffer using GlobalFree when you are done using the new string.
Here are some links for GlobalAlloc and GlobalFree that may help.
GlobalAlloc (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalalloc.asp)
GlobalFree (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/globalfree.asp)

Rags
Title: Re: Scan for comma and move within a string
Post by: Mark Jones on April 04, 2006, 04:20:26 PM
Hi Newbie. How's Florida? :) Rags has some good ideas. You could also try stralloc and strfree, they are macros found in MASM32\macros\macros.asm. Use them as follows:


.data?
    lpBuffer DD ?
.code
    stralloc 256
    mov lpBuffer,eax
; lpBuffer is a memory offset where 256 bytes are (zeroed) and reserved.
    strfree lpBuffer


Note you can also do this:


.data?
    lpBuffer DB 256 dup(?)
.code


To manipulate strings on the byte-level, try this to get bytes into AL:


    mov esi,YourSourceStringOffset
    mov al,BYTE PTR [esi]
    inc esi
    mov al,BYTE PTR [esi]
    inc esi ;.... etc.


Here is a much nicer way to write this, but you must use ESI:


    mov esi,YourSourceStringOffset
    cld
    lodsb
    lodsb ;.... etc.


And to put bytes into a string:


    mov edi,YourDestinationStringOffset
    mov BYTE PTR [edi],al
    inc edi
    mov BYTE PTR [edi],al
    inc edi ;.... etc.


and the nicer version...


    mov edi,YourDestinationStringOffset
    cld
    stosb
    stosb ;.... etc.


Have fun. :8)
Title: Re: Scan for comma and move within a string
Post by: newbieinfl on April 04, 2006, 04:44:23 PM
Thanks Mark!  This helps a lot!

Thank you to Rags as well, the sites did help.

-newbie

P.S.  Florida is great!  Not looking forward to hurricane season though.