The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Astro on September 09, 2009, 01:06:08 AM

Title: OK - I'm never going to get this
Post by: Astro on September 09, 2009, 01:06:08 AM
 :dazzled:

No doubt I'm trying something illegal here?

A related question: why do some things work some ways, yet when you copy/paste code but use it in a slightly different way (even though the erroring code is itself identical to the working version), it errors like crazy?

mov edx,ptrLIST_INFO
mov ecx,4
mov esi,[edx+16]
mov edi,offset string ; 16-bytes, zero-terminated
repz cmpsd


Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: 2-Bit Chip on September 09, 2009, 01:18:43 AM
What is LIST_INFO? Can't find it on msdn.microsoft.com or in any of the MASM32 include files.
Title: Re: OK - I'm never going to get this
Post by: dedndave on September 09, 2009, 01:36:19 AM
ptrLIST_INFO and - what is at ptrLIST_INFO+16 - lol
show us the data structure
show us ptrLIST_INFO and how it gets initialized
i am guessing that you want "mov edx,offset ptrLIST_INFO"
but, i'm not sure - then i have no idea what "[edx+16]" will yield
when you make posts like this, Astro, read them back to yourself and see if you have provided enough info to fix it   :bg
Title: Re: OK - I'm never going to get this
Post by: Astro on September 09, 2009, 09:14:37 AM
Argghh... sorry.

ptrLIST_INFO is my own structure.

At offset 16 (ptrLIST_INFO+16) for 16 bytes is byte data (a zero-terminated string).

I want to compare these 16 bytes to another 16 byte string that is hard coded: string db "MyString",0,0,0,0,0,0,0,0 but want to check all 16 bytes.

Bizarrely my original code worked in a console .exe but now I'm running it as a service it doesn't want to run (memory access violation). I copy/pasted the code, too, so there should be no possibility of an error!!!

I'm going nuts over what I perceive to be simple errors.  :'(

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: dedndave on September 09, 2009, 09:52:07 AM
mov edx,offset ptrLIST_INFO
Title: Re: OK - I'm never going to get this
Post by: Astro on September 09, 2009, 10:10:34 AM
I tried that, and it seems to have stopped crashing there  :U  but now I get another error so I'll have to let you know.

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: Astro on September 10, 2009, 02:22:53 PM
Hi,

After much whacking my head off a wall trying to understand this, it is: mov edx, ptrLIST_INFO

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: dedndave on September 10, 2009, 02:24:57 PM
we have no way of knowing, Astro, as you have not posted the defines for ptrLIST_INFO
Title: Re: OK - I'm never going to get this
Post by: Astro on September 10, 2009, 03:01:45 PM
The bigger problem was that I couldn't see how it was working, but I'm fairly sure I've got it now.  :thumbu

ptrLIST_INFO is a dword, and is a pointer to memory.

If ptrLIST_INFO is at 0x00000001 and contains 0x00005FA3:

0x00000001: 0x00005FA3
...
0x00005FA3: 0x12345678

mov edx,ptrLIST_INFO moves 0x00005FA3 into edx.

[edx] causes the contents of edx to be treated as memory, so when I:

mov dword ptr [edx], 5h

I change:

0x00005FA3: 0x12345678

to:

0x00005FA3: 0x00000005

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: dedndave on September 10, 2009, 04:22:42 PM
QuoteIf ptrLIST_INFO is at 0x00000001 and contains 0x00005FA3
0x00000001 is protected memory
attempting to read that address will get you our friend Dr Watson with c0000005
at any rate - let us know what the solution was
Title: Re: OK - I'm never going to get this
Post by: Astro on September 10, 2009, 05:20:14 PM
Quoteat any rate - let us know what the solution was
I already did. :)

mov edx, ptrLIST_INFO

The address 0x00000001 was an example only.

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: dedndave on September 10, 2009, 07:30:41 PM
ok - but that is what you showed in the first post
i.e. nothing changed and it fixed itself ?
Title: Re: OK - I'm never going to get this
Post by: Astro on September 10, 2009, 09:20:34 PM
 :cheekygreen:

Umm yeah...

I think my original question was actually to do with comparing the strings! :cheekygreen:

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

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: FORTRANS on September 10, 2009, 09:54:03 PM
Quote from: Astro on September 10, 2009, 09:20:34 PM
I think my original question was actually to do with comparing the strings! :cheekygreen:

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

Hi Astro,

   Yes, that will work.

Steve
Title: Re: OK - I'm never going to get this
Post by: Astro on September 10, 2009, 09:54:44 PM
Great! Thanks!!  :U

Best regards,
Astro.
Title: Re: OK - I'm never going to get this
Post by: KeepingRealBusy on September 13, 2009, 03:01:36 PM
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.
Title: Re: OK - I'm never going to get this
Post by: Astro on September 13, 2009, 05:05:13 PM
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.
Title: Re: OK - I'm never going to get this
Post by: hutch-- on September 13, 2009, 09:30:23 PM
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