This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slug.s
399 lines (381 loc) · 8.37 KB
/
slug.s
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
# ___ ___ ___ ___
# /\ \ /\__\ /\__\ /\ \
# /::\ \ /:/ / /:/ / /::\ \
# /:/\ \ \ /:/ / /:/ / /:/\:\ \
# _\:\~\ \ \ /:/ / /:/ / ___ /:/ \:\ \
# /\ \:\ \ \__\ /:/__/ /:/__/ /\__\ /:/__/_\:\__\
# \:\ \:\ \/__/ \:\ \ \:\ \ /:/ / \:\ /\ \/__/
# \:\ \:\__\ \:\ \ \:\ /:/ / \:\ \:\__\
# \:\/:/ / \:\ \ \:\/:/ / \:\/:/ /
# \::/ / \:\__\ \::/ / \::/ /
# \/__/ \/__/ \/__/ \/__/
#
#
# A 512-byte x86 bootsector game.
.code16
.text
.global _boot
.include "macros.s"
# Stack layout:
#
# | ... | BP
# |---------------|
# | direction | -2(BP)
# |---------------|
# | head | -4(BP)
# |---------------|
# | tail | -6(BP)
# |---------------|
# | target coords | -8(BP)
# |---------------|
# | | -10(BP)
# | |
# | SLUG | ...
# | |
# | | -88(BP)
# |---------------|
_boot:
#cli
xor %ax, %ax
mov $STACK_SEGMENT, %ax
mov %ax, %ss
mov %sp, %bp
# the direction
push $DOWN
# SLUG head
push $0
# SLUG tail
push $0
# Target coordinates
call gen_random_coord
push %dx
sub $SLUG_LEN, %sp
# the x-coordinate of the slug
mov $60, %bx
# the y-coordinate of the slug
mov $80, %cx
call compress_coords
lea SLUG_START(%bp), %bx
mov -4(%bp), %di
write_coords slug=%bx, coords=%al, pos=%di
# 7 = grey
mov $7, %bx
call init_video
.Ltick:
mov -4(%bp), %di
lea SLUG_START(%bp), %bx
call read_coords
mov -2(%bp), %ax
.Ltry_right:
cmp $RIGHT, %ax
jne .Ltry_down
mov %bx, %ax
mov $GRID_WIDTH, %dx
call slug_forward
mov %ax, %bx
jmp .Ldraw
.Ltry_down:
cmp $DOWN, %ax
jne .Ltry_left
mov %cx, %ax
mov $GRID_HEIGHT, %dx
call slug_forward
mov %ax, %cx
jmp .Ldraw
.Ltry_left:
cmp $LEFT, %ax
jne .Ltry_up
mov %bx, %ax
mov $(GRID_WIDTH - SQUARE_SIZE), %dx
call slug_backward
mov %ax, %bx
jmp .Ldraw
.Ltry_up:
mov %cx, %ax
mov $(GRID_HEIGHT- SQUARE_SIZE), %dx
call slug_backward
mov %ax, %cx
.Ldraw:
# Draw the next position
draw_square x=%bx, y=%cx, colour=$0xc0a
# Save the new slug coordinates
call compress_coords
push %ax
# Check for collisions
mov %al, %cl
lea SLUG_START(%bp), %bx
mov -6(%bp), %di
mov -4(%bp), %si
call coord_in_slug
test %al, %al
jnz .Lhlt
pop %ax
push %ax
# Check if the slug reached the target
cmp -8(%bp), %al
jne .Lerase_tail
# Create a new random target
call gen_random_coord
mov %dx, -8(%bp)
# Grow the slug
jmp .Lgrow
.Lerase_tail:
mov -6(%bp), %di
lea SLUG_START(%bp), %bx
call read_coords
# Delete the tail
draw_square x=%bx, y=%cx, colour=$0xc07
# Advance the tail
mov -6(%bp), %ax
call increment_end
mov %ax, -6(%bp)
.Lgrow:
# Advance the head
mov -4(%bp), %ax
call increment_end
mov %ax, -4(%bp)
# Store the new slug coordinates
lea SLUG_START(%bp), %bx
mov -4(%bp), %di
pop %ax
push %ax
write_coords slug=%bx, coords=%al, pos=%di
mov -8(%bp), %cl
lea SLUG_START(%bp), %bx
mov -6(%bp), %di
mov -4(%bp), %si
call coord_in_slug
test %al, %al
jnz .Lread_input
mov -8(%bp), %ax
call decompress_coords
# Draw a red square
draw_square x=%bx, y=%cx, colour=$0xc04
.Lread_input:
# Pause for 0.25s
pause ms=250
# Check for keystroke
mov $1, %ah
int $0x16
# No keystroke - just keep drawing
jz .Ltick
# Read the keystroke
xor %ax, %ax
int $0x16
mov %al, -2(%bp)
jmp .Ltick
.Lhlt:
# 4 = red
mov $4, %bx
call init_video
hlt
# Set the video mode and the background colour.
#
# Arguments:
# BX - the backgorund colour
init_video:
# Set the video mode to VGA 320 x 200 colour
mov $0xd, %ax
int $0x10
# Set the background colour
mov $0xb, %ah
int $0x10
ret
# AX - the head/tail of SLUG
#
# Calculate the value of (AX + 1) mod SLUG_LEN
#
# Arguments:
# AX - the value to increment (SLUG_HEAD or SLUG_TAIL)
# Returns:
# AX - (AX + 1) mod SLUG_LEN
increment_end:
add $1, %ax
mov $SLUG_LEN, %bl
div %bl
mov %ah, %al
xor %ah, %ah
ret
# Store the SLUG coordinates in a single byte.
#
# Arguments:
# BX - the x-coordinate
# CX - the y-coordinate
# Returns:
# AL - the coordinates stored as 4-bit values (AH = BX, AL = CX)
compress_coords:
# x-coordinate
xor %dx, %dx
mov %bx, %ax
mov $SQUARE_SIZE, %bx
div %bx
mov %ax, %bx
# y-coordinate (don't bother zeroing out DX, because the remainder
# of the previous division will be 0 anyway)
mov %cx, %ax
mov $SQUARE_SIZE, %cx
div %cx
# At this point, the lower 4 bits of AL contain the y-coordinate.
# Now move the x-coordinate and into the highest order 4 bits of AL
shl $4, %bl
add %bl, %al
ret
# Extract the SLUG coordinates from AL.
#
# Arguments:
# AL - the coordinates stored as 4-bit values (AH = BX, AL = CX)
# Returns:
# BX - the x-coordinate
# CX - the y-coordinate
decompress_coords:
# Save AL
mov %al, %cl
xor %ah, %ah
# x-coordinate
shr $4, %al
mov $SQUARE_SIZE, %dx
mul %dx
mov %ax, %bx
xor %ah, %ah
# y-coordinate
and $0b1111, %cl
mov %cl, %al
mov $SQUARE_SIZE, %dx
mul %dx
mov %ax, %cx
ret
# Read the coordinates stored at the specified SLUG index.
#
# Arguments:
# BX - the SLUG
# DI - the index of the coordinate to read
# Returns:
# BX - the x-coordinate
# CX - the y-coordinate
read_coords:
mov (%bx, %di), %al
call decompress_coords
ret
# Check whether the specified coordinates exist in the SLUG array.
#
# Arguments:
# BX - SLUG
# CL - the coordinates to search for (stored as two 4-bit values)
# DI - the tail
# SI - the head
# Returns:
# AL - whether the coordinate was found
coord_in_slug:
mov (%bx, %di), %al
cmp %al, %cl
je .Lfound
cmp %si, %di
je .Lnot_found
mov %di, %ax
push %bx
call increment_end
pop %bx
mov %ax, %di
jmp coord_in_slug
.Lnot_found:
xor %al, %al
ret
.Lfound:
mov $1, %al
ret
# Move the slug one square forward, ensuring it comes out the other side
# when it reaches the edge.
#
# Arguments:
# AX - position to advance
# DX - grid width/height
slug_forward:
add $SQUARE_SIZE, %ax
mov %dx, %di
xor %dx, %dx
div %di
mov %dx, %ax
ret
# Move the slug back one square, ensuring it comes out the other side
# when it reaches the edge.
#
# Arguments:
# AX - position to advance
# DX - grid width/height - SQUARE_SIZE
slug_backward:
sub $SQUARE_SIZE, %ax
test %ax, %ax
jge 1f
mov %dx, %ax
1:
ret
# Generate a random coordinate.
# Returns:
# DX - the coordinate
gen_random_coord:
mov $0, %ah
int $0x1a
mov %dx, %ax
and $0b1111, %al
# Make sure the coordinate is within bounds.
cmp $(GRID_HEIGHT - SQUARE_SIZE) / SQUARE_SIZE, %al
jle 1f
and $0b1111, %dl
add $(GRID_HEIGHT - SQUARE_SIZE) / SQUARE_SIZE, %dl
1:
ret
# Draw a square.
#
# Arguments:
# 8(%bp) - colour
# 6(%bp) - y-coordinate
# 4(%bp) - x-coordinate
draw_square:
push %bp
mov %sp, %bp
# Save the y-coordinate
push 6(%bp)
push $SQUARE_SIZE
1:
mov 4(%bp), %cx
mov %cx, %ax
add $SQUARE_SIZE, %ax
mov %ax, %gs
mov -2(%bp), %dx
mov 8(%bp), %es
.Ldraw_horizontal:
mov %es, %ax
xor %bh, %bh
int $0x10
mov %gs, %ax
add $1, %cx
cmp %ax, %cx
jl .Ldraw_horizontal
pop %ax
sub $1, %ax
push %ax
test %ax, %ax
jle .Ldone_draw_square
mov -2(%bp), %ax
add $1, %ax
mov %ax, -2(%bp)
jmp 1b
.Ldone_draw_square:
leave
ret
.set STACK_SEGMENT, 0x9000
.set DATA_SEGMENT, 0x9300
.set SQUARE_SIZE, 20
.set GRID_HEIGHT, 200
.set GRID_WIDTH, 320
.set UP, 107
.set RIGHT, 108
.set DOWN, 106
.set LEFT, 104
# A maximum of 160 coordinates
# The grid is 320 pixels wide and 200 pixels high. Since we use 20x20 squares,
# each position on the x-axis is between 0 and 16, and each position on
# the y-axis is between 0 and 10, so we only need 4 bits to represent each
# coordinate (10 * 16 * 4 bits = 80 bytes)
.set SLUG_LEN, 80
.set SLUG_START, -(SLUG_LEN + 8)