Advertisement
Guest User

Strlen

a guest
Sep 1st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. .model flat,c
  2. .code
  3.  
  4. ;extern "C" int SseTextStringCalcLength_(const char* s);
  5. ;
  6. ;Description: The following function calculates the length of a
  7. ; text string using the x86-SSe instruction pcmpistri.
  8. ;
  9. ;Returns: Length of text string
  10. ;
  11. ;Requires SSE4.2
  12.  
  13. SseTextStringCalcLength_ proc
  14. push ebp
  15. mov ebp,esp
  16.  
  17. ;Initialize registers for string length calculation
  18. mov eax,[ebp+8] ;eax = 's'
  19. sub eax,16 ;adjust eax for use in loop
  20. mov edx,0ff01h
  21. movd xmm1,edx ;xmm1[15:0] = char range
  22.  
  23. ;Calculate next address and test for near end-of-page condition
  24. @@: add eax,16 ;eax = next text block
  25. mov edx,eax
  26. and edx,0fffh ;edx = low 12 bits of address
  27. cmp edx,0ff0h
  28. ja NearEndOfPage ;jump if within 16 bytest of page boundary
  29.  
  30. ;Test current text block for '\0' byte
  31. pcmpistri xmm1,[eax],14h ;compare char range and text
  32. jnz @B ;jump if '\0' byte not found
  33.  
  34. ;Found '\0' byte in current block (index in ECX)
  35. ;Calculate string length and return
  36. add eax,ecx ;eax = ptr to '\0' byte
  37. sub eax,[ebp+8] ;eax = final string length
  38. pop ebp
  39. ret
  40.  
  41. ; Search for the '\0' terminator by examing each character
  42. NearEndOfPage:
  43. mov ecx,4096 ;ecx = size of page in bytes
  44. sub ecx, edx ;ecx = number of bytes to check
  45.  
  46. @@: mov dl,[eax] ;dl = next text string character
  47. or dl,dl
  48. jz FoundNull ;jump if '\0' found
  49. inc eax ;eax = ptr to next char
  50. dec ecx
  51. jnz @B ;jump if more chars to test
  52.  
  53. ;Remainder of text string can be searched using 16-byte blocks
  54. ;EAX is now aligned on a 16-byte boundary
  55. sub eax,16 ;adjust eax for use in loop
  56. @@: add eax,16 ;eax = ptr to next text block
  57. pcmpistri xmm1,[eax],14h ;compare char range and text
  58. jnz @B ;jump if '\0' byte not found
  59.  
  60. ;Found '\0' byte in current block(index in ECX)
  61. add eax,ecx ;eax = ptr to '\0' byte
  62.  
  63. ;Calculate final string length and return
  64. FoundNull:
  65. sub eax,[ebp+8] ;eax = final string length
  66. pop ebp
  67. ret
  68. SseTextStringCalcLength_ endp
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement