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.

122 lines
3.2 KiB

  1. page ,132
  2. title strrev - reverse a string in place
  3. ;***
  4. ;strrev.asm - reverse a string in place
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines _strrev() - reverse a string in place (not including
  10. ; '\0' character)
  11. ;
  12. ;Revision History:
  13. ; 10-26-83 RN initial version
  14. ; 07-16-87 JCR Added check for empty string (fixes large model bug)
  15. ; 05-18-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, minor alterations
  18. ; 10-26-88 JCR General cleanup for 386-only code
  19. ; 10-26-88 JCR Re-arrange regs to avoid push/pop ebx
  20. ; 03-26-90 GJF Changed to _stdcall. Also, fixed the copyright.
  21. ; 01-18-91 GJF ANSI naming.
  22. ; 05-10-91 GJF Back to _cdecl, sigh...
  23. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  24. ;
  25. ;*******************************************************************************
  26. .xlist
  27. include cruntime.inc
  28. .list
  29. page
  30. ;***
  31. ;char *_strrev(string) - reverse a string in place
  32. ;
  33. ;Purpose:
  34. ; Reverses the order of characters in the string. The terminating
  35. ; null character remains in place.
  36. ;
  37. ; Algorithm:
  38. ; char *
  39. ; _strrev (string)
  40. ; char *string;
  41. ; {
  42. ; char *start = string;
  43. ; char *left = string;
  44. ; char ch;
  45. ;
  46. ; while (*string++)
  47. ; ;
  48. ; string -= 2;
  49. ; while (left < string)
  50. ; {
  51. ; ch = *left;
  52. ; *left++ = *string;
  53. ; *string-- = ch;
  54. ; }
  55. ; return(start);
  56. ; }
  57. ;
  58. ; NOTE: There is a check for an empty string in the following code.
  59. ; Normally, this would fall out of the "cmp si,di" instruction in the
  60. ; loop portion of the routine. However, if the offset of the empty
  61. ; string is 0 (as it could be in large model), then the cmp does not
  62. ; catch the empty string and the routine essentially hangs (i.e., loops
  63. ; moving bytes one at a time FFFFh times). An explicit empty string
  64. ; check corrects this.
  65. ;
  66. ;Entry:
  67. ; char *string - string to reverse
  68. ;
  69. ;Exit:
  70. ; returns string - now with reversed characters
  71. ;
  72. ;Uses:
  73. ;
  74. ;Exceptions:
  75. ;
  76. ;*******************************************************************************
  77. CODESEG
  78. public _strrev
  79. _strrev proc \
  80. uses edi esi, \
  81. string:ptr byte
  82. mov edi,[string] ; di = string
  83. mov edx,edi ; dx=pointer to string; save return value
  84. mov esi,edi ; si=pointer to string
  85. xor eax,eax ; search value (null)
  86. or ecx,-1 ; cx = -1
  87. repne scasb ; find null
  88. cmp ecx,-2 ; is string empty? (if offset value is 0, the
  89. je short done ; cmp below will not catch it and we'll hang).
  90. sub edi,2 ; string is not empty, move di pointer back
  91. ; di points to last non-null byte
  92. lupe:
  93. cmp esi,edi ; see if pointers have crossed yet
  94. jae short done ; exit when pointers meet (or cross)
  95. mov ah,[esi] ; get front byte...
  96. mov al,[edi] ; and end byte
  97. mov [esi],al ; put end byte in front...
  98. mov [edi],ah ; and front byte at end
  99. add esi,1 ; front moves up...
  100. sub edi,1 ; and end moves down
  101. jmp short lupe ; keep switching bytes
  102. done:
  103. mov eax,edx ; return value: string addr
  104. ifdef _STDCALL_
  105. ret DPSIZE ; _stdcall return
  106. else
  107. ret ; _cdecl return
  108. endif
  109. _strrev endp
  110. end