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.

150 lines
3.2 KiB

  1. page ,132
  2. title memset - set sections of memory all to one byte
  3. ;***
  4. ;memset.asm - set a section of memory to all one byte
  5. ;
  6. ; Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; contains the memset() routine
  10. ;
  11. ;Revision History:
  12. ; 05-07-84 RN initial version
  13. ; 06-30-87 SKS faster algorithm
  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-19-88 JCR Enable word alignment code for all models/CPUs,
  17. ; Some code improvement
  18. ; 10-25-88 JCR General cleanup for 386-only code
  19. ; 10-27-88 JCR More optimization (dword alignment, no ebx usage, etc)
  20. ; 03-23-90 GJF Changed to _stdcall. Also, fixed the copyright.
  21. ; 05-10-91 GJF Back to _cdecl, sigh...
  22. ; 01-23-95 GJF Improved routine from Intel Israel. I fixed up the
  23. ; formatting and comments.
  24. ; 01-24-95 GJF Added FPO directive.
  25. ;
  26. ;*******************************************************************************
  27. .xlist
  28. include cruntime.inc
  29. .list
  30. page
  31. ;***
  32. ;char *memset(dst, value, count) - sets "count" bytes at "dst" to "value"
  33. ;
  34. ;Purpose:
  35. ; Sets the first "count" bytes of the memory starting
  36. ; at "dst" to the character value "value".
  37. ;
  38. ; Algorithm:
  39. ; char *
  40. ; memset (dst, value, count)
  41. ; char *dst;
  42. ; char value;
  43. ; unsigned int count;
  44. ; {
  45. ; char *start = dst;
  46. ;
  47. ; while (count--)
  48. ; *dst++ = value;
  49. ; return(start);
  50. ; }
  51. ;
  52. ;Entry:
  53. ; char *dst - pointer to memory to fill with value
  54. ; char value - value to put in dst bytes
  55. ; int count - number of bytes of dst to fill
  56. ;
  57. ;Exit:
  58. ; returns dst, with filled bytes
  59. ;
  60. ;Uses:
  61. ;
  62. ;Exceptions:
  63. ;
  64. ;*******************************************************************************
  65. CODESEG
  66. public memset
  67. memset proc
  68. .FPO ( 0, 3, 0, 0, 0, 0 )
  69. mov edx,[esp + 0ch] ; edx = "count"
  70. mov ecx,[esp + 4] ; ecx points to "dst"
  71. test edx,edx ; 0?
  72. jz short toend ; if so, nothing to do
  73. xor eax,eax
  74. mov al,[esp + 8] ; the byte "value" to be stored
  75. ; Align address on dword boundary
  76. push edi ; preserve edi
  77. mov edi,ecx ; edi = dest pointer
  78. cmp edx,4 ; if it's less then 4 bytes
  79. jb tail ; tail needs edi and edx to be initialized
  80. neg ecx
  81. and ecx,3 ; ecx = # bytes before dword boundary
  82. jz short dwords ; jump if address already aligned
  83. sub edx,ecx ; edx = adjusted count (for later)
  84. adjust_loop:
  85. mov [edi],al
  86. inc edi
  87. dec ecx
  88. jnz adjust_loop
  89. dwords:
  90. ; set all 4 bytes of eax to [value]
  91. mov ecx,eax ; ecx=0/0/0/value
  92. shl eax,8 ; eax=0/0/value/0
  93. add eax,ecx ; eax=0/0val/val
  94. mov ecx,eax ; ecx=0/0/val/val
  95. shl eax,10h ; eax=val/val/0/0
  96. add eax,ecx ; eax = all 4 bytes = [value]
  97. ; Set dword-sized blocks
  98. mov ecx,edx ; move original count to ecx
  99. and edx,3 ; prepare in edx byte count (for tail loop)
  100. shr ecx,2 ; adjust ecx to be dword count
  101. jz tail ; jump if it was less then 4 bytes
  102. rep stosd
  103. main_loop_tail:
  104. test edx,edx ; if there is no tail bytes,
  105. jz finish ; we finish, and it's time to leave
  106. ; Set remaining bytes
  107. tail:
  108. mov [edi],al ; set remaining bytes
  109. inc edi
  110. dec edx ; if there is some more bytes
  111. jnz tail ; continue to fill them
  112. ; Done
  113. finish:
  114. mov eax,[esp + 8] ; return dest pointer
  115. pop edi ; restore edi
  116. ret
  117. toend:
  118. mov eax,[esp + 4] ; return dest pointer
  119. ret
  120. memset endp
  121. end