Results 1 to 6 of 6

Thread: cmp help

  1. #1
    Guest

    Post

    Any ideas why this wont work? Im compilng with A86.

    Code:
    START:
            mov     ds, cs
            jmp     MAIN
    
    Message1        db      13, 10, "Password    [pass.exe]", 13, 10
                    db      "Copyright (C) 2000 Hero Inc. All rights reserved.", 13, 10, "$"
    Prompt          db      13, 10, "Password: $"
    Password        db      "hello$"
    Buffer          db      "$"
    
    MAIN:
            ;Clear
            call    ClearScreen
    
            ;Print Message1
            mov     dx, offset Message1
            mov     ah, 9
            int     21h
    
            ;Print Prompt
            mov     dx, offset Prompt
            mov     ah, 9
            int     21h
    
            ;Input string
            mov     ah, 3fh
            xor     bx, bx
            mov     cx, 20h
            mov     dx, offset Buffer
            int     21h
    
            mov     ax, offset Buffer
            cmp     ax, offset Password
    
            je      EXIT
            jmp     MAIN
    
    EXIT:
            call    ClearScreen
            mov     ax, 4c00h
            int     21h
    
    ClearScreen     proc
            mov     ax, 0003h
            int     10h
            ret
    ClearScreen     endp
    
    END START
    I think it has something to do with these lines...

    Code:
    mov     ax, offset Buffer
    cmp     ax, offset Password

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I think it's a bit ambitious to try to compare 2 strings with a single cmp line. all cmp does is subtract the 2 values and save the reruld to a register (can't remember which one, i think it's dx or it might be a special 2 bit register that just rempmbers the sign) then the jump commands go on whether this it +ve, -ve or 0.

    I havn't got to the offset comand in my quest to learn ASM but i think you're just comparing the pointers to the strings, which will never be equal. either write a strcmp macro or a strcmp sub.

    hope it helps

  3. #3
    Guest
    Any code ideas?

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    err, you want pointers to the 2 strings in 2 registers say ebx and ecx then go into a loop, compare the values of the 2 bytes pointed to by ebx and ecx if they are not equal put a value of 0 (false) into eax, if they are equal check if one of them is 0, if it is put -1 (true) into eax and return, if not increment ebx and eax and go back to the start of the loop.

    Unfortunatley I'm not quite at the stage where I can code that correctly, so I'll code it incorectly instead


    Code:
    StringCompare proc
    
        pop ebx    ;pops 2 parameters off the stack, 
        pop ecx    ;these should be pointers to char arrays 
    
    strt:    ;start of loop
    
            cmp [ebx],[ecx]  ;compare the bytes pointed to by
                             ;ebx and ecx, this syntax is
                             ;probably vey wrong so change it
                             ;remember you only want to compare 
                             ;single bytes, not words or dwords
    
            jne RetFalse  ;if they are not the same return false
    
            cmp [ebx], 0    ;compare the byte pointed to by ebx
                            ;to 0 (syntax will be wrong again)
    
    jne strt        ;if it's not a termination character 
                    ;go back to the start
    
    ;end of loop
    
    ;both strings terminated so return true
    mov eax, -1
    ret
    
    
    RetFalse:
    ;we've found 2 different characters so return false   
        mov eax, 0
        ret
    
    StringCompare endp
    if you code that correctly it should work.

  5. #5
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    I was just wondering, maybe if you XOR them then the first value in the XOR would be zero, and then put a jz.

    But that wouldn't be case sensitive, so you could convert it to lower case by anding each char with 6F (0110 1111).

    Okay, I'm probably speaking rubbish, but the intention's good.
    Courgettes.

  6. #6
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Code:
    mov ax, BYTE [Buffer]
    cmp ax, BYTE [Password]
    I am not very good with A86, so I might be a little off. I am using the NASM syntax though

    If you are comparing the values at Buffer and Password, then this would be your code.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width