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.

142 lines
3.4 KiB

  1. page ,132
  2. title memccpy - copy bytes until character found
  3. ;***
  4. ;memccpy.asm - copy bytes until a character is found
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; defines _memccpy() - copies bytes until a specifed character
  10. ; is found, or a maximum number of characters have been copied.
  11. ;
  12. ;Revision History:
  13. ; 05-16-84 RN initial version
  14. ; 05-17-88 SJM Add model-independent (large model) ifdef
  15. ; 08-04-88 SJM convert to cruntime/ add 32-bit support
  16. ; 08-23-88 JCR Minor 386 adjustments
  17. ; 10-25-88 JCR General cleanup for 386-only code
  18. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  19. ; 01-17-91 GJF ANSI naming.
  20. ; 05-10-91 GJF Back to _cdecl, sigh...
  21. ; 10-27-92 SKS Avoid using a MASM keyword ("C") as a parameter name
  22. ; 01-19-95 GJF Faster version courtesy of Intel Israel.
  23. ; 01-24-95 GJF Added FPO directive.
  24. ;
  25. ;*******************************************************************************
  26. .xlist
  27. include cruntime.inc
  28. .list
  29. page
  30. ;***
  31. ;char *_memccpy(dest, src, _c, count) - copy bytes until character found
  32. ;
  33. ;Purpose:
  34. ; Copies bytes from src to dest until count bytes have been
  35. ; copied, or up to and including the character _c, whichever
  36. ; comes first.
  37. ;
  38. ; Algorithm:
  39. ; char *
  40. ; _memccpy (dest, src, _c, count)
  41. ; char *dest, *src, _c;
  42. ; unsigned int count;
  43. ; {
  44. ; while (count && (*dest++ = *src++) != _c)
  45. ; count--;
  46. ;
  47. ; return(count ? dest : NULL);
  48. ; }
  49. ;
  50. ;Entry:
  51. ; char *dest - pointer to memory to receive copy
  52. ; char *src - source of bytes
  53. ; char _c - character to stop copy at
  54. ; int count - max number of bytes to copy
  55. ;
  56. ;Exit:
  57. ; returns pointer to byte immediately after _c in dest;
  58. ; returns NULL if _c was never found
  59. ;
  60. ;Uses:
  61. ;
  62. ;Exceptions:
  63. ;
  64. ;*******************************************************************************
  65. CODESEG
  66. public _memccpy
  67. _memccpy proc
  68. .FPO ( 0, 4, 0, 0, 0, 0 )
  69. mov ecx,[esp + 10h] ; ecx = max byte count
  70. push ebx ; save ebx
  71. test ecx,ecx ; if it's nothing to move
  72. jz ret_zero_len ; restore ebx, and return NULL
  73. mov bh,[esp + 10h] ; bh = byte to look for
  74. push esi ; save esi
  75. test ecx,1 ; test if counter is odd or even
  76. mov eax,[esp + 0ch] ; eax = dest , don't affect flags
  77. mov esi,[esp + 10h] ; esi = source , don't affect flags
  78. ; nop
  79. jz lupe2 ; if counter is even, do double loop
  80. ; else do one iteration, and drop into double loop
  81. mov bl,[esi] ; get first byte into bl
  82. inc esi ; kick src (esi points to src)
  83. mov [eax],bl ; store it in dest
  84. inc eax ; kick dest
  85. cmp bl,bh ; see if we just moved the byte
  86. je short toend
  87. dec ecx ; decriment counter
  88. jz retnull ; drop into double loop if nonzero
  89. lupe2:
  90. mov bl,[esi] ; get first byte into bl
  91. add esi,2 ; kick esi (src)
  92. cmp bl,bh ; check if we just moved the byte (from bl)
  93. je toend_mov_inc ; store bl & exit
  94. mov [eax],bl ; store first byte from bl
  95. mov bl,[esi - 1] ; get second byte into bl
  96. mov [eax + 1],bl ; store second byte from bl
  97. add eax,2 ; kick eax (dest)
  98. cmp bl,bh ; see if we just moved the byte
  99. je short toend ; end of string
  100. sub ecx,2 ; modify counter, and if nonzero continue
  101. jnz lupe2 ; else drop out & return NULL
  102. retnull:
  103. pop esi
  104. ret_zero_len:
  105. xor eax,eax ; null pointer
  106. pop ebx
  107. ret ; _cdecl return
  108. toend_mov_inc:
  109. mov [eax],bl ; store first byte from bl
  110. inc eax ; eax points rihgt after the value
  111. toend: pop esi
  112. pop ebx
  113. ret ; _cdecl return
  114. _memccpy endp
  115. end