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.

187 lines
3.9 KiB

  1. page ,132
  2. title memchr - search memory for a given character
  3. ;***
  4. ;memchr.asm - search block of memory for a given character
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines memchr() - search memory until a character is
  10. ; found or a limit is reached.
  11. ;
  12. ;Revision History:
  13. ; 05-16-84 RN initial version
  14. ; 07-20-87 SKS rewritten for speed
  15. ; 05-17-88 SJM Add model-independent (large model) ifdef
  16. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  17. ; 08-23-88 JCR 386 cleanup
  18. ; 10-25-88 JCR General cleanup for 386-only code
  19. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  20. ; 05-10-91 GJF Back to _cdecl, sigh...
  21. ; 05-01-95 GJF New, faster version from Intel!
  22. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  23. ;
  24. ;*******************************************************************************
  25. .xlist
  26. include cruntime.inc
  27. .list
  28. page
  29. ;***
  30. ;char *memchr(buf, chr, cnt) - search memory for given character.
  31. ;
  32. ;Purpose:
  33. ; Searched at buf for the given character, stopping when chr is
  34. ; first found or cnt bytes have been searched through.
  35. ;
  36. ; Algorithm:
  37. ; char *
  38. ; memchr (buf, chr, cnt)
  39. ; char *buf;
  40. ; int chr;
  41. ; unsigned cnt;
  42. ; {
  43. ; while (cnt && *buf++ != c)
  44. ; cnt--;
  45. ; return(cnt ? --buf : NULL);
  46. ; }
  47. ;
  48. ;Entry:
  49. ; char *buf - memory buffer to be searched
  50. ; char chr - character to search for
  51. ; unsigned cnt - max number of bytes to search
  52. ;
  53. ;Exit:
  54. ; returns pointer to first occurence of chr in buf
  55. ; returns NULL if chr not found in the first cnt bytes
  56. ;
  57. ;Uses:
  58. ;
  59. ;Exceptions:
  60. ;
  61. ;*******************************************************************************
  62. CODESEG
  63. public memchr
  64. memchr proc
  65. .FPO ( 0, 1, 0, 0, 0, 0 )
  66. mov eax,[esp+0ch] ; eax = count
  67. push ebx ; Preserve ebx
  68. test eax,eax ; check if count=0
  69. jz short retnull ; if count=0, leave
  70. mov edx,[esp+8] ; edx = buffer
  71. xor ebx,ebx
  72. mov bl,[esp+0ch] ; bl = search char
  73. test edx,3 ; test if string is aligned on 32 bits
  74. jz short main_loop_start
  75. str_misaligned: ; simple byte loop until string is aligned
  76. mov cl,byte ptr [edx]
  77. add edx,1
  78. xor cl,bl
  79. je short found
  80. sub eax,1 ; counter--
  81. jz short retnull
  82. test edx,3 ; already aligned ?
  83. jne short str_misaligned
  84. main_loop_start:
  85. sub eax,4
  86. jb short tail_less_then_4
  87. ; set all 4 bytes of ebx to [value]
  88. push edi ; Preserve edi
  89. mov edi,ebx ; edi=0/0/0/char
  90. shl ebx,8 ; ebx=0/0/char/0
  91. add ebx,edi ; ebx=0/0/char/char
  92. mov edi,ebx ; edi=0/0/char/char
  93. shl ebx,10h ; ebx=char/char/0/0
  94. add ebx,edi ; ebx = all 4 bytes = [search char]
  95. jmp short main_loop_entry ; ecx >=0
  96. return_from_main:
  97. pop edi
  98. tail_less_then_4:
  99. add eax,4
  100. jz retnull
  101. tail_loop: ; 0 < eax < 4
  102. mov cl,byte ptr [edx]
  103. add edx,1
  104. xor cl,bl
  105. je short found
  106. sub eax,1
  107. jnz short tail_loop
  108. retnull:
  109. pop ebx
  110. ret ; _cdecl return
  111. main_loop:
  112. sub eax,4
  113. jb short return_from_main
  114. main_loop_entry:
  115. mov ecx,dword ptr [edx] ; read 4 bytes
  116. xor ecx,ebx ; ebx is byte\byte\byte\byte
  117. mov edi,7efefeffh
  118. add edi,ecx
  119. xor ecx,-1
  120. xor ecx,edi
  121. add edx,4
  122. and ecx,81010100h
  123. je short main_loop
  124. ; found zero byte in the loop?
  125. char_is_found:
  126. mov ecx,[edx - 4]
  127. xor cl,bl ; is it byte 0
  128. je short byte_0
  129. xor ch,bl ; is it byte 1
  130. je short byte_1
  131. shr ecx,10h ; is it byte 2
  132. xor cl,bl
  133. je short byte_2
  134. xor ch,bl ; is it byte 3
  135. je short byte_3
  136. jmp short main_loop ; taken if bits 24-30 are clear and bit
  137. ; 31 is set
  138. byte_3:
  139. pop edi ; restore edi
  140. found:
  141. lea eax,[edx - 1]
  142. pop ebx ; restore ebx
  143. ret ; _cdecl return
  144. byte_2:
  145. lea eax,[edx - 2]
  146. pop edi
  147. pop ebx
  148. ret ; _cdecl return
  149. byte_1:
  150. lea eax,[edx - 3]
  151. pop edi
  152. pop ebx
  153. ret ; _cdecl return
  154. byte_0:
  155. lea eax,[edx - 4]
  156. pop edi ; restore edi
  157. pop ebx ; restore ebx
  158. ret ; _cdecl return
  159. memchr endp
  160. end