-
Notifications
You must be signed in to change notification settings - Fork 0
/
5InRowGame.asm
546 lines (479 loc) · 9.89 KB
/
5InRowGame.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
;Mohamed Aref Agbaria 314720293
INCLUDE Irvine32.inc
include 5InRowGameData.inc
.data
myName BYTE "name: Mohamed Aref Agbaria Id: 314720293",13,10,0
WelcomeMsg BYTE " Welcome to the world of Order & Chaos :",13,10,0
PlayerWord BYTE" Player ",0
ReadCoordMsg2 BYTE" Enter row number : ",0
ReadCoordMsg3 BYTE"Enter column number : ",0
ErorMsg BYTE"***Eror, try again",13,10,0
ReadXOMsg BYTE "Choose piece (X or O only) : ",0
WinMsg BYTE" is the WINNER !!",13,10,0
DrawMsg BYTE"The game finished in DRAW",13,10,0
Row DWORD 0
Column DWORD 0
DiagonalOffset DWORD 0
ContinueGame Dword 4
SequenceLength Dword 5
.code
myMain PROC
call PreGameStarting
checkGameStatus:
cmp eax,ContinueGame
jne gameFinished
push offset turn
push offset Board
call next_step
jmp checkGameStatus
gameFinished:
call printTheResult
call ReadChar
call exitProcess
myMain ENDP
next_step PROC
BOARDADDRESS = 8
PLAYERTURN = BOARDADDRESS + 4
GAMEFINISHEDINDRAW = 3
BOARDHAVEEMPTYLOC = 1
push ebp
mov ebp, esp
push [ebp+BOARDADDRESS]
call PrintBoard
push [ebp+BOARDADDRESS]
call CheckEMptyLoc
cmp ebx,BOARDHAVEEMPTYLOC
jne NoEMptyLoc
push [ebp+PLAYERTURN]
call Read_coord
mov esi, eax ; save eax value place
call Read_XO
mov bx,ax ; save the piece
push ax ;Piece
push esi; xy Location
push [ebp+BOARDADDRESS]; board addres
call SetXY
cmp eax,1 ; update player turn 1->2 2->1
jne skip
mov esi,[ebp+PLAYERTURN]
cmp word ptr[esi],1
je incTurn
dec word ptr[esi]
dec word ptr[esi]
incTurn:
inc word ptr[esi]
push [ebp+BOARDADDRESS]; board addres
call Check4Win
cmp eax,1
jne skip
push [ebp+PLAYERTURN]
call WhoTheWinner
jmp skip2
jmp skip
NoEMptyLoc:
mov eax,GAMEFINISHEDINDRAW
jmp skip2
skip: ;skip := called if there is no winner and we want to update eax
mov eax,ContinueGame
skip2: ;skip2 := called if there is a winner and we donot want to update eax
mov esp,ebp
pop ebp
ret 6
next_step ENDP
printTheResult PROC
push ebp
mov ebp, esp
cmp eax,1
je Player1Win
cmp eax,2
je Player2Win
jmp Draw
Player1Win:
mov edx,offset PlayerWord
call WriteString
call WriteDec
mov edx, offset WinMsg
call WriteString
jmp skip
Player2Win:
mov edx,offset PlayerWord
call WriteString
call WriteDec
mov edx, offset WinMsg
call WriteString
jmp skip
Draw:
mov edx,offset DrawMsg
call WriteString
skip:
mov esp,ebp
pop ebp
ret
printTheResult ENDP
WhoTheWinner PROC
; return in eax which player is the winner (1/2)
PLAYERTURN = 8
push ebp
mov ebp,esp
mov esi,[ebp+PLAYERTURN] ; esi store player turn offset
cmp word ptr[esi],1
je player2Win
jmp skip
player2Win:
mov eax,2
skip:
mov esp,ebp
pop ebp
ret 4
WhoTheWinner ENDP
Check4Win Proc
BOARDADDRESS = 8
push ebp
mov ebp,esp
push [ebp+BOARDADDRESS]
call Check4WinRows
push [ebp+BOARDADDRESS]
call Check4WinColumns
push [ebp+BOARDADDRESS]
call Check4WinDiagonal
cmp eax,1
jne NoWinner
push [ebp+BOARDADDRESS]
call PrintBoard
jmp skip
NoWinner:
mov eax,ContinueGame
skip:
mov esp,ebp
pop ebp
ret 4
Check4Win ENDP
Check4WinDiagonal PROC
BOARDADDRESS = 8
push ebp
mov ebp,esp
cmp eax,1
je skip
mov edi,[ebp+BOARDADDRESS]; edi store board adress
mov Row,1 ; edx store row number
mov Column,1
mov ecx,N
mov esi,0 ; index to travel on the board
sub ecx,SequenceLength
inc ecx ; now ecx store how many rows to check (from where start the diagonal elements)
SearchDiagonalOuterLoop:
push ecx
mov ecx,N
mov Column,1
SearchDiagonalInnerLoop:
mov edx,N
sub edx,Column
inc edx
cmp edx,SequenceLength ; (N-Column+1>=SequenceLength)
JL LeftSearch
mov DiagonalOffset,N
inc DiagonalOffset
push [ebp+BOARDADDRESS] ; Board Addres
Call CheckSpecificDiagonal
LeftSearch:
mov edx,Column
cmp edx,SequenceLength ;(Column>=SequenceLength)
JL PieceNotInDiagonal
mov DiagonalOffset,N
dec DiagonalOffset
push [ebp+BOARDADDRESS] ; Board Addres
Call CheckSpecificDiagonal
cmp eax,1
JE exitLoop
PieceNotInDiagonal:
inc esi
inc Column
Loop SearchDiagonalInnerLoop
pop ecx
loop SearchDiagonalOuterLoop
jmp skip
exitLoop:
pop ecx
skip:
mov esp,ebp
pop ebp
ret 4
Check4WinDiagonal ENDP
CheckSpecificDiagonal PROC USES esi ecx edi
BOARDADDRESS = 20
push ebp
mov ebp,esp
mov ecx,SequenceLength
mov edi,[ebp+BOARDADDRESS]; edi store board adress
CheckDiagonalLoop:
cmp [edi+esi],bl
jne skip
add esi,DiagonalOffset
Loop CheckDiagonalLoop
mov eax,1
jmp skip
skip:
mov esp,ebp
pop ebp
ret 4
CheckSpecificDiagonal ENDP
Check4WinColumns PROC
BOARDADDRESS = 8
push ebp
mov ebp,esp
cmp eax,1
je skip
mov edx,0 ; edx store the length of sequence of same piece
mov edi,[ebp+BOARDADDRESS]; edi store board adress
mov eax,0 ; eax store game status
mov ecx,N; loop iterations number
mov esi,0 ; index to travel on the board
OuterLoopCheckColumns:
push ecx
mov ecx,N
InnerLoopCheckColumns:
cmp [edi+esi],bl ; bl store the last piece that set on the board
jne DonotCount
inc edx
jmp ContinueInnerLoopCheckColumns
DonotCount:
cmp edx,0
jbe ContinueInnerLoopCheckColumns
cmp edx,SequenceLength
jne DecraseSequenceCounter
jmp ContinueInnerLoopCheckColumns
DecraseSequenceCounter:
dec edx
ContinueInnerLoopCheckColumns:
add esi,N
Loop InnerLoopCheckColumns
cmp edx,SequenceLength
jne NoWinner
mov eax,1
jmp exitLoop
NoWinner:
mov edx,0
inc edi
mov esi,0
pop ecx
Loop OuterLoopCheckColumns
exitLoop:
skip:
mov esp,ebp
pop ebp
ret 4
Check4WinColumns ENDP
Check4WinRows PROC
BOARDADDRESS = 8
push ebp
mov ebp,esp
mov edx,0 ; edx store the length of sequence of same piece
mov edi,[ebp+BOARDADDRESS]; edi store board adress
mov eax,0 ; eax store game status
mov ecx,N; loop iterations number
mov esi,0 ; index to travel on the board
OuterLoopCheckRows:
push ecx
mov ecx,N
InnerLoopCheckRows:
cmp [edi+esi],bl ; bl store the last piece that set on the board
jne DonotCount
inc edx
jmp ContinueInnerLoopCheckRows
DonotCount:
cmp edx,0
jbe ContinueInnerLoopCheckRows
cmp edx,SequenceLength ; to change
jne DecraseSequenceCounter
jmp ContinueInnerLoopCheckRows
DecraseSequenceCounter:
dec edx
ContinueInnerLoopCheckRows:
inc esi
Loop InnerLoopCheckRows
cmp edx,SequenceLength
jne NoWinner
mov eax,1
jmp exitLoop
NoWinner:
mov edx,0
pop ecx
Loop OuterLoopCheckRows
exitLoop:
mov esp,ebp
pop ebp
ret 4
Check4WinRows ENDP
SetXY PROC
;return in eax 1 if succesfully set the piece in the place
BOARDADDRESS = 8
SETPLACE = BOARDADDRESS + 4
PIECE = SETPLACE + 4
SUCCESFULLYSETPIECE = 1
push ebp
mov ebp,esp
mov edi, [ebp+BOARDADDRESS] ;edi store board addres
mov esi, [ebp+SETPLACE] ;esi store the index (place in the Board) where to set the piece
mov dx, [ebp+PIECE] ; dx store the piece we want to set (X/O)
cmp BYTE PTR[edi+esi],'-'
je setPiece
mov edx, offset ErorMsg
call WriteString
jmp skip
setPiece:
mov [edi+esi],dl
mov eax,SUCCESFULLYSETPIECE
skip:
mov esp,ebp
pop ebp
ret 10
SetXY ENDP
Read_XO PROC
push ebp
mov ebp,esp
pieceInput:
mov eax,0 ; clean eax to get char from user
mov edx,offset ReadXOMsg
call WriteString
call ReadChar
call WriteChar
call Crlf
cmp al,'O'
je validPiece
cmp al, 'X'
je validPiece
mov edx,offset ErorMsg
call WriteString
jmp pieceInput
validPiece:
mov esp,ebp
pop ebp
ret
Read_XO ENDP
Read_coord PROC uses ebx edx
;return in eax the place in the board
PLAYERTURN = 16
push ebp
mov ebp,esp
rowInput:
mov eax,0
mov edx ,offset PlayerWord
call WriteString
mov eax, [ebp+PLAYERTURN] ; eax store player turn offset
movzx eax,Word ptr[eax]
call WriteDec
mov edx ,offset ReadCoordMsg2
call WriteString
call ReadInt
cmp eax,N
jG badRowInput
cmp eax,0
jG checkColumn
badRowInput:
mov edx,offset ErorMsg
call WriteString
jmp rowInput
checkColumn:
dec eax
mov ebx,N
mul ebx
mov ebx,eax ; ebx store row place in the board
columnInput:
mov edx ,offset ReadCoordMsg3
call WriteString
call ReadInt
cmp eax,N
JG badColumnInput
cmp eax,0
jG validInput
badColumnInput:
mov edx,offset ErorMsg
call WriteString
jmp columnInput
validInput:
dec eax
add eax,ebx
mov esp,ebp
pop ebp
ret 4
Read_coord ENDP
CheckEMptyLoc PROC uses ecx edx edi esi
; return ebx = 1 if the board has empty place otherwise ebx = 0
BOARDADDRESS = 24
THEREISANEMPTYPLACE = 1
push ebp
mov ebp, esp
mov ecx,NN ; ecx store board length
mov edi,[ebp+BOARDADDRESS] ; edi store board addres
mov esi,0 ; esi index to travel on the board
mov ebx,0 ; ebx store the return value
EmptyPlace:
mov dl,[edi+esi]
cmp dl,'-'
jz YesEmpty
inc esi
loop EmptyPlace
jmp skip
YesEmpty:
mov ebx,THEREISANEMPTYPLACE
skip:
mov esp,ebp
pop ebp
ret 4
CheckEMptyLoc ENDP
PrintBoard PROC USES eax ecx edx edi esi
BOARDADDRESS = 28
push ebp
mov ebp, esp
mov ecx,N ; ecx is edge length
mov edi,[ebp+BOARDADDRESS] ; edi store board addres
mov esi,0 ; esi index to travel on the board
mov edx, 1 ; edx store a number of column and row (1-6) uses for printing
mov al, ' '
call WriteChar
call WriteChar
PrintRowHeader:;print 1 2 3 4 .........
mov eax, edx
call WriteDec
mov al, ' '
call WriteChar
inc edx
loop PrintRowHeader
call CRLF
mov edx, 1
mov ecx,N
OuterLoopPrintBoard:
mov eax, edx
call WriteDec
mov al, ' '
call WriteChar
push ecx
mov ecx,N
InnerLoopPrintBoard:
mov al,[edi+esi]
call WriteChar
mov al, ' '
call WriteChar
inc esi
loop InnerLoopPrintBoard
Call CRLF
pop ecx
inc edx
loop OuterLoopPrintBoard
mov esp,ebp
pop ebp
ret 4
PrintBoard ENDP
PreGameStarting PROC
push ebp
mov ebp, esp
mov edx, offset myName
call WriteString ; print Name
mov edx, offset WelcomeMsg
call WriteString ; print WelcomeMsg
mov eax,ContinueGame ; set eax to start the game
mov esp,ebp
pop ebp
ret
PreGameStarting ENDP
END myMain