汇编 - cmp 指令总是通过
Assembly - cmp instruction always passes
我正在制作井字棋游戏,并且在制作检查玩家是否获胜的功能时。
出于测试目的,我让程序在玩家获胜时退出。
只是为了开始,我让程序只检查第一行:
0, 0, 0 ==> this row
0, 0, 0
0, 0, 0
我运行遇到一个问题,每次我将A位置更改为An X或O时程序都存在,除了当我选择其中一个位置时由程序检查(因此,如果我选择第一行中的任何位置)。
函数的代码在最后
我不想包含整个代码,因为我不只是将它打印到屏幕上,而是在图形模式下绘制它 (ah=13h int 10h)
我将只向您展示主循环和 checkIfWon 函数,我只过滤了重要的东西。
这是代码:
董事会定义:
.data
board db 0,0,0
db 0,0,0
db 0,0,0
CheckIfWon 函数:
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
程序没有退出(如我所愿):
存在(不是我想要的):
程序跳过它的原因可能是什么?
编辑:
我找到了解决方案,请参阅评论
我找到了解决问题的方法:
问题是,因为我将 board 定义为
board db 0,0,0
db 0,0,0
db 0,0,0
支票每次都通过,因为它们都是 0。
当我点击位置上的东西时,棋盘变为
board X,0,0
0,0,0
0,0,0
那就没通过
解决方案代码:
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
我正在制作井字棋游戏,并且在制作检查玩家是否获胜的功能时。 出于测试目的,我让程序在玩家获胜时退出。 只是为了开始,我让程序只检查第一行:
0, 0, 0 ==> this row
0, 0, 0
0, 0, 0
我运行遇到一个问题,每次我将A位置更改为An X或O时程序都存在,除了当我选择其中一个位置时由程序检查(因此,如果我选择第一行中的任何位置)。
函数的代码在最后
我不想包含整个代码,因为我不只是将它打印到屏幕上,而是在图形模式下绘制它 (ah=13h int 10h) 我将只向您展示主循环和 checkIfWon 函数,我只过滤了重要的东西。 这是代码: 董事会定义:
.data
board db 0,0,0
db 0,0,0
db 0,0,0
CheckIfWon 函数:
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
程序没有退出(如我所愿):
程序跳过它的原因可能是什么?
编辑:
我找到了解决方案,请参阅评论
我找到了解决问题的方法:
问题是,因为我将 board 定义为
board db 0,0,0
db 0,0,0
db 0,0,0
支票每次都通过,因为它们都是 0。
当我点击位置上的东西时,棋盘变为
board X,0,0
0,0,0
0,0,0
那就没通过
解决方案代码:
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