News:

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

CMP Problem?

Started by HiddenDragon, November 22, 2010, 04:27:52 PM

Previous topic - Next topic

HiddenDragon

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

brethren

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

clive

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.
It could be a random act of randomness. Those happen a lot as well.

dedndave

        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

RuiLoureiro

dedndave,
                Does it work ? Do you get the MessageBox ?
                But all things seems to be correct

dedndave

i didn't try it, Rui   :P

RuiLoureiro

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

dedndave

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

dedndave

well - i don't see any include missing
perhaps you aren't linking it with /SUBSYSTEM:WINDOWS

RuiLoureiro

Ok i used Console assemble link or assemble&link , the MessageBox doesnt open
It runs and close

RuiLoureiro

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

dedndave

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

RuiLoureiro

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

dedndave


HiddenDragon

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?