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.

139 lines
3.6 KiB

  1. page ,132
  2. title strlen - return the length of a null-terminated string
  3. ;***
  4. ;strlen.asm - contains strlen() routine
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; strlen returns the length of a null-terminated string,
  10. ; not including the null byte itself.
  11. ;
  12. ;Revision History:
  13. ; 04-21-87 SKS Rewritten to be fast and small, added file header
  14. ; 05-18-88 SJM Add model-independent (large model) ifdef
  15. ; 08-02-88 SJM Add 32 bit code, use cruntime vs cmacros
  16. ; 08-23-88 JCR 386 cleanup
  17. ; 10-05-88 GJF Fixed off-by-2 error.
  18. ; 10-10-88 JCR Minor improvement
  19. ; 10-25-88 JCR General cleanup for 386-only code
  20. ; 10-26-88 JCR Re-arrange regs to avoid push/pop ebx
  21. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  22. ; 05-10-91 GJF Back to _cdecl, sigh...
  23. ; 04-23-93 GJF Tuned for the 486.
  24. ; 06-16-93 GJF Added .FPO directive.
  25. ; 11-28-94 GJF New, faster version from Intel.
  26. ; 11-28-95 GJF Align main_loop on para boundary for 486 and P6
  27. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  28. ;
  29. ;*******************************************************************************
  30. .xlist
  31. include cruntime.inc
  32. .list
  33. page
  34. ;***
  35. ;strlen - return the length of a null-terminated string
  36. ;
  37. ;Purpose:
  38. ; Finds the length in bytes of the given string, not including
  39. ; the final null character.
  40. ;
  41. ; Algorithm:
  42. ; int strlen (const char * str)
  43. ; {
  44. ; int length = 0;
  45. ;
  46. ; while( *str++ )
  47. ; ++length;
  48. ;
  49. ; return( length );
  50. ; }
  51. ;
  52. ;Entry:
  53. ; const char * str - string whose length is to be computed
  54. ;
  55. ;Exit:
  56. ; EAX = length of the string "str", exclusive of the final null byte
  57. ;
  58. ;Uses:
  59. ; EAX, ECX, EDX
  60. ;
  61. ;Exceptions:
  62. ;
  63. ;*******************************************************************************
  64. CODESEG
  65. public strlen
  66. strlen proc
  67. .FPO ( 0, 1, 0, 0, 0, 0 )
  68. string equ [esp + 4]
  69. mov ecx,string ; ecx -> string
  70. test ecx,3 ; test if string is aligned on 32 bits
  71. je short main_loop
  72. str_misaligned:
  73. ; simple byte loop until string is aligned
  74. mov al,byte ptr [ecx]
  75. add ecx,1
  76. test al,al
  77. je short byte_3
  78. test ecx,3
  79. jne short str_misaligned
  80. add eax,dword ptr 0 ; 5 byte nop to align label below
  81. align 16 ; should be redundant
  82. main_loop:
  83. mov eax,dword ptr [ecx] ; read 4 bytes
  84. mov edx,7efefeffh
  85. add edx,eax
  86. xor eax,-1
  87. xor eax,edx
  88. add ecx,4
  89. test eax,81010100h
  90. je short main_loop
  91. ; found zero byte in the loop
  92. mov eax,[ecx - 4]
  93. test al,al ; is it byte 0
  94. je short byte_0
  95. test ah,ah ; is it byte 1
  96. je short byte_1
  97. test eax,00ff0000h ; is it byte 2
  98. je short byte_2
  99. test eax,0ff000000h ; is it byte 3
  100. je short byte_3
  101. jmp short main_loop ; taken if bits 24-30 are clear and bit
  102. ; 31 is set
  103. byte_3:
  104. lea eax,[ecx - 1]
  105. mov ecx,string
  106. sub eax,ecx
  107. ret
  108. byte_2:
  109. lea eax,[ecx - 2]
  110. mov ecx,string
  111. sub eax,ecx
  112. ret
  113. byte_1:
  114. lea eax,[ecx - 3]
  115. mov ecx,string
  116. sub eax,ecx
  117. ret
  118. byte_0:
  119. lea eax,[ecx - 4]
  120. mov ecx,string
  121. sub eax,ecx
  122. ret
  123. strlen endp
  124. end