TIC TAC TOE in Assembly Language

831 views Asked by At

I am trying to make TIC TAC TOE in Assembly Language. I'm having trouble in using cmp. I want that when the user chooses 1 it should jump to the label one and then on screen replace only the character '1' with 'X' but it is replacing all the nine numbers with 'X'.
Here's my code:

.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
1

There are 1 answers

0
Sep Roland On BEST ANSWER
mov ecx,9
L13:
call clrscr
call BOARD
call GAME
loop L13

This main loop will fail since the call to BOARD will modify the loop control variable in ECX. You need to preserve it.

mov ecx,9
L13:
push ecx         <<<<<<
call clrscr
call BOARD
call GAME
pop ecx          <<<<<<
loop L13

In the GAME procedure, you need to avoid that all the cases fall through in each other.

    ...
    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

Between these instructions you're missing a proper program exit or else you'd better write the ellipses (...) so we can know there's more code here that you didn't feel the need to include.

loop L13

...

BOARD proc

call readint
mov al,info

Usually readint would give a result in EAX. You then could make a copy of it, but that would mean writing mov info, al, the opposite of what you've written! Remember the destination is on the left side, the source is on the right side.