News:

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

OK - I'm never going to get this

Started by Astro, September 09, 2009, 01:06:08 AM

Previous topic - Next topic

KeepingRealBusy

Astro,

QuoteI think my original question was actually to do with comparing the strings!

Is it correct for comparing 4 bytes in one go, 4 times, for a total of 16 bytes?

This will work as long as both strings are padded to the end of the 16 bytes with nulls (0's) for any characters that follow the initial string terminator (the first null). This also works only for a match/no-match comparison test (je/jne). If you want to know which string is larger than the other (ja/jb) then when a mismatched DWORD compare is found, you need to get both mismatched DWORDS in two regs, BSWAP both registers to convert the DWORDS to big-endian format, then compare the regs again and ja/jb.

Remember, if you have a DWORD with a value of 01234567h in a register and you save it in memory, it is saved in little-endian format as 67452301h, and when loaded back in a register it is converted back to 01234567h. But a string compare is an implied big-endian operation ("abbc" is less than "bbbb" but in regs it appears as 'c' 'b' 'b' 'a' compared to 'b' 'b' 'b' 'b'  which makes ir appear that the second string is less than the first string - after the BSWAPs the regs will appear as 'a' 'b' 'b' 'c' and 'b' 'b' 'b' 'b' and the compare will be correct).

Dave.

Astro

Hi,

Thanks for the response.

Both strings are known to be 16 bytes long, and will be zero-terminated at the 16th byte in both cases.

Quoteit is saved in little-endian format as 67452301h
Ahh - that answers a query I had. I couldn't decide it if reversed the data type (e.g. DWORD 0x12345678 became 0x87654321 or whether it was always just the bytes).

That's great - thanks!

Best regards,
Astro.

hutch--

If waht you are comparing is an exact 16 bytes every time the task is very easy. The example was easier to write than explain. This example assumes it needs to be done fast in a streaming context, if it only needs to be done occasionally it could be done simp[ler but slower.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    include \masm32\include\masm32rt.inc
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

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


    dword_by_4_compare PROTO :DWORD, :DWORD


    .data
      cmpdata db "1234567812345678",0  ; 16 bytes zero terminated

      yesdata db "1234567812345678",0  ; matching 16 bytes

      nodata  db "1234567x12345678",0  ; mismatch 16 bytes



    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    invoke dword_by_4_compare,OFFSET cmpdata,OFFSET yesdata
    print str$(eax),13,10


    invoke dword_by_4_compare,OFFSET cmpdata,OFFSET nodata
    print str$(eax),13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 4

dword_by_4_compare proc psrc:DWORD, pcmp:DWORD

    push esi
    push edi

    mov esi, [esp+4+8]
    mov edi, [esp+8+8]

  ; ----------------------------

    mov eax, [esi]
    mov ecx, [edi]
    cmp eax, ecx
    jne nomatch

    mov eax, [esi+4]
    mov ecx, [edi+4]
    cmp eax, ecx
    jne nomatch

    mov eax, [esi+8]
    mov ecx, [edi+8]
    cmp eax, ecx
    jne nomatch

    mov eax, [esi+12]
    mov ecx, [edi+12]
    cmp eax, ecx
    jne nomatch

  ; ----------------------------

  match:
    mov eax, 1      ; return 1 for string match
    pop edi
    pop esi

    ret 8

  nomatch:
    xor eax, eax    ; return zero on fail
    pop edi
    pop esi

    ret 8

dword_by_4_compare endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php