The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: HiddenDragon on November 22, 2010, 04:27:52 PM

Title: CMP Problem?
Post by: HiddenDragon on November 22, 2010, 04:27:52 PM
I'm using this to compare my Yes and my Compare but it jumps to no. Does cmp not compare the text or what did I do wrong?

.486
.model flat, stdcall
option casemap :none
include \masm32\masm32\include\user32.inc
include \masm32\masm32\include\kernel32.inc
includelib \masm32\masm32\lib\user32.lib
includelib \masm32\masm32\lib\kernel32.lib
.data
Yes db "Yes",0
Compare db "Yes",0
No db "No",0
.code
start:
mov eax, offset Yes
mov ebx, offset Compare
cmp eax, ebx
je yes
jne no
yes:
push 0
push offset Yes
push offset Yes
push 0
call MessageBoxA
jmp done
no:
push 0
push offset No
push offset No
push 0
call MessageBoxA
done:
call ExitProcess
end start
Title: Re: CMP Problem?
Post by: brethren on November 22, 2010, 04:35:29 PM
what you are doing is putting the address of Yes in eax and the address of Compare in ebx, of course those 2 addresses will never be equal

to compare 2 strings you'll need to use a procedure. either code one yourself or have a look at what masm32 has to offer. maybe szcmp will suit your purpose
Title: Re: CMP Problem?
Post by: clive on November 22, 2010, 04:37:33 PM
Comparing addresses won't help. You need a string compare function like strcmp, stricmp, part of the MSVC Runtime Library.

CMP compares the NUMBERS in registers and/or memory. To compare ASCII digits you'll need to step through them one at a time.
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 05:07:35 PM
        push    esi
        push    edi
        mov     esi,offset Yes
        mov     edi,offset Compare
        mov     ecx,sizeof Yes
        repz    cmpsb
        pop     edi
        pop     esi
        mov     eax,offset No
        jnz     no

        mov     eax,offset Yes

no:     push    0
        push    eax
        push    eax
        push    0
        call    MessageBoxA
        push    0
        call    ExitProcess


EDIT - added push/pop esi/edi to be safe   :P
also added push 0 for ExitProcess, to be correct
Title: Re: CMP Problem?
Post by: RuiLoureiro on November 22, 2010, 05:52:15 PM
dedndave,
                Does it work ? Do you get the MessageBox ?
                But all things seems to be correct
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 06:19:19 PM
i didn't try it, Rui   :P
Title: Re: CMP Problem?
Post by: RuiLoureiro on November 22, 2010, 06:21:11 PM
Quote from: dedndave on November 22, 2010, 06:19:19 PM
i didn't try it, Rui   :P
               Ok, try. I tried and it doesnt open the MessageBox
Quote
.486
.model flat, stdcall
option casemap :none

include     \masm32\include\windows.inc

include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib


.data
   Yes     db "Yes",0
   Compare db "Yes",0
   No      db "No",0

.code
   start:

        push    esi
        push    edi
        mov     esi,offset Yes
        mov     edi,offset Compare
        mov     ecx,sizeof Yes
        repz    cmpsb
        pop     edi
        pop     esi
        mov     eax,offset No
        jnz     no

        mov     eax,offset Yes

no:     push    0
        push    eax
        push    eax
        push    0
        call    MessageBoxA
       
        push    0
        call    ExitProcess
;-----------------------------------
end start
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 06:25:22 PM
yes - it works
full program.....
        INCLUDE \masm32\include\masm32rt.inc

        .DATA

Yes     db "Yes",0
Compare db "Yes",0
No      db "No",0

        .CODE

Start:  push    esi
        push    edi
        mov     esi,offset Yes
        mov     edi,offset Compare
        mov     ecx,sizeof Yes
        repz    cmpsb
        pop     edi
        pop     esi
        mov     eax,offset No
        jnz     no

        mov     eax,offset Yes

no:     push    0
        push    eax
        push    eax
        push    0
        call    MessageBoxA
        push    0
        call    ExitProcess

        END     Start


you must be missing an include   :bg
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 06:32:10 PM
well - i don't see any include missing
perhaps you aren't linking it with /SUBSYSTEM:WINDOWS
Title: Re: CMP Problem?
Post by: RuiLoureiro on November 22, 2010, 06:35:24 PM
Ok i used Console assemble link or assemble&link , the MessageBox doesnt open
It runs and close
Title: Re: CMP Problem?
Post by: RuiLoureiro on November 22, 2010, 06:44:49 PM
The problem is connected with the folder where i put the prog
I create a new folder and now it works correctly  :wink

Quote
.486
.model flat, stdcall
option casemap :none

include     \masm32\include\windows.inc
include     \masm32\macros\macros.asm

include     \masm32\include\kernel32.inc
include     \masm32\include\user32.inc

includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib


.data
   Yes     db "Yes",0
   Compare db "Yes",0
   No      db "No",0

.code
   start:
      mov   eax, offset Yes
      mov   ebx, offset Compare
            call  StrCmp
            jne   no
           
   yes:
      invoke    MessageBox, 0, addr Yes, addr Yes, MB_OK
      jmp done
   no:
      invoke    MessageBox, 0, addr No, addr No, MB_OK
   done:

            invoke  ExitProcess, 0
;-----------------------------------
StrCmp      proc

next:       movzx   ecx, byte ptr [eax]
            movzx   edx, byte ptr [ebx]
           
            cmp     ecx, edx
            jne     short _exit
           
            add     eax, 1
            add     ebx, 1
           
            cmp     ecx, 0
            jne     next
                               
_exit:      ret
StrCmp      endp
;-----------------------------------
end start
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 07:02:05 PM
what do you have it named as ?
perhaps there is a batch file named the same or something - lol
BAT files execute before EXE's
Title: Re: CMP Problem?
Post by: RuiLoureiro on November 22, 2010, 07:08:05 PM
Quote from: dedndave on November 22, 2010, 07:02:05 PM
what do you have it named as ?
perhaps there is a batch file named the same or something - lol
BAT files execute before EXE's
              i call it Dragon2, 3, 4... I havenot BATs there. It is strange !
The same file Dragon2.asm in QEditor in that folder doesnt work
in the new folder Dragon it runs ok
              I got the error: rsrc files. If i delete rsrc.obj it assembles and runs correctly
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 10:03:00 PM
 :bg
Title: Re: CMP Problem?
Post by: HiddenDragon on November 22, 2010, 11:16:54 PM
Thanks a lot guys. Was really wondering why they weren't comparing strings and got that solved. Thanks ag

EDIT:

What does

mov     ecx,sizeof Yes

do?

Also,

repz    cmpsb

will compare the byte at esi and the byte at edi while zero?
Title: Re: CMP Problem?
Post by: dedndave on November 22, 2010, 11:57:26 PM
the "sizeof" operator returns the length in bytes of the data define named Yes
in this case, it is 4 - the length of "Yes" and the null terminator
comparing all 4 bytes insures that, not only are the first 3 bytes the same, but that the strings are the same length

REPZ CMPSB is a repeated string instruction that compares the byte at [ESI] with the byte at [EDI]
it then increments ESI (source index) and EDI (destination index), and also decrements ECX (count register) if REP is used
REPZ makes the instruction repeat as long as the result is zero, and the count is non-zero
in other words, it will stop at the first comparison inequality, or until ECX becomes zero
when done, the zero flag will be set if the strings are equal

here is a good reference by Randy Hyde - in particular, chapter 6 - it covers the string instructions

http://www.arl.wustl.edu/~lockwood/class/cs306/books/artofasm/toc.html
Title: Re: CMP Problem?
Post by: HiddenDragon on November 23, 2010, 01:10:40 AM
Thanks. That takes care of all my questions on that.