-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.s
417 lines (354 loc) · 12.9 KB
/
puzzle.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# The contents of this file are not graded, it exists purely as a reference solution that you can use
# #define MAX_GRIDSIZE 16
# #define MAX_MAXDOTS 15
# /*** begin of the solution to the puzzle ***/
# // encode each domino as an int
# int encode_domino(unsigned char dots1, unsigned char dots2, int max_dots) {
# return dots1 < dots2 ? dots1 * max_dots + dots2 + 1 : dots2 * max_dots + dots1 + 1;
# }
encode_domino:
bge $a0, $a1, encode_domino_greater_row
mul $v0, $a0, $a2 # col * max_dots
add $v0, $v0, $a1 # col * max_dots + row
add $v0, $v0, 1 # col * max_dots + row + 1
j encode_domino_end
encode_domino_greater_row:
mul $v0, $a1, $a2 # row * max_dots
add $v0, $v0, $a0 # row * max_dots + col
add $v0, $v0, 1 # col * max_dots + row + 1
encode_domino_end:
jr $ra
# -------------------------------------------------------------------------
next:
# $a0 = row
# $a1 = col
# $a2 = num_cols
# $v0 = next_row
# $v1 = next_col
# int next_row = ((col == num_cols - 1) ? row + 1 : row);
move $v0, $a0
sub $t0, $a2, 1
bne $a1, $t0, next_col
add $v0, $v0, 1
next_col:
# int next_col = (col + 1) % num_cols;
add $t1, $a1, 1
rem $v1, $t1, $a2
jr $ra
# // main solve function, recurse using backtrack
# // puzzle is the puzzle question struct
# // solution is an array that the function will fill the answer in
# // row, col are the current location
# // dominos_used is a helper array of booleans (represented by a char)
# // that shows which dominos have been used at this stage of the search
# // use encode_domino() for indexing
# int solve(dominosa_question* puzzle,
# unsigned char* solution,
# int row,
# int col) {
#
# int num_rows = puzzle->num_rows;
# int num_cols = puzzle->num_cols;
# int max_dots = puzzle->max_dots;
# int next_row = ((col == num_cols - 1) ? row + 1 : row);
# int next_col = (col + 1) % num_cols;
# unsigned char* dominos_used = puzzle->dominos_used;
#
# if (row >= num_rows || col >= num_cols) { return 1; }
# if (solution[row * num_cols + col] != 0) {
# return solve(puzzle, solution, next_row, next_col);
# }
#
# unsigned char curr_dots = puzzle->board[row * num_cols + col];
#
# if (row < num_rows - 1 && solution[(row + 1) * num_cols + col] == 0) {
# int domino_code = encode_domino(curr_dots,
# puzzle->board[(row + 1) * num_cols + col],
# max_dots);
#
# if (dominos_used[domino_code] == 0) {
# dominos_used[domino_code] = 1;
# solution[row * num_cols + col] = domino_code;
# solution[(row + 1) * num_cols + col] = domino_code;
# if (solve(puzzle, solution, next_row, next_col)) {
# return 1;
# }
# dominos_used[domino_code] = 0;
# solution[row * num_cols + col] = 0;
# solution[(row + 1) * num_cols + col] = 0;
# }
# }
# if (col < num_cols - 1 && solution[row * num_cols + (col + 1)] == 0) {
# int domino_code = encode_domino(curr_dots,
# puzzle->board[row * num_cols + (col + 1)],
# max_dots);
# if (dominos_used[domino_code] == 0) {
# dominos_used[domino_code] = 1;
# solution[row * num_cols + col] = domino_code;
# solution[row * num_cols + (col + 1)] = domino_code;
# if (solve(puzzle, solution, next_row, next_col)) {
# return 1;
# }
# dominos_used[domino_code] = 0;
# solution[row * num_cols + col] = 0;
# solution[row * num_cols + (col + 1)] = 0;
# }
# }
# return 0;
# }
solve:
sub $sp, $sp, 80
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
sw $s3, 16($sp)
sw $s4, 20($sp)
sw $s5, 24($sp)
sw $s6, 28($sp)
sw $s7, 32($sp)
move $s0, $a0 # puzzle
move $s1, $a1 # solution
move $s2, $a2 # row
move $s3, $a3 # col
# int num_rows = puzzle->num_rows;
# int num_cols = puzzle->num_cols;
# int max_dots = puzzle->max_dots;
# unsigned char* dominos_used = puzzle->dominos_used;
lw $s4, 0($s0) # puzzle->num_rows
lw $s5, 4($s0) # puzzle->num_cols
lw $s6, 8($s0) # puzzle->max_dots
la $s7, 268($s0) # puzzle->dominos_used
# Compute:
# - next_row (Done below)
# - next_col (Done below)
mul $t0, $s2, $s5
add $t0, $t0, $s3 # row * num_cols + col
add $t1, $s2, 1
mul $t1, $t1, $s5
add $t1, $t1, $s3 # (row + 1) * num_cols + col
mul $t2, $s2, $s5
add $t2, $t2, $s3
add $t2, $t2, 1 # row * num_cols + (col + 1)
la $t3, 12($s0) # puzzle->board
add $t4, $t3, $t0
lbu $t9, 0($t4)
sw $t9, 44($sp) # puzzle->board[row * num_cols + col]
add $t4, $t3, $t1
lbu $t9, 0($t4)
sw $t9, 48($sp) # puzzle->board[(row + 1) * num_cols + col]
add $t4, $t3, $t2
lbu $t9, 0($t4)
sw $t9, 52($sp) # puzzle->board[row * num_cols + (col + 1)]
# solution addresses
add $t9, $s1, $t0
sw $t9, 56($sp) # &solution[row * num_cols + col]
add $t9, $a1, $t1
sw $t9, 60($sp) # &solution[(row + 1) * num_cols + col]
add $t9, $a1, $t2
sw $t9, 64($sp) # &solution[row * num_cols + (col + 1)]
# int next_row = ((col == num_cols - 1) ? row + 1 : row);
# int next_col = (col + 1) % num_cols;
move $a0, $s2
move $a1, $s3
move $a2, $s5
jal next
sw $v0, 36($sp)
sw $v1, 40($sp)
# if (row >= num_rows || col >= num_cols) { return 1; }
sge $t0, $s2, $s4
sge $t1, $s3, $s5
or $t0, $t0, $t1
beq $t0, 0, solve_not_base
li $v0, 1
j solve_end
solve_not_base:
# if (solution[row * num_cols + col] != 0) {
# return solve(puzzle, solution, next_row, next_col);
# }
lw $t0, 56($sp)
lb $t0, 0($t0)
beq $t0, 0, solve_not_solved
move $a0, $s0
move $a1, $s1
move $a2, $v0
move $a3, $v1
jal solve
j solve_end
solve_not_solved:
# unsigned char curr_dots = puzzle->board[row * num_cols + col];
lw $t9, 44($sp) # puzzle->board[row * num_cols + col]
# if (row < num_rows - 1 && solution[(row + 1) * num_cols + col] == 0) {
sub $t5, $s4, 1
bge $s2, $t5, end_vert
lw $t0, 60($sp)
lbu $t8, 0($t0) # solution[(row + 1) * num_cols + col]
bne $t8, 0, end_vert
# int domino_code = encode_domino(curr_dots,
# puzzle->board[(row + 1) * num_cols + col],
# max_dots);
move $a0, $t9
lw $a1, 48($sp)
move $a2, $s6
jal encode_domino
sw $v0, 68($sp)
# if (dominos_used[domino_code] == 0) {
add $t0, $s7, $v0
lbu $t1, 0($t0)
bne $t1, 0, end_vert
# dominos_used[domino_code] = 1;
li $t1, 1
sb $t1, 0($t0)
# solution[row * num_cols + col] = domino_code;
# solution[(row + 1) * num_cols + col] = domino_code;
lw $t0, 56($sp)
sb $v0, 0($t0)
lw $t0, 60($sp)
sb $v0, 0($t0)
# if (solve(puzzle, solution, next_row, next_col)) {
# return 1;
# }
move $a0, $s0
move $a1, $s1
lw $a2, 36($sp)
lw $a3, 40($sp)
jal solve
beq $v0, 0, end_vert_if
li $v0, 1
j solve_end
end_vert_if:
# dominos_used[domino_code] = 0;
lw $v0, 68($sp) # domino_code
add $t0, $v0, $s7
sb $zero, 0($t0)
# solution[row * num_cols + col] = 0;
lw $t0, 56($sp)
sb $zero, 0($t0)
# solution[(row + 1) * num_cols + col] = 0;
lw $t0, 60($sp)
sb $zero, 0($t0)
# }
# }
end_vert:
# if (col < num_cols - 1 && solution[row * num_cols + (col + 1)] == 0) {
sub $t5, $s5, 1
bge $s3, $t5, ret_0
lw $t0, 64($sp)
lbu $t1, 0($t0) # solution[row * num_cols + (col + 1)]
bne $t1, 0, ret_0
# int domino_code = encode_domino(curr_dots,
# puzzle->board[row * num_cols + (col + 1)],
# max_dots);
lw $a0, 44($sp) # puzzle->board[row * num_cols + col]
lw $a1, 52($sp)
move $a2, $s6
jal encode_domino
sw $v0, 68($sp)
# if (dominos_used[domino_code] == 0) {
add $t0, $s7, $v0
lbu $t1, 0($t0)
bne $t1, 0, ret_0
# dominos_used[domino_code] = 1;
li $t1, 1
sb $t1, 0($t0)
# solution[row * num_cols + col] = domino_code;
lw $t0, 56($sp)
sb $v0, 0($t0)
# solution[row * num_cols + (col + 1)] = domino_code;
lw $t0, 64($sp)
sb $v0, 0($t0)
# if (solve(puzzle, solution, next_row, next_col)) {
# return 1;
# }
move $a0, $s0
move $a1, $s1
lw $a2, 36($sp)
lw $a3, 40($sp)
jal solve
beq $v0, 0, end_horz_if
li $v0, 1
j solve_end
end_horz_if:
# dominos_used[domino_code] = 0;
lw $v0, 68($sp) # domino_code
add $t0, $s7, $v0
sb $zero, 0($t0)
# solution[row * num_cols + col] = 0;
lw $t0, 56($sp)
sb $zero, 0($t0)
# solution[row * num_cols + (col + 1)] = 0;
lw $t0, 64($sp)
sb $zero, 0($t0)
# }
# }
# return 0;
# }
ret_0:
li $v0, 0
solve_end:
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
lw $s3, 16($sp)
lw $s4, 20($sp)
lw $s5, 24($sp)
lw $s6, 28($sp)
lw $s7, 32($sp)
add $sp, $sp, 80
jr $ra
# // zero out an array with given number of elements
# void zero(int num_elements, unsigned char* array) {
# for (int i = 0; i < num_elements; i++) {
# array[i] = 0;
# }
# }
zero:
li $t0, 0 # i = 0
zero_loop:
bge $t0, $a0, zero_end_loop
add $t1, $a1, $t0
sb $zero, 0($t1)
add $t0, $t0, 1
j zero_loop
zero_end_loop:
jr $ra
# // the slow solve entry function,
# // solution will appear in solution array
# // return value shows if the dominosa is solved or not
# int slow_solve_dominosa(dominosa_question* puzzle, unsigned char* solution) {
# zero(puzzle->num_rows * puzzle->num_cols, solution);
# zero(MAX_MAXDOTS * MAX_MAXDOTS, dominos_used);
# return solve(puzzle, solution, 0, 0);
# }
# // end of solution
# /*** end of the solution to the puzzle ***/
slow_solve_dominosa:
sub $sp, $sp, 16
sw $ra, 0($sp)
sw $s0, 4($sp)
sw $s1, 8($sp)
sw $s2, 12($sp)
move $s0, $a0
move $s1, $a1
# zero(puzzle->num_rows * puzzle->num_cols, solution);
lw $t0, 0($s0)
lw $t1, 4($s0)
mul $a0, $t0, $t1
jal zero
# zero(MAX_MAXDOTS * MAX_MAXDOTS + 1, dominos_used);
li $a0, 226
la $a1, 268($s0)
jal zero
# return solve(puzzle, solution, 0, 0);
move $a0, $s0
move $a1, $s1
li $a2, 0
li $a3, 0
jal solve
lw $ra, 0($sp)
lw $s0, 4($sp)
lw $s1, 8($sp)
lw $s2, 12($sp)
add $sp, $sp, 16
jr $ra