-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot8.s
1335 lines (1115 loc) · 30.6 KB
/
bot8.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
.data
# syscall constants
PRINT_STRING = 4
PRINT_CHAR = 11
PRINT_INT = 1
# memory-mapped I/O
VELOCITY = 0xffff0010
ANGLE = 0xffff0014
ANGLE_CONTROL = 0xffff0018
BOT_X = 0xffff0020
BOT_Y = 0xffff0024
TIMER = 0xffff001c
REQUEST_PUZZLE = 0xffff00d0 ## Puzzle
SUBMIT_SOLUTION = 0xffff00d4 ## Puzzle
BONK_INT_MASK = 0x1000
BONK_ACK = 0xffff0060
TIMER_INT_MASK = 0x8000
TIMER_ACK = 0xffff006c
REQUEST_PUZZLE_INT_MASK = 0x800 ## Puzzle
REQUEST_PUZZLE_ACK = 0xffff00d8 ## Puzzle
PICKUP = 0xffff00f4
SPAWN_MINIBOT = 0xffff00dc
BUILD_SILO = 0xffff2000
SELECT_IDLE = 0xffff00e4
SET_TARGET = 0xffff00e8
GET_KERNEL_LOCATIONS = 0xffff200c
SELECT_ID = 0xffff2004
GET_MINIBOT = 0xffff2014
GET_MAP = 0xffff00f0
# Add any MMIO that you need here (see the Spimbot Documentation)
CTR_LEFT = 15
CTR_TOP = 7
### Puzzle
GRIDSIZE = 16
signal: .word 0
has_puzzle: .word 0
anchor: .word 0
puzzle: .half 0:2000
heap: .half 0:2000
location: .byte 0:1700
minibot: .word 0:30
atk_flag: .word 0
test_loc: .word 0:10
puzzle_cnt: .word 6
silo_built: .word 0
#### Puzzle
BNK_AGL: .word 45
three: .float 3.0
five: .float 5.0
PI: .float 3.141592
F180: .float 180.0
.text
main:
# Construct interrupt mask
li $t4, 0
or $t4, $t4, REQUEST_PUZZLE_INT_MASK # puzzle interrupt bit
or $t4, $t4, TIMER_INT_MASK # timer interrupt bit
or $t4, $t4, BONK_INT_MASK # timer interrupt bit
or $t4, $t4, 1 # global enable
mtc0 $t4, $12
#Fill in your code here
la $t0, puzzle
sw $t0, REQUEST_PUZZLE
j center_main
main_dispatch:
lw $t0, signal
beq $t0, 1 mission_solve6
beq $t0, 0 mission_move_main
mission_solve6:
lw $t0, TIMER
addi $t0 $t0 200
sw $t0, TIMER
lw $s0, puzzle_cnt
puzzle_loop:
beq $s0 $0 out_puzzle_loop ## when solved 4 puzzles go collect
la $t1 has_puzzle
la $a0 puzzle
la $a1 heap
li $a2 0
li $a3 0
while:
lw $t2 0($t1)
bne $0 $t2 start_solving
j while
start_solving:
jal slow_solve_dominosa
la $a0 puzzle
sw $a0 REQUEST_PUZZLE
la $a1 heap
sw $a1 SUBMIT_SOLUTION
sw $0 has_puzzle
addi $s0 $s0 -1
j puzzle_loop
out_puzzle_loop:
li $t0 1
sw $t0, SPAWN_MINIBOT
lw $t0, silo_built
bne $t0, $zero, builtt
jal test_field
# j test_field
builtt:
j main_dispatch
center_main:
## moves the bot to the center of the map
lw $t0 BOT_X($0)
blt $t0 160 upper_start
li $t1 225
j start_end
upper_start:
li $t1 45
start_end:
sw $t1 ANGLE($0)
li $t1 1
sw $t1 ANGLE_CONTROL($0)
li $a0 20
li $a1 20
jal dist
li $t0 10
sw $t0 VELOCITY($0)
move $a0 $v0
li $t8 8
mul $a0 $a0 $t8
jal stop_timer ## move to 20 18?
j main_dispatch
mission_move_main:
lw $t8, TIMER
li $t9 1000000
add $t8 $t8 $t9
sw $t8, TIMER
li $s8 CTR_LEFT
li $s7 CTR_TOP
get_more:
jal main_target
move $a0 $s8
move $a1 $s7
jal move_main
lw $t8 signal
bne $t8, 0, main_dispatch
j get_more
test_field:
# lw $t8, TIMER
# li $t9 10000
# add $t8 $t8 $t9
# sw $t8, TIMER
#your code
# lw $t0, puzzle_cnt
# beq $t0, 4, enter_move_again
sub $sp, $sp, 4
sw $ra, 0($sp)
li $t0, 1
sw $t0, SPAWN_MINIBOT
sw $t0, SPAWN_MINIBOT
sw $t0, SPAWN_MINIBOT
li $t0, 0x0505
sw $t0, SELECT_IDLE
sw $t0, SET_TARGET
li $a0, 1000
jal stop_timer
li $t0, 0x0505 ## build silo at (6,26)
sw $t0, BUILD_SILO
li $t8, 1
sw $t8, silo_built
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
main_mbot: ## spawn 2 bots and move to silo
li $t0 1
sw $t0, SPAWN_MINIBOT
sw $t0, SPAWN_MINIBOT
# sw $t0, SPAWN_MINIBOT
# sw $t0, SPAWN_MINIBOT
li $t0, 0x00001a06
sw $t0, SELECT_IDLE
sw $t0, SET_TARGET
jr $ra
# enter_move_again:
# jal get_next_location
# move_again:
# la $a1 test_loc
# jal move_minibot
# la $a0 400
# jal stop_timer
# sw $t0, SELECT_IDLE
# li $t0, 0x1a06
# sw $t0, SET_TARGET
# la $a0 500
# jal stop_timer
# lw $t5, signal
# bne $t5, 2, main_dispatch
# j move_again
# j test_field
move_minibot: ## move $a0 bots to $a0 different locations stored in $a1
sub $sp, $sp, 4
la $a1 test_loc
sw $ra, 0($sp)
la $t0, minibot
sw $t0, GET_MINIBOT
lw $t1, 0($t0) # number of minibots
#bgt $a0, $t1, mv_mbot_out
addi $t2, $t0, 4 # minibots*
li $t0, 0
mv_mbot:
bge $t0, $t1, mv_mbot_out
li $t3, 4
mul $t3, $t3, $t0
add $t3, $t3, $a1
lw $t4, 0($t3)
# sll $t4, $t4, 8
# lbu $t5, 0($t3) # loc
lw $t6, 0($t2) # bot id
sw $t6, SELECT_ID
sw $t4, SET_TARGET
addi $t0, $t0, 1
addi $t2, $t2, 8
j mv_mbot
mv_mbot_out:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
get_oppo_silo:
la $t0 location
sw $t0, GET_MAP
li $t1 0
li $t4 3
oppo_oloop:
bge $t1, 1600, oppo_ooout
add $t3 $t0 $t1
lbu $t3 0($t1)
beq $t3 $t4 storeturn
addi $t1 $t1 1
j oppo_oloop
oppo_oout:
li $t4 0
sw $t4, atk_flag
jr $ra
storeturn:
li $t4 1
sw $t4, atk_flag
li $t4 40
div $t5 $t1 $t4 #y
mul $t4 $t4 $t5
sub $t4 $t1 $t4 #x
li $t6 8
sll $t4 $t4 $t6
or $t4 $t4 $t5
sw $t4, test_loc
assign_minibot:
addi $sp $sp -4
sw $ra 0($sp)
jal get_next_location
la $t0 minibot
sw $t0 GET_MINIBOT($0)
lw $t0 minibot($0)
li $t1 0
li $t4 8
la $t2 minibot
addi $t2 $t2 4
addi $t0 $t0 -1
li $v0 0
li $v1 0
assign_loop:
ble $t0 $t1 assign_loop_out
mul $t3 $t1 $t4
add $t5 $t3 $t2
lw $t5 0($t5) #ID
sw $t5 SELECT_ID($0)
jal get_next_location
sll $t8 $v0 8
or $t8 $t8 $v1
sw $t8 SET_TARGET($0)
addi $t1 $t1 1
j assign_loop
assign_loop_out:
lw $ra 0($sp)
addi $sp $sp 4
jr $ra
get_next_location:
la $t9 location
sw $t9 GET_KERNEL_LOCATIONS($0)
la $t4 test_loc
add $t9 4
li $t5 0 # find 10
loopp:
bge $t5 10 loop_over
lw $t6, TIMER
div $t8, $t6, 15
div $t9, $t6, 13
mul $t9, $t9, 13
sub $t7, $t6, $t9
mul $t8, $t8, 15
sub $t6 $t6 $t8
li $a1 40
addi $t7 $t7 20
addi $t6 $t6 2
la $t9 location
addi $t9 $t9 4
loc_outer:
bge $t6 34 loc_outer_exit
loc_inner:
bge $t7 17 loc_inner_exit
mul $t8 $a1 $t6
add $t8 $t8 $t7
add $t8 $t8 $t9
lb $t8 0($t8)
bgt $t8 3 loc_outer_exit
addi $t7 1
j loc_inner
loc_inner_exit:
li $t7 20
addi $t6 1
j loc_outer
loc_outer_exit:
ble $t8 3 gnls
sll $t8 $t6 8
or $t8 $t7 $t8 ###reversed
sw $t8 0($t4)
add $t4 4
add $t5 1
gnls:
j loopp
loop_over:
jr $ra
return_minibot: ## move all idle minibots back to silo
li $t0, 0x00001a06
sw $t0, SELECT_IDLE
sw $t0, SET_TARGET
jr $ra
move_main:
addi $sp $sp -12
sw $ra 0($sp)
sw $s0 4($sp)
sw $s1 8($sp)
lw $t0, silo_built
beq $t0, $zero, label2
la $t0, minibot
sw $t0, GET_MINIBOT
lw $t1, minibot
bne $t1, $zero, label
jal main_mbot
label:
jal move_minibot
label2:
move $a0 $s8
move $a1 $s7
lw $t0 BOT_X #x
lw $t1 BOT_Y #y
li $t8 8
# div $t0 $t0 $t8
# div $t1 $t1 $t8
mul $a0 $a0 $t8
mul $a1 $a1 $t8
slti $t2 $t0 160 #x < 160
slti $t3 $t1 160 #y < 160
not $t4 $t2
not $t5 $t3
sub $s0 $a0 $t0
sub $s1 $a1 $t1
ble $s0 $0 x_offset_neg
x_offset_pos:
ble $s1 $0 xpyn
xpyp:
ble $t3 $t2 xy_move
j yx_move
xpyn:
ble $t5 $t2 xy_move
j yx_move
x_offset_neg:
ble $s1 $0 xnyn
xnyp:
ble $t5 $t2 yx_move
j xy_move
xnyn:
ble $t3 $t2 yx_move
j xy_move
xy_move:
move $a0 $s0
abs $a0 $a0
ble $s0 $0 xleft
xright:
li $t8 0
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
jal stop_timer
ble $s1 $0 xyup
j xydown
xleft:
li $t8 180
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
jal stop_timer
ble $s1 $0 xyup
j xydown
xyup:
li $t8 270
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s1
neg $a0 $a0
jal stop_timer
j move_end
xydown:
li $t8 90
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s1
jal stop_timer
j move_end
yx_move:
ble $s1 $0 yup
ydown:
li $t8 90
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s1
jal stop_timer
ble $s1 $0 yxleft
j yxright
yup:
li $t8 270
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s1
neg $a0 $a0
jal stop_timer
ble $s1 $0 yxleft
j yxright
yxleft:
li $t8 180
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s0
neg $a0 $a0
jal stop_timer
j move_end
yxright:
li $t8 0
sw $t8 ANGLE
li $t8 1
sw $t8 ANGLE_CONTROL
li $t8 10
sw $t8 VELOCITY
move $a0 $s0
jal stop_timer
j move_end
move_end:
jal return_minibot
lw $ra 0($sp)
lw $s0 4($sp)
lw $s1 8($sp)
addi $sp $sp 12
jr $ra
main_target:
la $t9 location
sw $t9 GET_KERNEL_LOCATIONS($0)
#ensure safety
li $t5 26
li $t6 33
bge $s7 $t6 skipv0reset
li $s7 CTR_TOP
skipv0reset:
bge $s8 $t5 skipv1reset
li $s8 CTR_LEFT
skipv1reset:
li $t6 40
addi $t9 $t9 4
mt_outer:
bge $s7 33 mt_outer_exit
mt_inner:
bge $s8 26 mt_inner_exit
mul $t8 $t6 $s7
add $t8 $t8 $s8
add $t8 $t8 $t9
lb $t8 0($t8)
blt $t8 4 mt_next_loc
jr $ra
mt_next_loc:
addi $s8 $s8 1
j mt_inner
mt_inner_exit:
li $s8 CTR_LEFT
addi $s7 $s7 1
j mt_outer
mt_outer_exit:
li $s7 CTR_TOP
###print result
jr $ra
dist:
mul $a0, $a0, $a0 # x^2
mul $a1, $a1, $a1 # y^2
add $v0, $a0, $a1 # x^2 + y^2
mtc1 $v0, $f0
cvt.s.w $f0, $f0 # float(x^2 + y^2)
sqrt.s $f0, $f0 # sqrt(x^2 + y^2)
cvt.w.s $f0, $f0 # int(sqrt(...))
mfc1 $v0, $f0
jr $ra
.globl sb_arctan
sb_arctan:
li $v0, 0 # angle = 0;
abs $t0, $a0 # get absolute values
abs $t1, $a1
ble $t1, $t0, no_TURN_90
## if (abs(y) > abs(x)) { rotate 90 degrees }
move $t0, $a1 # int temp = y;
neg $a1, $a0 # y = -x;
move $a0, $t0 # x = temp;
li $v0, 90 # angle = 90;
no_TURN_90:
bgez $a0, pos_x # skip if (x >= 0)
## if (x < 0)
add $v0, $v0, 180 # angle += 180;
pos_x:
mtc1 $a0, $f0
mtc1 $a1, $f1
cvt.s.w $f0, $f0 # convert from ints to floats
cvt.s.w $f1, $f1
div.s $f0, $f1, $f0 # float v = (float) y / (float) x;
mul.s $f1, $f0, $f0 # v^^2
mul.s $f2, $f1, $f0 # v^^3
l.s $f3, three # load 3.0
div.s $f3, $f2, $f3 # v^^3/3
sub.s $f6, $f0, $f3 # v - v^^3/3
mul.s $f4, $f1, $f2 # v^^5
l.s $f5, five # load 5.0
div.s $f5, $f4, $f5 # v^^5/5
add.s $f6, $f6, $f5 # value = v - v^^3/3 + v^^5/5
l.s $f8, PI # load PI
div.s $f6, $f6, $f8 # value / PI
l.s $f7, F180 # load 180.0
mul.s $f6, $f6, $f7 # 180.0 * value / PI
cvt.w.s $f6, $f6 # convert "delta" back to integer
mfc1 $t0, $f6
add $v0, $v0, $t0 # angle += delta
jr $ra
# 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)