-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.asm
511 lines (462 loc) · 8.52 KB
/
snake.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
;Tohar Mualem
IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; Your variables here
;Key
KeyPressed db 0 ;1-Yes,0-No
;Border Parameter
BorderX dw 310
BorderY dw 190
;Check If Lost
LostParameter db 0 ;0=NotLost, 1=Lost
;CheckOneSec
Clock equ es:6Ch
;Snake
XArray dw 540 dup (0)
YArray dw 540 dup (0)
StartX dw 160
StartY dw 100
SituationOfStartSnake db 2;1-right 2-up 3-left 4-down
NumOfSquares dw 1
a db 1
EndSnakeX dw 0
EndSnakeY dw 0
;Apple
AppleX dw 10
AppleY dw 10
AmIScored db 0;1=Score,0=No Score
FreeXArray dw 30 dup (0)
FreeYArray dw 18 dup (0)
FreeRowX db 0
FreeLineY db 0
FreeX dw 10
FreeY dw 10
;Print Square
SquareX dw 0
SquareY dw 0
SquareColor db 4
CountSquareX db 0
CountSquareY db 0
; --------------------------
CODESEG
proc PrintSquare
push [SquareY]
PrintTheLineXTimes:
push [SquareX]
PrintLine:
mov bh,0h
mov cx,[SquareX]
mov dx,[SquareY]
mov al,[SquareColor]
mov ah,0ch
int 10h
add [CountSquareX],1
add [SquareX],1
cmp [CountSquareX],10
jne PrintLine
mov [CountSquareX],0
pop [SquareX]
add [CountSquareY],1
add [SquareY],1
cmp [CountSquareY],10
jne PrintTheLineXTimes
mov [CountSquareY],0
pop [SquareY]
ret
endp PrintSquare
proc PrintApple
YLoop:
mov bh,0h
mov cx,[FreeX]
mov dx,[FreeY]
mov ah,0Dh
int 10h
cmp al,0
jne NotFree
xor ax,ax
mov ax,2
mul [FreeRowX]
mov di,ax
mov bx,offset FreeXArray
mov ax,[FreeX]
mov [bx+di],ax
inc [FreeRowX]
jmp TheRowIsFree
NotFree:
add [FreeY],10
cmp [FreeY],190
jne YLoop
mov [FreeY],10
add [FreeX],10
cmp [FreeX],310
jne YLoop
TheRowIsFree:
mov [FreeY],10
add [FreeX],10
cmp [FreeX],310
jl YLoop
dec [FreeRowX]
mov ax, 40h
mov es, ax
mov bx,0
mov ax, [Clock]
mov ah, [byte cs:bx]
xor al, ah
and al, [FreeRowX]
xor ah,ah
xor bx,bx
mov bx,2
mul bx
mov di,ax
mov bx,offset FreeXArray
mov ax,[bx+di]
mov [FreeX],ax
mov [AppleX],ax
CheckFreeY:
mov bh,0h
mov cx,[FreeX]
mov dx,[FreeY]
mov ah,0Dh
int 10h
cmp al,0
jne NotFree1
xor ax,ax
mov ax,2
mul [FreeLineY]
mov di,ax
mov bx,offset FreeYArray
mov ax,[FreeY]
mov [bx+di],ax
inc [FreeLineY]
NotFree1:
add [FreeY],10
cmp [FreeY],190
jne CheckFreeY
dec [FreeLineY]
mov ax, 40h
mov es, ax
mov bx,0
mov ax, [Clock]
mov ah, [byte cs:bx]
xor al, ah
and al, [FreeLineY]
xor ah,ah
xor bx,bx
mov bx,2
mul bx
mov di,ax
mov bx,offset FreeYArray
mov ax,[bx+di]
mov [AppleY],ax
mov ax,[AppleX]
mov [SquareX],ax
mov ax,[AppleY]
mov [SquareY],ax
mov [SquareColor],5
call PrintSquare
mov [SquareColor],4
mov [FreeLineY],0
mov [FreeRowX],0
mov [FreeX],10
mov [FreeY],10
ret
endp PrintApple
proc Iscored
mov bh,0h
mov cx,[StartX]
mov dx,[StartY]
mov ah,0Dh
int 10h
cmp al,5
jne YouDidntScore
mov [AmIScored],1
YouDidntScore:
ret
endp Iscored
proc PrintBorders
mov [SquareColor],14
UpAndDownBorder:
mov [SquareY],0
mov ax,[BorderX]
mov [SquareX],ax
call PrintSquare
mov [SquareY],190
call PrintSquare
sub [BorderX],10
cmp [BorderX],0
jge UpAndDownBorder
RightAndLeftBorder:
mov [SquareX],0
mov ax,[BorderY]
mov [SquareY],ax
call PrintSquare
mov [SquareX],310
call PrintSquare
sub [BorderY],10
cmp [BorderY],0
jge RightAndLeftBorder
mov [SquareColor],4
ret
endp PrintBorders
proc CheckIfILostBySnake
mov bh,0h
mov cx,[StartX]
mov dx,[StartY]
mov ah,0Dh
int 10h
cmp al,4
jne NotHitYourself
mov [LostParameter],1
NotHitYourself:
ret
endp CheckIfILostBySnake
proc CheckIfILostByBorder
cmp [StartX],310
jne LostByBorderX1
mov [LostParameter],1
LostByBorderX1:
cmp [StartX],0
jne LostByBorderX2
mov [LostParameter],1
LostByBorderX2:
cmp [StartY],190
jne LostByBorderY1
mov [LostParameter],1
LostByBorderY1:
cmp [StartY],0
jne LostByBorderY2
mov [LostParameter],1
LostByBorderY2:
ret
endp CheckIfILostByBorder
proc AddSquare
xor ax,ax
mov ax,2
mul [NumOfSquares]
mov di,ax
mov bx,offset XArray
mov ax,[EndSnakeX]
mov [word ptr bx+di],ax
mov [SquareX],ax
mov bx,offset YArray
mov ax,[EndSnakeY]
mov [word ptr bx+di],ax
mov [SquareY],ax
call PrintSquare
inc [NumOfSquares]
ret
endp AddSquare
proc MoveSnake
xor ax,ax
mov ax,2
mul [NumOfSquares]
mov di,ax
mov [SquareColor],0
mov bx,offset XArray
mov ax,[word ptr bx+di-2]
mov [EndSnakeX],ax
mov [SquareX],ax
mov bx,offset YArray
mov ax,[word ptr bx+di-2]
mov [EndSnakeY],ax
mov [SquareY],ax
call PrintSquare
mov [SquareColor],4
cmp [SituationOfStartSnake],1
jne NotRightStart1
add [StartX],10
NotRightStart1:
cmp [SituationOfStartSnake],2
jne NotUpStart1
sub [StartY],10
NotUpStart1:
cmp [SituationOfStartSnake],3
jne NotLeftStart1
sub [StartX],10
NotLeftStart1:
cmp [SituationOfStartSnake],4
jne NotDownStart1
add [StartY],10
NotDownStart1:
xor ax,ax
mov ax,2
mul [NumOfSquares]
mov di,ax
mov bx,offset XArray
loopla1:
mov ax,[word ptr bx+di-4]
mov [word ptr bx+di-2],ax
sub di,2
cmp di,2
jne loopla1
xor ax,ax
mov ax,2
mul [NumOfSquares]
mov di,ax
mov bx,offset YArray
looplb1:
mov ax,[word ptr bx+di-4]
mov [word ptr bx+di-2],ax
sub di,2
cmp di,2
jne looplb1
call CheckIfILostByBorder
call CheckIfILostBySnake
call Iscored
mov bx,offset XArray
mov ax,[StartX]
mov [bx],ax
mov bx,offset YArray
mov ax,[StartY]
mov [bx],ax
mov ax,[StartX]
mov [SquareX],ax
mov ax,[StartY]
mov [SquareY],ax
call PrintSquare
ret
endp MoveSnake
proc Key
in al, 64h ; Read keyboard status port
cmp al, 10b ; Data in buffer ?
je NoKeyPressed ; Wait until data available
in al, 60h ; Get keyboard data
;Check If Up
cmp al, 11h
jne Up
cmp [SituationOfStartSnake],4
je YouCantUp
cmp [SituationOfStartSnake],2
je YouCantUp
mov [KeyPressed],1
mov [SituationOfStartSnake],2
Up:
YouCantUp:
;Check If Left
cmp al,1Eh
jne Left
cmp [SituationOfStartSnake],1
je YouCantLeft
cmp [SituationOfStartSnake],3
je YouCantLeft
mov [KeyPressed],1
mov [SituationOfStartSnake],3
Left:
YouCantLeft:
;Check If Down
cmp al,1Fh
jne Down
cmp [SituationOfStartSnake],2
je YouCantDown
cmp [SituationOfStartSnake],4
je YouCantDown
mov [KeyPressed],1
mov [SituationOfStartSnake],4
Down:
YouCantDown:
;Check If Right
cmp al,20h
jne Right
cmp [SituationOfStartSnake],3
je YouCantRight
cmp [SituationOfStartSnake],1
je YouCantRight
mov [KeyPressed],1
mov [SituationOfStartSnake],1
Right:
YouCantRight:
NoKeyPressed:
mov ah,0Ch
int 21h
ret
endp Key
start:
mov ax, @data
mov ds, ax
; --------------------------
; Your code here
mov ax, 13h
int 10h
call PrintBorders
mov bx,offset XArray
mov ax,[StartX]
mov [bx],ax
mov bx,offset YArray
mov ax,[StartY]
mov [bx],ax
mov ax,[StartX]
mov [SquareX],ax
mov ax,[StartY]
mov [SquareY],ax
call PrintSquare
mov ax,[StartX]
mov [EndSnakeX],ax
mov ax,[StartY]
mov [EndSnakeY],ax
cmp [SituationOfStartSnake],1
jne NotRightStart3
add [EndSnakeX],10
NotRightStart3:
cmp [SituationOfStartSnake],2
jne NotUpStart3
sub [EndSnakeY],10
NotUpStart3:
cmp [SituationOfStartSnake],3
jne NotLeftStart3
sub [EndSnakeX],10
NotLeftStart3:
cmp [SituationOfStartSnake],4
jne NotDownStart3
add [EndSnakeY],10
NotDownStart3:
call AddSquare
call AddSquare
call AddSquare
call PrintApple
loopgame:
mov ax, 40h
mov es, ax
mov ax, [Clock]
FirstTick:
cmp ax, [Clock]
je FirstTick
mov cx, 1
DelayLoop:
mov ax, [Clock]
Tick:
cmp ax, [Clock]
je Tick
call Key
cmp [KeyPressed],1
jne NoKey1
cmp [LostParameter],1
je exit
cmp [AmIScored],1
jne NO1
call AddSquare
call PrintApple
mov [AmIScored],0
NO1:
NoKey1:
mov [KeyPressed],0
loop DelayLoop
call MoveSnake
cmp [LostParameter],1
je exit
cmp [AmIScored],1
jne NO2
call AddSquare
call PrintApple
mov [AmIScored],0
NO2:
mov cx,1
cmp cx,0
jne loopgame
; --------------------------
exit:
mov ax, 4c00h
int 21h
END start