home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / chasm2.zip / STRCOMP.ASM < prev    next >
Assembly Source File  |  1986-10-31  |  3KB  |  74 lines

  1. ;==============================================================
  2. ; function StrComp(var Str1, Str2: AnyString): integer;
  3. ;    external 'strcomp';
  4. ;
  5. ; External function for Turbo Pascal
  6. ;
  7. ; Compares two strings, returning an integer as follows:
  8. ;
  9. ;          0 : Str1 = Str2
  10. ;          1 : Str1 > Str2
  11. ;          2 : Str2 > Str1
  12. ;
  13. ; Turbo's string compare routine copies both strings onto
  14. ; the stack and compares the copies.  This routine runs
  15. ; about 3 times faster by passing pointers and comparing
  16. ; the strings in place.
  17. ;
  18. ; Another advantage is that the integer result can be used to
  19. ; drive a CASE statement, eliminating multiple comparisons.
  20. ;================================================================
  21.  
  22. stack         struc                ;models structure of stack
  23. oldbp         dw  0000H            ;saved BP register
  24. retaddr       dw  0000H            ;return address to Turbo
  25. str2ptr       dw  0000H, 0000H     ;address of 2nd parameter
  26. str1ptr       dw  0000H, 0000H     ;address of 1st parameter
  27. result        dw  0000H            ;funtion result field
  28.               endstruc
  29.  
  30. strcomp       proc   near
  31.               push   bp               ;establish addressability
  32.               mov    bp, sp           ;of parameters
  33.               push   ds
  34.  
  35.               lds    si, str1ptr[bp]  ;point to string 1
  36.               les    di, str2ptr[bp]  ;and string 2
  37.  
  38.               mov    cl, [si]         ;CX <== length(Str1)
  39.               xor    ch, ch           ;  ditto
  40.               inc    si               ;point to 1st char of string
  41.  
  42.               mov    dl, es:[di]      ;DX <== length(Str2)
  43.               xor    dh, dh           ;   ditto
  44.               inc    di               ;point to 1st char of string
  45.  
  46.               mov    bh, cl           ;save lengths for later
  47.               mov    bl, dl
  48.  
  49.               cmp    cl, dl           ;use shorter length
  50.               jb     s1               ;skip if CL has short one
  51.               xchg   cl, dl           ;otherwise switch them
  52.  
  53. s1            or     cl, cl           ;null string?
  54.               jz     s2               ;skip if so
  55.  
  56.               repe                    ;perform string comparison
  57.               cmpsb
  58.               jne    s4
  59.  
  60. s2            cmp    bh, bl           ;substrings equal? if so,
  61.                                       ;resolve by length comparison
  62.  
  63. s3            jne    s4               ;test for equal option
  64.               xor    ax, ax           ;send 0 for equal
  65.               jmps   exit
  66. s4            jb     s5               ;St1 > St2?
  67.               mov    ax, 0001H        ;send 1 if St1 greater
  68.               jmps   exit
  69. s5            mov    ax, 0002H        ;send 2 if St2 greater
  70.  
  71. exit          pop    ds
  72.               pop    bp
  73.               ret    0AH
  74.               endp