News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

szCmpi

Started by Mr Earl, March 04, 2007, 09:09:49 PM

Previous topic - Next topic

Mr Earl

I discovered that my copy of szCmpi was not giving correct results.  If the compare failed on the LAST byte of the compare, it returned zero, same as a hit.  Maybe my copy is not up to date?  Here's my version from M32lib:

szCmpi proc src:DWORD, dst:DWORD, ln:DWORD

  ; -----------------------------------------------------------
  ; algorithm compares any two byte sequences CASE INSENSITIVE
  ; and returns zero if they are identical or the character
  ; count where the two byte sequences mismatch.
  ; -----------------------------------------------------------

    push ebx
    push esi
    push edi

    jmp @F
    align 16
  table:
    db   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15
    db  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    db  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47
    db  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
    db  64, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
    db 112,113,114,115,116,117,118,119,120,121,122, 91, 92, 93, 94, 95
    db  96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
    db 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
    db 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143
    db 144,145,146,147,148,149,150,151,152,153,154,155,156,156,158,159
    db 160,161,162,163,164,165,166,167,168,169,170,171,172,173,173,175
    db 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
    db 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207
    db 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
    db 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
    db 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
  @@:

    lea ebx, table      ; table base address
    mov esi, src        ; 1st string in ESI
    mov edi, dst        ; 2nd string in EDI
    mov ecx, ln         ; length in ECX
    add ecx, esi        ; add ESI to get exit condition

  @@:
    mov al, [esi]       ; get 1st byte
    inc esi
    xlatb               ; replace it with byte in table
    mov ah, al          ; store it in AH
    mov al, [edi]       ; get 2nd byte
    inc edi
    xlatb               ; replace it with byte in table
(NOTE: my mods below replace from here to end)
    cmp ah, al          ; compare if equal
    jne @F              ; jump out if not equal
    cmp esi, ecx        ; exit on length
    jl @B
  @@:

    sub esi, src

    .if esi == ln
      xor eax, eax
    .else
      mov eax, esi
    .endif

    pop edi
    pop esi
    pop ebx

    ret

szCmpi endp


Modification to the end:

    ...
    cmp ah, al    ; compare if equal
    jne Failed    ; jump out if not equal
    cmp esi, ecx  ; exit (fall thru to Hit) on length
    jl @B ; else loop back to ck nxt chr

Hit:
    xor eax,eax
    jmp Done

Failed:
    sub esi, src
    mov eax, esi ;set eax=pos that failed

Done:
    pop edi
    pop esi
    pop ebx

    ret

szCmpi endp



PBrennick

That is a shame. Library functions should not fail. I, also have a large library and it is difficult to exhaustively check every function so I can understand how this could happen. I have added this fix to my copy of Hutch's library.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

hutch--

#2
Thanks but its an old version. Below is the module code from the current service pack for version 9.0 which is available in the masm32 sub forum. The version in masm32 9.0 is later than the one you posted and this one below is later again.

Try this version out to see if it does what you require.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    .486                      ; force 32 bit code
    .model flat, stdcall      ; memory model & calling convention
    option casemap :none      ; case sensitive

comment * -----------------------------------------------------------
        This algorithm was developed in conjunction with Jim Giordano
        ----------------------------------------------------------- *

    .data
    align 16
      szCmpi_tbl \
      db   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15
      db  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
      db  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47
      db  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63
      db  64, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
      db 112,113,114,115,116,117,118,119,120,121,122, 91, 92, 93, 94, 95
      db  96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111
      db 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127
      db 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143
      db 144,145,146,147,148,149,150,151,152,153,154,155,156,156,158,159
      db 160,161,162,163,164,165,166,167,168,169,170,171,172,173,173,175
      db 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191
      db 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207
      db 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223
      db 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239
      db 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255

    .code

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 16

szCmpi proc src:DWORD,dst:DWORD,ln:DWORD

    push ebx
    push esi
    push edi

    mov esi, [esp+4+12]
    mov edi, [esp+8+12]
    sub eax, eax                    ; zero eax as index

  align 4
  @@:
    movzx edx, BYTE PTR [esi+eax]
    movzx ebx, BYTE PTR [edi+eax]
    movzx ecx, BYTE PTR [edx+szCmpi_tbl]
    add eax, 1
    cmp cl, [ebx+szCmpi_tbl]
    jne quit
    cmp eax, [esp+12+12]
    jb @b

    sub eax, eax

  quit:
    pop edi
    pop esi
    pop ebx

    ret 12

szCmpi endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««


LATER : Here is a quick test piece and unless I have misunderstood the problem with the old version, it seems to be working properly.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

comment * -----------------------------------------------------
                        Build this  template with
                       "CONSOLE ASSEMBLE AND LINK"
        ----------------------------------------------------- *

    .data?
      value dd ?

    .data
      s1 db "Test",0
      d1 db "tEsT",0

      s2 db "This is a test",0
      d2 db "ThIs iS A TeS-",0

    .code

start:
   
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    call main
    inkey
    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

main proc

    invoke szCmpi,ADDR s1,ADDR d1,4
    print str$(eax),13,10

    invoke szCmpi,ADDR s2,ADDR d2,14
    print str$(eax),13,10

    ret

main endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start


Output is as follows.


0
14
Press any key to continue ...
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php