Assembly code snippets

Back to the snippets overview

Details

TitleMulti String Replace
AuthorQages
Submitted by:Qages
Date added:2002-08-20 23:57:41
Date modified:2002-08-21 00:04:31

Comments

Coded Completely by my self. its a bit slow, expect faster versions out soon. if you want to optimize and submit it, please include me that i was the original author.
This is Designed in and for masm32.
just invoke PStrReplace,string text,letter(s) to replace ,letter(s) to replace with, buffer to put replaced string
string text; must have at least 1 char null termanted. letter to replace; must have at least 1 char null termanted. letter to replace with; can be null string or at least 1 char null termanted.
example: you can replace 1 letter with 2, or 2 letters with 1, no limit.

Snippet

StringLen proto,:DWORD
PStrCpy proto,:DWORD,:DWORD,:DWORD
PStrCmp proto,:DWORD,:DWORD
PStrMultiMove proto,:DWORD,:DWORD,:DWORD
PStrReplace proto,:DWORD,:DWORD,:DWORD,:DWORD

.data
lpheap dd 0
.code

PStrReplace Proc stdcall public uses eax ebx ecx edx edi esi,lpString :DWORD,lpReplace :DWORD,PTRSTRING2REPW :DWORD,PTRbuffer :DWORD
Local PTRstringrepLEN :DWORD
Local PTRstrrepwLEN :DWORD
local dwReplacelen    :DWORD
Invoke PStrCpy,PTRbuffer,lpString,0
mov ecx,PTRbuffer
mov ebx,lpReplace
Invoke StringLen,ecx
mov PTRstringrepLEN,eax
Invoke StringLen,ebx
mov PTRstrrepwLEN,eax
cmp PTRstringrepLEN,eax
jb PStrReplace@endo
Invoke StringLen,PTRSTRING2REPW
mov dwReplacelen,eax
mov edx,PTRstringrepLEN
sub edx,PTRstrrepwLEN
mov edi,edx
xor eax,eax
xor edx,eax
mov esi,-1
jmp @neloop
PStrReplace@not:
dec ecx
Invoke PStrMultiMove,ecx,ebx,PTRSTRING2REPW
push eax
Invoke StringLen,PTRbuffer
mov PTRstringrepLEN,eax
pop eax
inc ecx
sub ecx,PTRstrrepwLEN; avoid loops
add ecx,dwReplacelen
sub edi,PTRstrrepwLEN
add edi,dwReplacelen
sub esi,PTRstrrepwLEN
add esi,dwReplacelen
@neloop:
cmp esi,edi
je PStrReplace@endo
Invoke PStrCmp,ecx,ebx
inc ecx
inc esi
cmp eax,1
jne @neloop
je PStrReplace@not
PStrReplace@endo:
ret
PStrReplace EndP
;===================================
PStrMultiMove Proc  uses eax ebx ecx edx edi esi,PTRDEST :DWORD ,PTRFIND :DWORD,PTRSRC :DWORD
Local pdest long
Local pfind long
Local psrc long
mov ecx,PTRDEST
Invoke StringLen,ecx
cmp eax,0
je PStrMultiMoveEND
mov pdest,eax
mov ebx,PTRSRC
Invoke StringLen,ebx
mov psrc,eax
Invoke StringLen,PTRFIND
cmp eax,0
je PStrMultiMoveEND
mov pfind,eax
add ecx,eax
push ebx
push ecx
mov edx,pdest
add edx,1000
Invoke GetProcessHeap
Invoke HeapAlloc,eax, 0 ,edx
mov lpheap,eax
pop ecx
pop ebx
Invoke PStrCpy,lpheap,ecx,0
sub ecx,pfind
cmp psrc,0
je @F
Invoke PStrCpy,ecx,ebx,1
@@:
add ecx,psrc
Invoke PStrCpy,ecx,lpheap,0
PStrMultiMoveENDHEAP:
Invoke GetProcessHeap
Invoke HeapFree, eax, 0, lpheap
PStrMultiMoveEND:
ret
PStrMultiMove EndP
;===================================
PStrCmp Proc stdcall public, PTRstring:DWORD,PTRstring2find:DWORD
push ecx
push ebx
push edx
push esi
mov ecx,PTRstring
mov ebx,PTRstring2find
Invoke StringLen,ebx
mov esi,eax
dec esi
xor eax,eax
xor edx,eax
mov dl, BYTE PTR [ebx+eax]
cmp dl, BYTE PTR [ecx+eax]
jne @ret0attrrcmp
@@:
cmp eax,esi
je @ret1attrrcmp
inc eax
mov dl, BYTE PTR [ebx+eax]
cmp dl, BYTE PTR [ecx+eax]
je @B
jne @ret0attrrcmp
@ret1attrrcmp:
xor eax,eax
inc eax
@popptrecmp:
pop esi
pop edx
pop ebx
pop ecx
ret
@ret0attrrcmp:
xor eax,eax
pop esi
pop edx
pop ebx
pop ecx
ret
PStrCmp EndP
;===================================
PStrCpy Proc uses eax ebx ecx edx  ,PTRDEST :DWORD ,PTRSRC :DWORD,DIFputnull :DWORD;
mov ecx,PTRDEST
mov ebx,PTRSRC
Invoke StringLen,ebx
mov edi,eax
cmp eax,0
je @F
dec edi
@@:
xor eax,eax
xor edx,edx
mov dl, BYTE PTR [ebx+eax]
mov BYTE PTR [ecx+eax],dl
cmp eax,edi
je @PStrCpy@PUtnull
@@:
inc eax
mov dl, BYTE PTR [ebx+eax]
mov BYTE PTR [ecx+eax],dl
cmp eax,edi
jne @B
@PStrCpy@PUtnull:
cmp DIFputnull,1
je @PStrCpyEND
inc eax
mov BYTE PTR [ecx+eax],0
@PStrCpyEND:
ret
PStrCpy EndP
;===================================
StringLen Proc  stdcall public,ptrSTRING1:DWORD;duh
push esi
push ebx
mov esi,ptrSTRING1
xor ebx,ebx
xor eax,eax
cmp BYTE PTR [esi+eax],0
jne @F
pop ebx
pop esi
ret
@@:
inc eax
cmp BYTE PTR [esi+eax],0
jne @B
pop ebx
pop esi
ret
StringLen EndP