The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: lelejau on December 16, 2010, 03:40:43 PM

Title: split strings
Post by: lelejau on December 16, 2010, 03:40:43 PM
Hello again  :P

I'm just wondering, how do we split strings in asm?

In c#, I did this way:

string[] parts = stringToSplit.Split(new char[]{'/'}); // split in every /

parts[0]
parts[1]

etc..

How it's done in asm?
Title: Re: split strings
Post by: oex on December 16, 2010, 03:44:42 PM
You can create a loop and loop through the first string copying to the second until the delimiter is reached ie


mov esi, String1
mov edi, String2

oexNext:

cmp [esi], BYTE PTR 'delimiterchar'
je oexStringCopied

mov al, [esi]
mov [edi], al

inc esi
inc edi

jmp oexNext

oexStringCopied:

Or by length....


mov esi, String1
mov edi, String2
mov ecx, Length

rep movsb
Title: Re: split strings
Post by: lelejau on December 16, 2010, 03:46:58 PM
ok, i'll try my luck.


what is movsb?
Title: Re: split strings
Post by: oex on December 16, 2010, 03:48:37 PM
Let me know if you get stuck.... Also check the instructions meaning ie movsb in the masm32 help files they auto increment esi and edi and decrement ecx
movsb = move single byte (esi->edi)

I made a mistake in my first example please recheck it (first example just fixed ::)) Note you would replace 'delimiterchar' with for example BYTE PTR ',' or just numeric ascii byte 32 (space)
Title: Re: split strings
Post by: lelejau on December 16, 2010, 03:59:04 PM
where my splited string will be at?

And, how I should use it?

I mean, esi[0], esi[1]...

Title: Re: split strings
Post by: oex on December 16, 2010, 04:07:35 PM
OK so if you allocate a string the length of the expected string ie


mov esi, chr$("Split String Example, Split On Comma")
mov edi, alloc(24)
xor ecx, ecx

oexNext:

cmp [esi], BYTE PTR ','
je oexStringCopied

mov al, [esi]
mov [edi], al
inc ecx

inc esi
inc edi

jmp oexNext

oexStringCopied:

sub esi, ecx ; Subtract Copied Length
print esi ; Prints "Split String Example"


Note that the string allocated is longer than the destination string by 4 bytes.... You want at least 1 '0' byte at the and for a zero delimited string
Title: Re: split strings
Post by: lelejau on December 16, 2010, 04:14:31 PM
each time you give me a different example, I'm getting a bit confused...

Lets just say the string I want to split at each , is "My car, is, red."

So spliting, I'd have:
My car
is
red

And I want to show a MessageBox with every splitted string.

MessageBox -> My car
MessageBox -> is
MessageBox -> red
Title: Re: split strings
Post by: oex on December 16, 2010, 04:51:09 PM
You can do this with the code in my last post above, this code is effectively the same as the first example.... Try to understand these 2 examples to answer your own question so you can adapt it to your desired implementations
Title: Re: split strings
Post by: lelejau on December 16, 2010, 04:54:47 PM
error A2008 - syntax error: esi
error A2006 - undefined symbol: chr$
error A2006 - undefined symbol: alloc
Title: Re: split strings
Post by: oex on December 16, 2010, 05:00:55 PM
Quote from: lelejau on December 16, 2010, 04:54:47 PM
error A2008 - syntax error: esi
error A2006 - undefined symbol: chr$
error A2006 - undefined symbol: alloc

ok I was including /masm32/macros/macros.asm
Title: Re: split strings
Post by: lelejau on December 16, 2010, 05:03:42 PM
including it gives me alot of errors...
Title: Re: split strings
Post by: oex on December 16, 2010, 05:07:43 PM
I'll have to leave someone else to answer this I cant think of what you need to include with macros.asm maybe masm32 lib almost definately windows.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib



Without macros.... Where were your strings (what variables are they in?).... There are other considerations if you use for example a predeclared BYTE buffer ie
LOCAL   szBuffer[500]:BYTE

Such as zeroing the buffer and setting esi via

lea esi, szBuffer
Title: Re: split strings
Post by: jj2007 on December 16, 2010, 05:12:33 PM
Here is a working example, I hope it is clear enough. Use \masm32\help\opcodes.chm to learn about e.g. rep scasb and lodsd

include \masm32\include\masm32rt.inc
.data?
TheArray dd 100 dup(?) ; create space for 100 strings
TheCopy db 1000 dup(?) ; we need a copy

.code
txMyCar db "My car,is,red.", 0
start:
TheBranch=1 ; try #2
mov esi, offset txMyCar ; our source string
mov edi, offset TheCopy ; we create a copy
push edi ; we need edi again, so save it
mov ecx, len(esi) ; we need a limit
push ecx ; we need ecx again, so save it
rep movsb
pop ecx ; ecx is back
pop edi ; edi is back
mov al, "," ; we search for commas
mov ebx, offset TheArray ; we put the pointers into [ebx]
.Repeat
mov edx, edi ; save the current pointer start
repne scasb ; look in [edi] for matches to al
mov [ebx], edx ; store the start of the string
mov byte ptr [edi-1], 0 ; wipe out the comma, and put a zero delimiter instead
add ebx, 4 ; increase ebx by a dword
test ecx, ecx ; have we reached the limit?
.Until Zero?
  if TheBranch eq 1
mov esi, offset TheArray ; start of array pointers
.Repeat
lodsd ; mov eax, [esi] plus add esi, 4
.Break .if !eax ; eax = 0 means end of pointers
print eax, 13, 10 ; print it...
.Until 0
  else
print TheArray[0], 13, 10
print TheArray[4], 13, 10
print TheArray[8], 13, 10
  endif
inkey "OK?"
exit
end start
Title: Re: split strings
Post by: oex on December 16, 2010, 05:17:14 PM
:lol thanks jj

include \masm32\include\masm32rt.inc

was what I was thinking
Title: Re: split strings
Post by: lelejau on December 16, 2010, 05:21:26 PM
Thanks, it worked perfectly.

Just answer me something, what TheBranch=1 is for?

I missed that.
Title: Re: split strings
Post by: jj2007 on December 16, 2010, 05:25:25 PM
Quote from: lelejau on December 16, 2010, 05:21:26 PM
Just answer me something, what TheBranch=1 is for?

It is called "conditional assembly". Masm generates either the loop code, or the three print statements. Good for testing variants.
Title: Re: split strings
Post by: lelejau on December 16, 2010, 05:30:00 PM
Still dont get it.
Title: Re: split strings
Post by: jj2007 on December 16, 2010, 05:38:52 PM
Masm generates different code depending on the value of TheBranch. The first variant uses a loop, the second one
Title: Re: split strings
Post by: lelejau on December 16, 2010, 05:59:10 PM
ok, thanks.