Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
6.2 KiB

  1. page ,132
  2. title strchr - search string for given character
  3. ;***
  4. ;strchr.asm - search a string for a given character
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines strchr() - search a string for a character
  10. ;
  11. ;Revision History:
  12. ; 10-27-83 RN initial version
  13. ; 05-17-88 SJM Add model-independent (large model) ifdef
  14. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  15. ; 08-23-88 JCR 386 cleanup
  16. ; 10-25-88 JCR General cleanup for 386-only code
  17. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  18. ; 05-10-91 GJF Back to _cdecl, sigh...
  19. ; 09-08-94 GJF Smaller and faster.
  20. ; 11-28-95 GJF Much faster version from Intel.
  21. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  22. ;
  23. ;*******************************************************************************
  24. .xlist
  25. include cruntime.inc
  26. .list
  27. page
  28. ;***
  29. ;char *strchr(string, chr) - search a string for a character
  30. ;
  31. ;Purpose:
  32. ; Searches a string for a given character, which may be the
  33. ; null character '\0'.
  34. ;
  35. ; Algorithm:
  36. ; char *
  37. ; strchr (string, chr)
  38. ; char *string, chr;
  39. ; {
  40. ; while (*string && *string != chr)
  41. ; string++;
  42. ; if (*string == chr)
  43. ; return(string);
  44. ; return((char *)0);
  45. ; }
  46. ;
  47. ;Entry:
  48. ; char *string - string to search in
  49. ; char chr - character to search for
  50. ;
  51. ;Exit:
  52. ; returns pointer to the first occurence of c in string
  53. ; returns NULL if chr does not occur in string
  54. ;
  55. ;Uses:
  56. ;
  57. ;Exceptions:
  58. ;
  59. ;*******************************************************************************
  60. CODESEG
  61. found_bx:
  62. lea eax,[edx - 1]
  63. pop ebx ; restore ebx
  64. ret ; _cdecl return
  65. align 16
  66. public strchr, __from_strstr_to_strchr
  67. strchr proc
  68. .FPO ( 0, 2, 0, 0, 0, 0 )
  69. xor eax,eax
  70. mov al,[esp + 8] ; al = chr (search char)
  71. __from_strstr_to_strchr label proc
  72. push ebx ; PRESERVE EBX
  73. mov ebx,eax ; ebx = 0/0/0/chr
  74. shl eax,8 ; eax = 0/0/chr/0
  75. mov edx,[esp + 8] ; edx = buffer
  76. test edx,3 ; test if string is aligned on 32 bits
  77. jz short main_loop_start
  78. str_misaligned: ; simple byte loop until string is aligned
  79. mov cl,[edx]
  80. add edx,1
  81. cmp cl,bl
  82. je short found_bx
  83. test cl,cl
  84. jz short retnull_bx
  85. test edx,3 ; now aligned ?
  86. jne short str_misaligned
  87. main_loop_start: ; set all 4 bytes of ebx to [chr]
  88. or ebx,eax ; ebx = 0/0/chr/chr
  89. push edi ; PRESERVE EDI
  90. mov eax,ebx ; eax = 0/0/chr/chr
  91. shl ebx,10h ; ebx = chr/chr/0/0
  92. push esi ; PRESERVE ESI
  93. or ebx,eax ; ebx = all 4 bytes = [chr]
  94. ; in the main loop (below), we are looking for chr or for EOS (end of string)
  95. main_loop:
  96. mov ecx,[edx] ; read dword (4 bytes)
  97. mov edi,7efefeffh ; work with edi & ecx for looking for chr
  98. mov eax,ecx ; eax = dword
  99. mov esi,edi ; work with esi & eax for looking for EOS
  100. xor ecx,ebx ; eax = dword xor chr/chr/chr/chr
  101. add esi,eax
  102. add edi,ecx
  103. xor ecx,-1
  104. xor eax,-1
  105. xor ecx,edi
  106. xor eax,esi
  107. add edx,4
  108. and ecx,81010100h ; test for chr
  109. jnz short chr_is_found ; chr probably has been found
  110. ; chr was not found, check for EOS
  111. and eax,81010100h ; is any flag set ??
  112. jz short main_loop ; EOS was not found, go get another dword
  113. and eax,01010100h ; is it in high byte?
  114. jnz short retnull ; no, definitely found EOS, return failure
  115. and esi,80000000h ; check was high byte 0 or 80h
  116. jnz short main_loop ; it just was 80h in high byte, go get
  117. ; another dword
  118. retnull:
  119. pop esi
  120. pop edi
  121. retnull_bx:
  122. pop ebx
  123. xor eax,eax
  124. ret ; _cdecl return
  125. chr_is_found:
  126. mov eax,[edx - 4] ; let's look one more time on this dword
  127. cmp al,bl ; is chr in byte 0?
  128. je short byte_0
  129. test al,al ; test if low byte is 0
  130. je retnull
  131. cmp ah,bl ; is it byte 1
  132. je short byte_1
  133. test ah,ah ; found EOS ?
  134. je retnull
  135. shr eax,10h ; is it byte 2
  136. cmp al,bl
  137. je short byte_2
  138. test al,al ; if in al some bits were set, bl!=bh
  139. je retnull
  140. cmp ah,bl
  141. je short byte_3
  142. test ah,ah
  143. jz retnull
  144. jmp short main_loop ; neither chr nor EOS found, go get
  145. ; another dword
  146. byte_3:
  147. pop esi
  148. pop edi
  149. lea eax,[edx - 1]
  150. pop ebx ; restore ebx
  151. ret ; _cdecl return
  152. byte_2:
  153. lea eax,[edx - 2]
  154. pop esi
  155. pop edi
  156. pop ebx
  157. ret ; _cdecl return
  158. byte_1:
  159. lea eax,[edx - 3]
  160. pop esi
  161. pop edi
  162. pop ebx
  163. ret ; _cdecl return
  164. byte_0:
  165. lea eax,[edx - 4]
  166. pop esi ; restore esi
  167. pop edi ; restore edi
  168. pop ebx ; restore ebx
  169. ret ; _cdecl return
  170. strchr endp
  171. end