[9] | 1 | ! memcmp() Author: Kees J. Bot
|
---|
| 2 | ! 2 Jan 1994
|
---|
| 3 | .sect .text; .sect .rom; .sect .data; .sect .bss
|
---|
| 4 |
|
---|
| 5 | ! int memcmp(const void *s1, const void *s2, size_t n)
|
---|
| 6 | ! Compare two chunks of memory.
|
---|
| 7 | !
|
---|
| 8 | .sect .text
|
---|
| 9 | .define _memcmp
|
---|
| 10 | .align 16
|
---|
| 11 | _memcmp:
|
---|
| 12 | cld
|
---|
| 13 | push ebp
|
---|
| 14 | mov ebp, esp
|
---|
| 15 | push esi
|
---|
| 16 | push edi
|
---|
| 17 | mov esi, 8(ebp) ! String s1
|
---|
| 18 | mov edi, 12(ebp) ! String s2
|
---|
| 19 | mov ecx, 16(ebp) ! Length
|
---|
| 20 | cmp ecx, 16
|
---|
| 21 | jb cbyte ! Don't bother being smart with short arrays
|
---|
| 22 | mov eax, esi
|
---|
| 23 | or eax, edi
|
---|
| 24 | testb al, 1
|
---|
| 25 | jnz cbyte ! Bit 0 set, use byte compare
|
---|
| 26 | testb al, 2
|
---|
| 27 | jnz cword ! Bit 1 set, use word compare
|
---|
| 28 | clword: shrd eax, ecx, 2 ! Save low two bits of ecx in eax
|
---|
| 29 | shr ecx, 2
|
---|
| 30 | repe
|
---|
| 31 | cmps ! Compare longwords
|
---|
| 32 | sub esi, 4
|
---|
| 33 | sub edi, 4
|
---|
| 34 | inc ecx ! Recompare the last longword
|
---|
| 35 | shld ecx, eax, 2 ! And any excess bytes
|
---|
| 36 | jmp last
|
---|
| 37 | cword: shrd eax, ecx, 1 ! Save low bit of ecx in eax
|
---|
| 38 | shr ecx, 1
|
---|
| 39 | repe
|
---|
| 40 | o16 cmps ! Compare words
|
---|
| 41 | sub esi, 2
|
---|
| 42 | sub edi, 2
|
---|
| 43 | inc ecx ! Recompare the last word
|
---|
| 44 | shld ecx, eax, 1 ! And one more byte?
|
---|
| 45 | cbyte: test ecx, ecx ! Set 'Z' flag if ecx = 0
|
---|
| 46 | last: repe
|
---|
| 47 | cmpsb ! Look for the first differing byte
|
---|
| 48 | seta al ! al = (s1 > s2)
|
---|
| 49 | setb ah ! ah = (s1 < s2)
|
---|
| 50 | subb al, ah
|
---|
| 51 | movsxb eax, al ! eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1
|
---|
| 52 | mov edx, esi ! For bcmp() to play with
|
---|
| 53 | pop edi
|
---|
| 54 | pop esi
|
---|
| 55 | pop ebp
|
---|
| 56 | ret
|
---|