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