Source code of Windows XP (NT5)
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.

121 lines
3.0 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. ;
  24. ;*******************************************************************************
  25. .xlist
  26. include cruntime.inc
  27. .list
  28. page
  29. ;***
  30. ;char *_strrev(string) - reverse a string in place
  31. ;
  32. ;Purpose:
  33. ; Reverses the order of characters in the string. The terminating
  34. ; null character remains in place.
  35. ;
  36. ; Algorithm:
  37. ; char *
  38. ; _strrev (string)
  39. ; char *string;
  40. ; {
  41. ; char *start = string;
  42. ; char *left = string;
  43. ; char ch;
  44. ;
  45. ; while (*string++)
  46. ; ;
  47. ; string -= 2;
  48. ; while (left < string)
  49. ; {
  50. ; ch = *left;
  51. ; *left++ = *string;
  52. ; *string-- = ch;
  53. ; }
  54. ; return(start);
  55. ; }
  56. ;
  57. ; NOTE: There is a check for an empty string in the following code.
  58. ; Normally, this would fall out of the "cmp si,di" instruction in the
  59. ; loop portion of the routine. However, if the offset of the empty
  60. ; string is 0 (as it could be in large model), then the cmp does not
  61. ; catch the empty string and the routine essentially hangs (i.e., loops
  62. ; moving bytes one at a time FFFFh times). An explicit empty string
  63. ; check corrects this.
  64. ;
  65. ;Entry:
  66. ; char *string - string to reverse
  67. ;
  68. ;Exit:
  69. ; returns string - now with reversed characters
  70. ;
  71. ;Uses:
  72. ;
  73. ;Exceptions:
  74. ;
  75. ;*******************************************************************************
  76. CODESEG
  77. public _strrev
  78. _strrev proc \
  79. uses edi esi, \
  80. string:ptr byte
  81. mov edi,[string] ; di = string
  82. mov edx,edi ; dx=pointer to string; save return value
  83. mov esi,edi ; si=pointer to string
  84. xor eax,eax ; search value (null)
  85. or ecx,-1 ; cx = -1
  86. repne scasb ; find null
  87. cmp ecx,-2 ; is string empty? (if offset value is 0, the
  88. je short done ; cmp below will not catch it and we'll hang).
  89. dec edi ; string is not empty, move di pointer back
  90. dec edi ; di points to last non-null byte
  91. lupe:
  92. cmp esi,edi ; see if pointers have crossed yet
  93. jae short done ; exit when pointers meet (or cross)
  94. mov ah,[esi] ; get front byte...
  95. mov al,[edi] ; and end byte
  96. mov [esi],al ; put end byte in front...
  97. mov [edi],ah ; and front byte at end
  98. inc esi ; front moves up...
  99. dec edi ; and end moves down
  100. jmp short lupe ; keep switching bytes
  101. done:
  102. mov eax,edx ; return value: string addr
  103. ifdef _STDCALL_
  104. ret DPSIZE ; _stdcall return
  105. else
  106. ret ; _cdecl return
  107. endif
  108. _strrev endp
  109. end