articles » current » assembly » x86 » procedures » strstr

x86 Assembly: strstr Procedure

Find ASCII substring in an ASCII string.

Use MASM for Visual C++ Express Edition 2005 to compile this procedure.

This procedure, strstr_asm, returns NULL (0) pointer when substring is not found in string, otherwise, returns pointer in str where substr was found.

TITLE 'extern "C" char* strstr_asm(char *str, char *substr);'


.model FLAT

PUBLIC  _strstr_asm

_strstr_asm PROC NEAR

  push  ebx
  push  ebp

  mov  ebp, DWORD PTR [esp+16] ; substr
  mov  al, BYTE PTR [ebp]
  test al, al
  push esi
  push edi
  jne  SHORT label1

  mov  eax, DWORD PTR [esp+20] ; str
  pop  edi
  pop  esi
  pop  ebp
  pop  ebx

  ret  0


  mov  edi, DWORD PTR [esp+20] ; str
  mov  dl, BYTE PTR [edi]
  test dl, dl
  mov  ecx, ebp
  je   SHORT label5


  cmp  dl, al
  jne  SHORT label4

  mov  esi, edi
  sub  esi, ebp


  mov  dl, BYTE PTR [ecx]
  test dl, dl
  je   SHORT label6
  mov  bl, BYTE PTR [esi+ecx]

  inc  ecx
  cmp  bl, dl
  je   SHORT label3

  mov  ecx, ebp


  mov  dl, BYTE PTR [edi+1]
  inc  edi
  test dl, dl
  jne  SHORT label2


  pop  edi
  pop  esi
  pop  ebp

  xor  eax, eax
  pop  ebx

  ret  0


  mov  eax, edi
  pop  edi
  pop  esi
  pop  ebp
  pop  ebx

  ret  0

_strstr_asm ENDP

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.