Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #       memset()                                        Author: Kees J. Bot
  2. #                                                               2 Jan 1994
  3. # void *memset(void *s, int c, size_t n)
  4. #       Set a chunk of memory to the same byte value.
  5. #
  6.  
  7. .intel_syntax
  8.  
  9. .global _memset
  10.  
  11.         .text
  12.         .align  16
  13. _memset:
  14.         push  ebp
  15.         mov ebp, esp
  16.         push  edi
  17.         mov     edi, [8+ebp]     # The string
  18.         movzx   eax, byte ptr [12+ebp]    # The fill byte
  19.         mov     ecx, [16+ebp]    # Length
  20.         cld
  21.         cmp ecx, 16
  22.         jb      sbyte           # Don't bother being smart with short arrays
  23.         test  edi, 1
  24.         jnz     sbyte           # Bit 0 set, use byte store
  25.         test  edi, 2
  26.         jnz     sword           # Bit 1 set, use word store
  27. slword:
  28.         movb    ah, al
  29.         mov edx, eax
  30.         sal edx, 16
  31.         or      eax, edx        # One byte to four bytes
  32.         shrd    edx, ecx, 2     # Save low two bits of ecx in edx
  33.         shr ecx, 2
  34.         rep     stosd           # Store longwords.
  35.         shld    ecx, edx, 2     # Restore low two bits
  36.         sword:
  37.         movb    ah, al          # One byte to two bytes
  38.         shr ecx, 1
  39.         rep     stosw           # Store words
  40.         adc     ecx, ecx        # One more byte?
  41. sbyte:
  42.         rep     stosb           # Store bytes
  43. done:
  44.         mov     eax, [8+ebp]    # Return some value you have no need for
  45.         pop edi
  46.         pop ebp
  47.         ret
  48.