Find characters inside a string

Started by Vortex, August 20, 2026, 09:18:26 PM

Previous topic - Next topic

Vortex

FindChar function to find characters inside strings :

.386
.model flat,stdcall
option casemap:none

ExitProcess PROTO :DWORD
printf PROTO C :DWORD,:VARARG

.data

mystr   db 'This is a test.',0
f1      db 'Address of the string = %X',13,10
        db 'Address of the character a = %X',0

.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

FindChar PROC src:DWORD,char:DWORD

    mov     eax,DWORD PTR [esp+4]
    mov     ecx,DWORD PTR [esp+8]
    dec     eax
@@:
    inc     eax
    mov     dl,BYTE PTR [eax]
    mov     dh,dl
    xor     dl,cl
    dec     dl
    dec     dh
    or      dl,dh
    cmp     dl,255
    jnz     @b
    retn    8

FindChar ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

start:

    invoke  FindChar,ADDR mystr,'a'
    invoke  printf,ADDR f1,ADDR mystr,eax

    invoke  ExitProcess,0

END start
Code it... That's all...

Vortex

Here is the 64-bit version :

ExitProcess PROTO :QWORD
printf PROTO C :QWORD,:VARARG

.data

mystr   db 'This is a test.',0
f1      db 'Address of the string = %X',13,10
        db 'Address of the character a = %X',0

.code

FindChar PROC src:QWORD,char:QWORD

    mov     rax,rcx
    dec     rax
@@:
    inc     rax
    mov     cl,BYTE PTR [rax]
    mov     ch,cl
    xor     cl,dl
    dec     ch
    dec     cl
    or      cl,ch
    cmp     cl,255
    jnz     @b
    ret

FindChar ENDP

start PROC PARMAREA=4*SIZEOF(QWORD)

    invoke  FindChar,ADDR mystr,'a'
    invoke  printf,ADDR f1,ADDR mystr,rax

    invoke  ExitProcess,0

start ENDP

END start
Code it... That's all...


Vortex

Another version assuming that the string to be searched contains only printable characters :

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

FindChar PROC src:DWORD,char:DWORD

    mov     eax,DWORD PTR [esp+4]
    mov     ecx,DWORD PTR [esp+8]
    dec     eax
@@:
    inc     eax
    mov     dl,BYTE PTR [eax]
    mov     dh,dl
    xor     dl,cl
    sub     dx,0101h
    or      dl,dh
    cmp     dl,255
    jnz     @b
    retn    8

FindChar ENDP

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef
Code it... That's all...