Search code examples
assemblyx86-16tasm

Assembly - cmp instruction always passes


I am making A Tic-Tac-Toe game and when making the function to check if a player won. For testing purposes, I made the program simply exit when A player won. Just for starting I made the program check only the 1st row:

0, 0, 0 ==> this row
0, 0, 0
0, 0, 0

I ran into a problem that the program exists every time I change A position to either An X or O, except when I choose one of the positions that are checked by the program (so, if I choose any position in the 1st row).
The code of the function is at the end

I don't want to include the whole code because I'm not just printing it to the screen but drawing it in graphics mode (ah=13h int 10h) I will just show you the main loop and checkIfWon function that I filtered for only important stuff. This is the code: the board definition:

.data
board   db 0,0,0
        db 0,0,0
        db 0,0,0

CheckIfWon function:

checkIfWon proc
    ; check rows
    ; Equal to: if (board[0] == board[1] && board[1] == board[2])
    movsx   eax, byte ptr [board]
    movsx   ecx, byte ptr [board+1]
    cmp     eax, ecx
    jne     cont

    movsx   eax, byte ptr [board]
    movsx   ecx, byte ptr [board+2]
    cmp     eax, ecx
    jne     cont

    mov ax, 4c00h
    int 21h

    cont:
    ret  

ret
endp checkIfWon
.386
; mainloop
mainloop:
    ; check for exit button (ESC)...
    ; get mouse button and cursor position...
    ;;;;;;;;;;;;;;;;;;;;;;; Check if mouse is on one of the positions 

        box0:
        ; check if it got clicked...
        drawXplayer0:
        ; draw X
        jmp boxHitten
        drawOplayer0:
        ; draw o

        jmp boxHitten

    box1:
        ; check if it got clicked...
        drawXplayer1:
            ; draw X
            jmp boxHitten
        drawOplayer1:
            ; draw o

        jmp boxHitten

    box2:
        ; check if it got clicked...
        drawXplayer2:
            ; draw X
            jmp boxHitten
        drawOplayer2:
            ; draw o

        jmp boxHitten

    box3:
        ; check if it got clicked...
        drawXplayer3:
            ; draw X
            jmp boxHitten
        drawOplayer3:
            ; draw o

        jmp boxHitten

    box4:
        ; check if it got clicked...
        drawXplayer4:
            ; draw X
            jmp boxHitten
        drawOplayer4:
            ; draw o

        jmp boxHitten

    box5:
        ; check if it got clicked...
        drawXplayer5:
            ; draw X
            jmp boxHitten
        drawOplayer5:
            ; draw o

        jmp boxHitten

    box6:
        ; check if it got clicked...
        drawXplayer6:
            ; draw X
            jmp boxHitten
        drawOplayer6:
            ; draw o

        jmp boxHitten

    box7:
        ; check if it got clicked...
        drawXplayer7:
            ; draw X
            jmp boxHitten
        drawOplayer7:
            ; draw o

        jmp boxHitten

    box8:
        ; check if it got clicked...
        drawXplayer8:
            ; draw X
            jmp boxHitten
        drawOplayer8:
            ; draw o

        jmp boxHitten

    boxHitten:  
        ; Fix overdrawn borders
        call drawBoard

        ; set cursor position 
        mov  dl, 1
        mov  dh, 1
        mov  bh, 0    ;Display page     
        mov  ah, 02h  ;SetCursorPosition
        int  10h
        ;
        ; change player
        ; if player='x': player='O' else player=x'

        ; here im playing a sound that tells the user he clicked


        ; delay
        push 500 ; 0.5 secs
        call delay
        call checkIfWon
        jmp mainloop

    takenError:
        push 14000 ; frequency for bad answer
        push 200       ; duration milliseconds
        call play

        ; delay
        push 1000
        call delay
        jmp mainloop




The program doesn't exit (like I wanted): example Exists (not what I wanted): notwhatiwanted

What could be the reason that the program skips it?

EDIT:
I found the solution, see comment


Solution

  • I figured out the solution to my problem:
    Thing is, since I defined board to be

    board db 0,0,0
              db 0,0,0
              db 0,0,0

    The check simply went trough every time, since they are both 0.
    And when I click something in the position, the board changes to

    board X,0,0
               0,0,0
               0,0,0

    Then it doesn't pass the check.
    solution code:

    checkIfWon proc
         ; check rows
         ; Equal to: if (board[0] != 0 && board[0] == board[1] && board[1] == board[2])
         movsx   eax, byte ptr [board]
         movsx   ecx, byte ptr [board+1]
         cmp     eax, 0
         je cont
         cmp     eax, ecx
         jne     cont
    
         movsx   eax, byte ptr [board]
         movsx   ecx, byte ptr [board+2]
         cmp     eax, ecx
         jne     cont
    
         mov ax, 4c00h
         int 21h
    
    cont:
        ret  
    
    ret
    endp checkIfWon