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.

151 lines
3.4 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. ; 06-12-01 PML inc->add 1, dec->sub 1 for Pentium 4 perf (vs7#267015)
  26. ;
  27. ;*******************************************************************************
  28. .xlist
  29. include cruntime.inc
  30. .list
  31. page
  32. ;***
  33. ;char *memset(dst, value, count) - sets "count" bytes at "dst" to "value"
  34. ;
  35. ;Purpose:
  36. ; Sets the first "count" bytes of the memory starting
  37. ; at "dst" to the character value "value".
  38. ;
  39. ; Algorithm:
  40. ; char *
  41. ; memset (dst, value, count)
  42. ; char *dst;
  43. ; char value;
  44. ; unsigned int count;
  45. ; {
  46. ; char *start = dst;
  47. ;
  48. ; while (count--)
  49. ; *dst++ = value;
  50. ; return(start);
  51. ; }
  52. ;
  53. ;Entry:
  54. ; char *dst - pointer to memory to fill with value
  55. ; char value - value to put in dst bytes
  56. ; int count - number of bytes of dst to fill
  57. ;
  58. ;Exit:
  59. ; returns dst, with filled bytes
  60. ;
  61. ;Uses:
  62. ;
  63. ;Exceptions:
  64. ;
  65. ;*******************************************************************************
  66. CODESEG
  67. public memset
  68. memset proc
  69. .FPO ( 0, 3, 0, 0, 0, 0 )
  70. mov edx,[esp + 0ch] ; edx = "count"
  71. mov ecx,[esp + 4] ; ecx points to "dst"
  72. test edx,edx ; 0?
  73. jz short toend ; if so, nothing to do
  74. xor eax,eax
  75. mov al,[esp + 8] ; the byte "value" to be stored
  76. ; Align address on dword boundary
  77. push edi ; preserve edi
  78. mov edi,ecx ; edi = dest pointer
  79. cmp edx,4 ; if it's less then 4 bytes
  80. jb tail ; tail needs edi and edx to be initialized
  81. neg ecx
  82. and ecx,3 ; ecx = # bytes before dword boundary
  83. jz short dwords ; jump if address already aligned
  84. sub edx,ecx ; edx = adjusted count (for later)
  85. adjust_loop:
  86. mov [edi],al
  87. add edi,1
  88. sub ecx,1
  89. jnz adjust_loop
  90. dwords:
  91. ; set all 4 bytes of eax to [value]
  92. mov ecx,eax ; ecx=0/0/0/value
  93. shl eax,8 ; eax=0/0/value/0
  94. add eax,ecx ; eax=0/0val/val
  95. mov ecx,eax ; ecx=0/0/val/val
  96. shl eax,10h ; eax=val/val/0/0
  97. add eax,ecx ; eax = all 4 bytes = [value]
  98. ; Set dword-sized blocks
  99. mov ecx,edx ; move original count to ecx
  100. and edx,3 ; prepare in edx byte count (for tail loop)
  101. shr ecx,2 ; adjust ecx to be dword count
  102. jz tail ; jump if it was less then 4 bytes
  103. rep stosd
  104. main_loop_tail:
  105. test edx,edx ; if there is no tail bytes,
  106. jz finish ; we finish, and it's time to leave
  107. ; Set remaining bytes
  108. tail:
  109. mov [edi],al ; set remaining bytes
  110. add edi,1
  111. sub edx,1 ; if there is some more bytes
  112. jnz tail ; continue to fill them
  113. ; Done
  114. finish:
  115. mov eax,[esp + 8] ; return dest pointer
  116. pop edi ; restore edi
  117. ret
  118. toend:
  119. mov eax,[esp + 4] ; return dest pointer
  120. ret
  121. memset endp
  122. end