home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / platform / strcmp.asm < prev    next >
Assembly Source File  |  1998-06-17  |  4KB  |  151 lines

  1.         page    ,132
  2.         title   strcmp.asm - compare two strings
  3. ;***
  4. ;strcmp.asm - routine to compare two strings (for equal, less, or greater)
  5. ;
  6. ;       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ;       STRCMP compares two strings and returns an integer
  10. ;       to indicate whether the first is less than the second, the two are
  11. ;       equal, or whether the first is greater than the second, respectively.
  12. ;       Comparison is done byte by byte on an UNSIGNED basis, which is to
  13. ;       say that Null (0) is less than any other character (1-255).
  14. ;
  15. ;*******************************************************************************
  16.  
  17.         .xlist
  18.         include cruntime.inc
  19.         .list
  20.  
  21. page
  22. ;***
  23. ;strcmp - compare two strings, returning less than, equal to, or greater than
  24. ;
  25. ;Purpose:
  26. ;       Compares two string, determining their lexical order.  Unsigned
  27. ;       comparison is used.
  28. ;
  29. ;       Algorithm:
  30. ;          int strcmp ( src , dst )
  31. ;                  unsigned char *src;
  32. ;                  unsigned char *dst;
  33. ;          {
  34. ;                  int ret = 0 ;
  35. ;
  36. ;                  while( ! (ret = *src - *dst) && *dst)
  37. ;                          ++src, ++dst;
  38. ;
  39. ;                  if ( ret < 0 )
  40. ;                          ret = -1 ;
  41. ;                  else if ( ret > 0 )
  42. ;                          ret = 1 ;
  43. ;
  44. ;                  return( ret );
  45. ;          }
  46. ;
  47. ;Entry:
  48. ;       const char * src - string for left-hand side of comparison
  49. ;       const char * dst - string for right-hand side of comparison
  50. ;
  51. ;Exit:
  52. ;       AX < 0, 0, or >0, indicating whether the first string is
  53. ;       Less than, Equal to, or Greater than the second string.
  54. ;
  55. ;Uses:
  56. ;       CX, DX
  57. ;
  58. ;Exceptions:
  59. ;
  60. ;*******************************************************************************
  61.  
  62.         CODESEG
  63.  
  64.         public  strcmp
  65. strcmp  proc
  66.  
  67.         .FPO    ( 0, 2, 0, 0, 0, 0 )
  68.  
  69.         mov     edx,[esp + 4]   ; edx = src
  70.         mov     ecx,[esp + 8]   ; ecx = dst
  71.  
  72.         test    edx,3
  73.         jnz     short dopartial
  74.  
  75.         align   4
  76. dodwords:
  77.         mov     eax,[edx]
  78.  
  79.         cmp     al,[ecx]
  80.         jne     short donene
  81.         or      al,al
  82.         jz      short doneeq
  83.         cmp     ah,[ecx + 1]
  84.         jne     short donene
  85.         or      ah,ah
  86.         jz      short doneeq
  87.  
  88.         shr     eax,16
  89.  
  90.         cmp     al,[ecx + 2]
  91.         jne     short donene
  92.         or      al,al
  93.         jz      short doneeq
  94.         cmp     ah,[ecx + 3]
  95.         jne     short donene
  96.         add     ecx,4
  97.         add     edx,4
  98.         or      ah,ah
  99.         jnz     short dodwords
  100.  
  101.         align   4
  102. doneeq:
  103.         xor     eax,eax
  104.         ret
  105.  
  106.         align   4
  107. donene:
  108.         ; The instructions below should place -1 in eax if src < dst,
  109.         ; and 1 in eax if src > dst.
  110.  
  111.         sbb     eax,eax
  112.         sal     eax,1
  113.         inc     eax
  114.         ret
  115.  
  116.         align   4
  117. dopartial:
  118.         test    edx,1
  119.         jz      short doword
  120.  
  121.         mov     al,[edx]
  122.         inc     edx
  123.         cmp     al,[ecx]
  124.         jne     short donene
  125.         inc     ecx
  126.         or      al,al
  127.         jz      short doneeq
  128.  
  129.         test    edx,2
  130.         jz      short dodwords
  131.  
  132.  
  133.         align   4
  134. doword:
  135.         mov     ax,[edx]
  136.         add     edx,2
  137.         cmp     al,[ecx]
  138.         jne     short donene
  139.         or      al,al
  140.         jz      short doneeq
  141.         cmp     ah,[ecx + 1]
  142.         jne     short donene
  143.         or      ah,ah
  144.         jz      short doneeq
  145.         add     ecx,2
  146.         jmp     short dodwords
  147.  
  148. strcmp  endp
  149.  
  150.         end
  151.