Hi somebody help me
How to split one string to multy strings in Masm, i need the function like Split in Visual Basic.:stupid:
Posted on 2004-03-01 01:46:25 by neverending
A quick (1minute) made example in TASM,
but i am sure you can convert it with ease :tongue:



;------------------------------
;Warn:
;-action in place
;-result array must have space
;-------------------------------
Str_Split PROC STDCALL
uses ecx,esi,edi
ARG @@lpsz_string:dword
ARG @@lplp_results:dword

mov edi,[@@lplp_results]
mov esi,[@@lpsz_string]
mov ecx,esi

@@loop_string:
mov al,[esi]
test al,al
jz @@finish

; check for space as separator
.IF al==32
;found end of one sub-string
;terminate it in place
mov byte ptr [esi],0
;store start into result array
mov [edi],ecx
add edi,4
mov ecx,esi
inc ecx
.ENDIF

inc esi
jmp @@loop_string

@@finish:
ret
ENDP
Posted on 2004-03-01 04:46:15 by BogdanOntanu
Here's mine, in MASM
Let's assume you will be splitting by only one character, "," for example
We'll want a function to look like
Split proc StrIn,Delimiter
Now, the next problem is how will we give back the array. Initially, we'll make the function overwrite the input string, and return the number of times we encountered "," :


Split proc uses ebx ecx StrIn,Delimiter
mov ebx,StrIn
mov ecx,1 ; number of strings encountered
xor eax,eax
_again:
mov al,[ebx]
.if eax==Delimiter
mov byte ptr[ebx],0
inc ecx
.endif
inc ebx
or al,al
jnz _again
mov eax,ecx
ret
Split endp

Now we'll have eax strings one after another. But accessing each of the strings won't be easy. So, we'll make another function - that gives us indexes of strings apart from number of strings. We'll still be modifying the input string. pArray is the address of a DWORD array, big enough to show the addresses of each of the substrings


Split proc uses ebx ecx edx StrIn,pArray,Delimiter
xor eax,eax
mov ebx,StrIn
mov edx,pArray
mov [edx],ebx
mov ecx,1 ; number of strings encountered
_again:
mov al,[ebx]
inc ebx
.if eax==Delimiter
mov byte ptr[ebx-1],0
mov [edx+ecx*4],ebx
inc ecx
.endif
or al,al
jnz _again
mov eax,ecx
ret
Split endp

Ok, we may want to put a limit to the number of string we get:


Split proc uses ebx ecx edx StrIn,pArray,MaxStrings,Delimiter
mov ebx,StrIn
mov edx,pArray
mov [edx],ebx
mov ecx,1 ; number of strings encountered
xor eax,eax
_again:
mov al,[ebx]
inc ebx
.if eax==Delimiter[color=green] && ecx<MaxStrings[/color]
mov byte ptr[ebx-1],0
mov [edx+ecx*4],ebx
inc ecx
.endif
or al,al
jnz _again
mov eax,ecx
ret
Split endp


Ok, we want to keep the original string, so we'll have to return a new array with the stuff inside:


Split proc uses ecx edx StrIn,Delimiter
local ReturnedArray,StringsNum,StrInLen
mov ebx,StrIn
xor eax,eax
mov ecx,1
@@:
mov al,[ebx]
inc ebx
.if eax==Delimiter
inc ecx
.endif
or al,al
jnz @B
sub ebx,StrIn
mov StrInLen,ebx
mov StringsNum,ecx
lea ebx,[ebx+ecx*4+4]
invoke HeapAlloc,HEAP1,0,ebx ; assuming HEAP1 is a heap you've already created
; invoke malloc,ebx <-- this is what the above stuff needs to do
mov ReturnedArray,eax

mov ecx,StringsNum
lea ebx,[eax+ecx*4+4]
mov edx,eax

mov [edx],ebx
mov ecx,1
xor eax,eax
_again:
mov al,[ebx]
inc ebx
.if al==Delimiter
mov byte ptr[ebx-1],0
mov [edx+ecx*4],ebx
inc ecx
.endif
or al,al
jnz _again
mov dword ptr[edx+ecx*4],0
mov eax,ecx ; EAX holds the number of strings found
mov ebx,ReturnedArray ; this is the actual array pointer.
; The first EAX+1 DWORDs contain pointers to the strings, the last DWORD of which is 0
; (in case we forgot how many the strings in the array are)
; use HeapFree or "free" to free this memory when you've finished using it
ret
Split endp
Posted on 2004-03-01 06:52:03 by Ultrano
this works for my needs :)



SplitString proc lpString:DWORD, lpArray:DWORD

xor eax, eax
mov edx, lpString
mov ecx, lpArray

mov [ecx+eax*4], edx
inc eax

Next:
cmp BYTE PTR [edx], 0
jz Salir
cmp BYTE PTR [edx], ' '
jz Patch
inc edx
jmp Next

Patch:
mov BYTE PTR [edx], 0
inc edx
mov [ecx+eax*4], edx
inc eax
jmp Next

Salir:
ret

SplitString endp
Posted on 2004-03-01 20:11:24 by Jnrz
I forgot some code in the last function - to actually copy the old string to the new place... here's the fixed code:



Split proc uses ecx edx StrIn,Delimiter
local ReturnedArray,StringsNum,StrInLen
mov ebx,StrIn
xor eax,eax
mov ecx,1
@@:
mov al,[ebx]
inc ebx
.if eax==Delimiter
inc ecx
.endif
or al,al
jnz @B
sub ebx,StrIn
mov StrInLen,ebx
mov StringsNum,ecx
lea ebx,[ebx+ecx*4+4]
invoke HeapAlloc,HEAP1,0,ebx ; assuming HEAP1 is a heap you've already created
; invoke malloc,ebx <-- this is what the above stuff needs to do
mov ReturnedArray,eax
mov ecx,StringsNum
lea ebx,[eax+ecx*4+4]
[color=blue]
push ebx
mov eax,StrIn
@@:
mov cl,[eax]
mov [ebx],cl
inc eax
inc ebx
or cl,cl
jnz @B
pop ebx
[/color]
mov eax,ReturnedArra
mov edx,eax

mov [edx],ebx
mov ecx,1
xor eax,eax
_again:
mov al,[ebx]
inc ebx
.if al==Delimiter
mov byte ptr[ebx-1],0
mov [edx+ecx*4],ebx
inc ecx
.endif
or al,al
jnz _again
mov dword ptr[edx+ecx*4],0
mov eax,ecx ; EAX holds the number of strings found
mov ebx,ReturnedArray ; this is the actual array pointer.
; The first EAX+1 DWORDs contain pointers to the strings, the last DWORD of which is 0
; (in case we forgot how many the strings in the array are)
; use HeapFree or "free" to free this memory when you've finished using it
ret
Split endp
Posted on 2004-03-02 12:57:46 by Ultrano