Subversion Repositories Kolibri OS

Rev

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

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