News:

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

String comparing issue

Started by Nilrem, April 18, 2005, 08:38:37 AM

Previous topic - Next topic

hutch--

Here is the same algo with the stack frame removed. It probably does not matter in this context but its easy enough to do and it appears to be working OK.

The algo is correctly binary compare rather than string compare and can be used for any memory comparison, not just string data.


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

align 4

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

bytecmp proc txt1:DWORD,txt2:DWORD,cnt:DWORD

    push esi

    mov esi, [esp+8]    ; load 1st byte data in ESI
    mov edx, [esp+12]   ; load 2nd byte data in EDX
    mov eax, [esp+16]   ; load byte count in EAX
    add esi, eax        ; add byte count to ESI
    add edx, eax        ; add byte count to EDX
    neg eax             ; reverse sign in EAX
    xor ecx, ecx        ; clear ECX to prevent stall with CL

  align 4
  @@:
    mov cl, [esi+eax]   ; load byte at ESI into BL
    cmp cl, [edx+eax]   ; compare BL to byte in EDX
    jne @F              ; exit if not equal
    add eax, 1          ; add 1 to test next pair of bytes
    jnz @B              ; jump back if count is not zero

    sub eax, 1          ; non zero is matching strings
    jmp quit

  @@:
    xor eax, eax        ; zero is non matching strings

  quit:
    pop esi

    ret 12

bytecmp endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

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

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

lingo


"Thankyou.  Had problems getting it to work..."
Now is OK
Try again pls :bdg

Regards,
Lingo

AeroASM

hey hutch:

If you want to negate a number, don't you have to use neg eax / add eax,1

instead of just neg eax?

hutch--

Aero,

Just write two examples and test it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

AeroASM

Thats  weird. It worked in practice, but in thoery it does not work.

Consider half-byte signed numbers:

1111 = -1
1110 = -2
1101 = -3
1100 = -4
1011 = -5
1010 = -6
1001 = -7
1000 = -8
0111 = 7
0110 = 6
0101 = 5
0100 = 4
0011 = 3
0010 = 2
0001 = 1
0000 = 0

Take 5 = 0101
neg eax gives 1010 = -6
therefore neg eax \ add eax,1 gives -6 + 1 = -5

tenkey

What theory?

Where did you get the idea that NEG of 0101 produces 1010 ?
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

MichaelW

AeroASM,

I think you are confusing not and neg.

neg eax

would be functionally equivalent to

not eax
inc eax

eschew obfuscation

pbrennick

I think he forgot to do the twos compliment.

Paul

AeroASM

MichaelW: spot on
pbrennick: wtf is twos complement?

thomasantony

Quote from: AeroASM on April 23, 2005, 05:33:10 PM
MichaelW: spot on
pbrennick: wtf is twos complement?
Yeah,
  I was thinking of asking that. I have seen both one's complement and two's complement and they have complemented my mind :wink

Thomas :U
There are 10 types of people in the world. Those who understand binary and those who don't.


Programmer's Directory. Submit for free

MichaelW

I posted this on the old forum, but it appears to have been lost.

Two's Complement
----------------
Processors of the x86 family use a two's complement representation
for signed integers. With this representation, the most significant
bit functions as a sign bit. A value of 0 for the sign bit indicates
a positive integer and a value of 1 indicates a negative integer.

For simplicity, the following examples use bytes, but the concept
is applicable to any size integer.

The value of a positive integer is the value of the bits:

   0000 0000b =  0
   0000 0001b = +1
   0000 0010b = +2
   ...
   0111 1111b = +127

The value of a negative integer is the value of the bits
minus 2^n, where n is the number of bits.

   1111 1111b (255) minus 2 ^ 8 (256) = -1
   1111 1110b (254) minus 2 ^ 8 (256) = -2
   1111 1101b (253) minus 2 ^ 8 (256) = -3
   ...
   1000 0000b (128) minus 2 ^ 8 (256) = -128

The next two examples require an understanding of the rules
for binary addition:

   0 + 0 = 0
   0 + 1 = 1
   1 + 0 = 1
   1 + 1 = 0 carry 1

To convert a value from positive to negative, or from negative
to positive, you invert (complement) all the bits and add 1:

   Start with +1:    0000 0001
   Invert all bits:  1111 1110
   Add 1:            0000 0001
   Now have -1:      1111 1111
   Invert all bits:  0000 0000
   Add 1:            0000 0001
   Now have +1:      0000 0001

You can verify that 1111 1111b represents -1 by adding 1:

   Start with -1:    1111 1111
   Add 1:            0000 0001
   Result is 0:    1 0000 0000 (carry ignored)


eschew obfuscation

AeroASM

Quote from: pbrennick on April 23, 2005, 05:21:41 PM
I think he forgot to do the twos compliment.

Paul


Thats what confused me. I knew that twos complement was a system of negative numbers, but I didn't know how to do a system.

tenkey

Two's complement is also the name of an operation that creates the arithmetic negative in a two's complement system.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

AeroASM

OK, got it. When Pbrennick said I forgot to do the twos complement, he meant that I did not know that there is an opcode to do it.

Nilrem

Hutch the problem arises if I have one string called Thunder and another Thunder2.