-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.asm
374 lines (294 loc) · 8.73 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
STACK SEGMENT PARA STACK
DB 64 DUP (' ')
STACK ENDS
DATA SEGMENT PARA 'DATA'
TIME_AUX DB 0 ;var for time
BLOCK_SIZE DB 08h ;size of one block
PEBBLE_POS DW 00h ;position of pebble
SNAKE_LENGTH DW 01h ;length of snake
SNAKE_ARRAY DW 1001 DUP(0000h)
SNAKE_HEAD_X DW 00h
SNAKE_HEAD_Y DW 00h
HEAD_DIRECTION DB 0 ;0up,1down;2left;3right
DATA ENDS
CODE SEGMENT PARA 'CODE'
MAIN PROC FAR
ASSUME CS:CODE,DS:DATA,SS:STACK
PUSH DS ;push to stack DS segment
SUB AX,AX ;clean AX register
PUSH AX
MOV AX,DATA ;save on AX register the contents of DATA segment
MOV DS,AX ;save on the DS segments the contents of AX
POP AX ;release the top item of the stack to the AX register
POP AX ;release the top item of the stack to the AX register
CALL INIT
CALL CLEAR_SCREEN
CHECK_TIME:
MOV AH,2Ch ;get system time
INT 21h ;CH = hour, CL = minutes, DH = secnod DL = 1/100 sec
CMP DL,TIME_AUX
JE CHECK_TIME
MOV TIME_AUX,DL
CALL KEYBOARD_INPUT
CALL MOVE_SNAKE
CALL PEBBLE_CHECK
CALL SELF_COLLISION_CHECK
CALL CLEAR_SCREEN
CALL DRAW_PEBBLE
CALL DRAW_SNAKE
JMP CHECK_TIME
RET
MAIN ENDP
INIT PROC NEAR
MOV DI,00h
CALL GENERATE_PEBBLE
MOV AX,PEBBLE_POS
MOV SNAKE_ARRAY[DI],AX
CALL GENERATE_PEBBLE
RET
INIT ENDP
CLEAR_SCREEN PROC NEAR
MOV AH,00h ;video mode
MOV AL,0Dh ;320x200
INT 10h ;video mode interrupt
RET
CLEAR_SCREEN ENDP
GENERATE_PEBBLE PROC NEAR
;rand val init
XOR BX,BX
MOV AH,2Ch ;get system time
INT 21h ;CH = hour, CL = minutes, DH = secnod DL = 1/100 sec
XOR CX,DX
XOR DX,DX
;x position
MOV AX,CX
MOV BX,0028h
DIV BX
MOV DH,DL
PUSH DX
;y position
MOV AX,CX
XOR DX,DX
MOV BX,0019h
DIV BX
POP BX
MOV BL,DL
MOV PEBBLE_POS,BX
RET
GENERATE_PEBBLE ENDP
PEBBLE_CHECK PROC NEAR
XOR AX,AX
XOR BX,BX
MOV AX,SNAKE_ARRAY[0]
MOV BX,PEBBLE_POS
CMP AX,BX
JNE NO_PEBBLE_MATCH
MOV AX,SNAKE_LENGTH
INC AX
MOV SNAKE_LENGTH,AX
CALL GENERATE_PEBBLE
NO_PEBBLE_MATCH:
RET
PEBBLE_CHECK ENDP
SELF_COLLISION_CHECK PROC NEAR
MOV DI,SNAKE_LENGTH
CMP DI,01h
JE NO_CHECK_NEEDED
MOV BX,SNAKE_ARRAY[0]
XOR AX,AX
MOV AL,2
MUL DI
MOV DI,AX
COLLISION_CHECK_LOOP:
SUB DI,2
MOV AX,SNAKE_ARRAY[DI]
CMP AX,BX
JNE NO_COLLISION
CALL EXIT_NOW
NO_COLLISION:
CMP DI,02h
JG COLLISION_CHECK_LOOP
NO_CHECK_NEEDED:
RET
SELF_COLLISION_CHECK ENDP
MOVE_SNAKE PROC NEAR
;copy each arr[i] to arr[i+1] for snake length
;move snake head in direction
XOR AX,AX
MOV AL,2
MOV DI,SNAKE_LENGTH
MUL DI
MOV DI,AX
;01 23 45
COPY_SNAKE:
SUB DI,2
MOV AX,SNAKE_ARRAY[DI]
ADD DI, 2
MOV SNAKE_ARRAY[DI],AX
SUB DI,2
CMP DI,00h
JNE COPY_SNAKE
MOV DX,SNAKE_ARRAY[DI]
CMP HEAD_DIRECTION,00h
JE MOVE_UP
CMP HEAD_DIRECTION,01h
JE MOVE_DOWN
CMP HEAD_DIRECTION,02h
JE MOVE_LEFT
CMP HEAD_DIRECTION,03h
JE MOVE_RIGHT
MOVE_UP:
DEC DL
CMP DL,00h
JGE MOVE_DONE
MOV DL,19h
JMP MOVE_DONE
MOVE_DOWN:
INC DL
CMP DL,19h
JLE MOVE_DONE
MOV DL,00h
JMP MOVE_DONE
MOVE_LEFT:
DEC DH
CMP DH,00h
JGE MOVE_DONE
MOV DH,28h
JMP MOVE_DONE
MOVE_RIGHT:
INC DH
CMP DH,28h
JLE MOVE_DONE
MOV DH,00h
JMP MOVE_DONE
MOVE_DONE:
MOV SNAKE_ARRAY[DI],DX
RET
MOVE_SNAKE ENDP
DRAW_PEBBLE PROC NEAR
;;x pos
MOV AX,PEBBLE_POS
MOV AL,AH ;move x coord to al
XOR AH,AH ;clear ah
MOV CL,BLOCK_SIZE
MUL CL ;ax = al*cl
MOV CX,AX ;got x coord position
MOV SNAKE_HEAD_X,CX
;y coord
MOV AX,PEBBLE_POS
XOR AH,AH ;clear ah
MOV DL,BLOCK_SIZE
MUL DL ;ax = al*dl
MOV DX,AX ;got y coord position
MOV SNAKE_HEAD_Y,DX
CALL DRAW_ONE_BLOCK_HORIZONTAL
RET
DRAW_PEBBLE ENDP
DRAW_SNAKE PROC NEAR
XOR AX,AX
MOV AL,2
MOV DI,SNAKE_LENGTH
MUL DI
MOV DI,AX
DRAW_ONE_BLOCK:
SUB DI,2
;x coord
MOV AX,SNAKE_ARRAY[DI] ;copy whole snake blck
MOV AL,AH ;move x coord to al
XOR AH,AH ;clear ah
MOV CL,BLOCK_SIZE
MUL CL ;ax = al*cl
MOV CX,AX ;got x coord position
MOV SNAKE_HEAD_X,CX
;y coord
MOV AX,SNAKE_ARRAY[DI] ;copy whole snake blck
XOR AH,AH ;clear ah
MOV DL,BLOCK_SIZE
MUL DL ;ax = al*dl
MOV DX,AX ;got y coord position
MOV SNAKE_HEAD_Y,DX
CALL DRAW_ONE_BLOCK_HORIZONTAL
CMP DI,00h
JNE DRAW_ONE_BLOCK
RET
DRAW_SNAKE ENDP
DRAW_ONE_BLOCK_HORIZONTAL PROC NEAR
DOBH_LOOP:
MOV AH,0Ch ;draw pixel
MOV AL,0Fh ;color
MOV BH,00h ;page num
INT 10h
INC CX ;inc pixel x by 1
;compare if we have enough pixels done for row
;need H of SNAKE_ARRAY[DI]
;multiply by block size, add block size
;compare to current pixel position
MOV AX,CX
SUB AX,SNAKE_HEAD_X
CMP AL,BLOCK_SIZE
JNG DOBH_LOOP
;line finished: reset position
MOV CX,SNAKE_HEAD_X
;new line
INC DX
;compare if we have enough rows
MOV AX,DX
SUB AX,SNAKE_HEAD_Y
CMP AL,BLOCK_SIZE
JNG DOBH_LOOP
RET
DRAW_ONE_BLOCK_HORIZONTAL ENDP
KEYBOARD_INPUT PROC NEAR
;check if any key has been pressed if not exit
MOV AH,01h
INT 16h
JZ KEYBOARD_END
;check which key is being pressed
;TODO CHECK IF YOU CAN READ FROM 01h
MOV AH,00h
INT 16h
;exit on escape
CMP AH,01h
JNE DONT_EXIT
CALL EXIT_NOW
DONT_EXIT:
;if it is 'arrow_up' move up
CMP AH,48h
JE MOVE_HEAD_UP
;if it is 'arrow_down move down
CMP AH,50h
JE MOVE_HEAD_DOWN
CMP AH,4Bh
JE MOVE_HEAD_LEFT
CMP AH,4Dh
JE MOVE_HEAD_RIGHT
JMP KEYBOARD_END
MOVE_HEAD_UP:
CMP HEAD_DIRECTION,01h
JE KEYBOARD_END
MOV HEAD_DIRECTION,00h
JMP KEYBOARD_END
MOVE_HEAD_DOWN:
CMP HEAD_DIRECTION,00h
JE KEYBOARD_END
MOV HEAD_DIRECTION,01h
JMP KEYBOARD_END
MOVE_HEAD_LEFT:
CMP HEAD_DIRECTION,03h
JE KEYBOARD_END
MOV HEAD_DIRECTION,02h
JMP KEYBOARD_END
MOVE_HEAD_RIGHT:
CMP HEAD_DIRECTION,02h
JE KEYBOARD_END
MOV HEAD_DIRECTION,03h
JMP KEYBOARD_END
KEYBOARD_END:
RET
KEYBOARD_INPUT ENDP
EXIT_NOW PROC NEAR
MOV AH,4Ch
INT 21h
EXIT_NOW ENDP
CODE ENDS
END