我正在尝试用汇编语言制作TIC TAC TOE。我在使用cmp
时遇到了麻烦。我希望当用户选择1时它应该跳转到标签 one ,然后在屏幕上只用'X'替换字符'1',但它用'X'替换所有9个数字。
这是我的代码:
.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.data
tic BYTE "TIC TAC TOE GAME"
player BYTE "PLAYER 1 (X) : PLAYER 2 (O)"
myarray1 BYTE '1' , ' ' , '|' , ' ' , '2' , ' ' , '|' ,' ' ,'3'
myarray2 BYTE '4' , ' ' , '|' , ' ' , '5' , ' ' , '|' ,' ' ,'6'
myarray3 BYTE '7', ' ' , '|' , ' ' , '8' , ' ' , '|' ,' ' ,'9'
position BYTE "Select You Position: ",0
choise BYTE 'X','O'
turn BYTE 0
info byte ?
.code
main PROC
mov bl,turn
mov ecx,9
L13:
call clrscr
call BOARD
call GAME
loop L13
BOARD proc
;TO DISPLAY TIC TAC TOE GAME
mov ecx, LENGTHOF tic
mov esi,Offset tic
L0:
mov al,[esi]
call writechar
inc esi
loop l0
call crlf
call crlf
;TO DISPLAY PLAYER INFO
mov ecx,LENGTHOF player
mov esi,OFFSET player
l6:
mov al,[esi]
call writechar
inc esi
loop L6
call crlf
call crlf
;LOOP TO DISPLAY FIRST ARRAY
mov ecx,LENGTHOF myarray1
mov esi,OFFSET myarray1
L1:
mov al,[esi]
call writechar
inc esi
loop L1
call crlf
;LOOP TO DISPLAY SECOND ARRAY
mov ecx,LENGTHOF myarray2
mov esi,OFFSET myarray2
L2:
mov al,[esi]
call writechar
inc esi
loop L2
call crlf
;LOOP TO DISPLAY THIRD ARRAY
mov ecx,LENGTHOF myarray3
mov esi,OFFSET myarray3
L3:
mov al,[esi]
call writechar
inc esi
loop L3
call crlf
call crlf
mov ecx,LENGTHOF position
mov esi,OFFSET position
L4:
mov al,[esi]
call writechar
inc esi
loop L4
call crlf
ret
BOARD endp
GAME proc
L60:
call readint
mov al,info
cmp al,1
je one
cmp al,2
je two
cmp al,3
je three
cmp al,4
je four
cmp al,5
je five
cmp al,6
je six
cmp al,7
je seven
cmp al,8
je eight
cmp al,9
je nine
one:
mov al,choise
xchg al,[myarray1]
two:
mov al,choise
xchg al,[myarray1+4]
three:
mov al,choise
xchg al,[myarray1+8]
four:
mov al,choise
xchg al,[myarray2]
five:
mov al,choise
xchg al,[myarray2+4]
six:
mov al,choise
xchg al,[myarray2+8]
seven:
mov al,choise
xchg al,[myarray3]
eight:
mov al,choise
xchg al,[myarray3+4]
nine:
mov al,choise
xchg al,[myarray3+8]
ret
GAME endp
exit
main endp
end main
答案 0 :(得分:0)
mov ecx,9
L13:
call clrscr
call BOARD
call GAME
loop L13
这个主循环将失败,因为对 BOARD 的调用将修改ECX
中的循环控制变量。你需要保留它。
mov ecx,9
L13:
push ecx <<<<<<
call clrscr
call BOARD
call GAME
pop ecx <<<<<<
loop L13
在 GAME 程序中,您需要避免所有情况都相互影响。
...
cmp al,9
je nine
RET <<<<<< What if choice was invalid ?
one:
mov al,choise
xchg al,[myarray1]
RET <<<<<<
two:
mov al,choise
xchg al,[myarray1+4]
RET <<<<<<
...
loop L13 BOARD proc
在这些说明之间,你错过了正确的程序退出,否则你最好写下省略号(...),这样我们就可以知道你这里有更多的代码了。觉得有必要包括。
loop L13
...
BOARD proc
call readint mov al,info
通常 readint 会在EAX
中给出结果。然后你可以复制它,但这意味着写mov info, al
,与你所写的相反!请记住目的地位于左侧,来源位于右侧。