This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.s~
1094 lines (876 loc) · 22.8 KB
/
snake.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
# Requires:
# - [no external symbols]
#
# Provides:
# - Global variables:
.globl symbols
.globl grid
.globl snake_body_row
.globl snake_body_col
.globl snake_body_len
.globl snake_growth
.globl snake_tail
# - Utility global variables:
.globl last_direction
.globl rand_seed
.globl input_direction__buf
# - Functions for you to implement
.globl main
.globl init_snake
.globl update_apple
.globl move_snake_in_grid
.globl move_snake_in_array
# - Utility functions provided for you
.globl set_snake
.globl set_snake_grid
.globl set_snake_array
.globl print_grid
.globl input_direction
.globl get_d_row
.globl get_d_col
.globl seed_rng
.globl rand_value
########################################################################
# Constant definitions.
N_COLS = 15
N_ROWS = 15
MAX_SNAKE_LEN = N_COLS * N_ROWS
EMPTY = 0
SNAKE_HEAD = 1
SNAKE_BODY = 2
APPLE = 3
NORTH = 0
EAST = 1
SOUTH = 2
WEST = 3
########################################################################
# .DATA
.data
# const char symbols[4] = {'.', '#', 'o', '@'};
symbols:
.byte '.', '#', 'o', '@'
.align 2
# int8_t grid[N_ROWS][N_COLS] = { EMPTY };
grid:
.space N_ROWS * N_COLS
.align 2
# int8_t [MAX_SNAKE_LEN] = { EMPTY };
snake_body_row:
.space MAX_SNAKE_LEN
.align 2
# int8_t snake_body_col[MAX_SNAKE_LEN] = { EMPTY };
snake_body_col:
.space MAX_SNAKE_LEN
# int snake_body_len = 0;
snake_body_len:
.word 0
# int snake_growth = 0;
snake_growth:
.word 0
# int snake_tail = 0;
snake_tail:
.word 0
# Game over prompt, for your convenience...
main__game_over:
.asciiz "Game over! Your score was "
########################################################################
#
# Your journey begins here, intrepid adventurer!
#
# Implement the following 6 functions, and check these boxes as you
# finish implementing each function
#
# - [ o] main
# - [ O] init_snake
# - [ O] update_apple
# - [ o] update_snake
# - [ o] move_snake_in_grid
# - [ o] move_snake_in_array
#
########################################################################
# .TEXT <main>
.text
main:
# Args: void
# Returns:
# - $v0: int
#
# Frame: $ra, []
# Uses: [$a0,$a1,$v0,$t1]
# Clobbers: [$a0,$a1,$v0,$t1]
#
# Locals:
# - []
#
# Structure:
# main
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
main__prologue:
# set up stack frame
addiu $sp, $sp, -4
sw $ra, ($sp)
main__body:
jal init_snake # init_snake();
jal update_apple # update_apple();
main_do:
jal print_grid # print_grid();
jal input_direction # direction = input_direction()
move $a0, $v0
jal update_snake
bnez $v0, main_do # while (update_snake(direction));
main_print_score:
la $a0, main__game_over #printf("Game over! Your score was %d\n", score);
li $v0, 4
syscall
lw $t1, snake_body_len
div $t1, $t1, 3
move $a0, $t1 # printf("%d", z);
li $v0, 1
syscall
li $a0, '\n' # printf("%c", '\n');
li $v0, 11
syscall
main__epilogue:
# tear down stack frame
lw $ra, ($sp)
addiu $sp, $sp, 4
li $v0, 0
jr $ra # return 0;
########################################################################
# .TEXT <init_snake>
.text
init_snake:
# Args: void
# Returns: void
#
# Frame: $ra, []
# Uses: [$a0,$a1,$a2]
# Clobbers: [$a0,$a1,$a2,$v0]
#
# Locals:
# - []
#
# Structure:
# init_snake
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
init_snake__prologue:
# set up stack frame
addiu $sp, $sp, -4
sw $ra, ($sp)
init_snake__:
li $a0, 7 # set_snake(7, 7, SNAKE_HEAD);
li $a1, 7
li $a2, 1
jal set_snake
li $a0, 7 # set_snake(7, 6, SNAKE_BODY);
li $a1, 6
li $a2, 2
jal set_snake
li $a0, 7 # set_snake(7, 5, SNAKE_BODY);
li $a1, 5
li $a2, 2
jal set_snake
li $a0, 7 # set_snake(7, 4, SNAKE_BODY);
li $a1, 4
li $a2, 2
jal set_snake
init_snake__epilogue:
# tear down stack frame
lw $ra, ($sp)
addiu $sp, $sp, 4
jr $ra # return;
########################################################################
# .TEXT <update_apple>
.text
update_apple:
# Args: void
# Returns: void
#
# Frame: $ra, [s0,s1,s2,s3,s4]
# Uses: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4]
# Clobbers: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4]
#
# Locals:
# - [$s0,$s1,$s2,$s3,$s4]
#
# Structure:
# update_apple
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
update_apple__prologue:
# set up stack frame
addiu $sp, $sp, -24
sw $ra, 20($sp)
sw $s0, 16($sp)
sw $s1, 12($sp)
sw $s2, 8($sp)
sw $s3, 4($sp)
sw $s4, ($sp)
update_apple__body:
apple_do_loop_start:
li $a0, N_ROWS #apple_row = rand_value(N_ROWS);
jal rand_value
move $s0, $v0 #int apple_row = rand_value(n);
li $a0, N_COLS #apple_col = rand_value(N_COLS);
jal rand_value
move $s1, $v0 #int apple_col = rand_value(n);
apple_do_loop_cond:
li $s2, N_ROWS
mul $s2, $s2, $s0 #15*row
add $s2, $s2, $s1 #15*row+col
lb $s3, grid($s2) # load array value into register
bnez $s3, apple_do_loop_start #while (grid[apple_row][apple_col] != EMPTY);
apple_do_loop_end:
li $s4, APPLE #grid[apple_row][apple_col] = APPLE;
sb $s4, grid($s2)
update_apple__epilogue:
# tear down stack frame
lw $s4, ($sp)
lw $s3, 4($sp)
lw $s2, 8($sp)
lw $s1, 12($sp)
lw $s0, 16($sp)
lw $ra, 20($sp)
addiu $sp, $sp, 24
jr $ra # return;
########################################################################
# .TEXT <update_snake>
.text
update_snake:
# Args:
# - $a0: int direction
# Returns:
# - $v0: bool
#
# Frame: $ra, [$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
# Uses: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
# Clobbers: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
#
# Locals:
# - [$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
#
# Structure:
# update_snake
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
update_snake__prologue:
# set up stack frame
addiu $sp, $sp, -36
sw $ra, 32($sp)
sw $s0, 28($sp)
sw $s1, 24($sp)
sw $s2, 20($sp)
sw $s3, 16($sp)
sw $s4, 12($sp)
sw $s5, 8($sp)
sw $s6, 4($sp)
sw $s7, ($sp)
update_snake__body:
jal get_d_row
move $s0, $v0 #int d_row = get_d_row(direction);
jal get_d_col #int d_col = get_d_col(direction);
move $s1, $v0
li $s4, 0
lb $s2, snake_body_row($s4) # int head_row = snake_body_row[0];
lb $s3, snake_body_col($s4) # int head_col = snake_body_col[0];
li $s5, N_COLS #grid[head_row][head_col] = SNAKE_BODY;
mul $s5, $s2, $s5
add $s5, $s3, $s5
li $s6, SNAKE_BODY
sb $s6, grid($s5)
add $s0, $s2, $s0 #int new_head_row = head_row + d_row;
add $s1, $s3, $s1 #int new_head_col = head_col + d_col;
#if (new_head_row < 0) return false
bltz $s0, update_snake_return_false
#if(new_head_row >= N_ROWS)return false;
bge $s0, N_ROWS, update_snake_return_false
#if (new_head_col < 0) return false;
bltz $s1, update_snake_return_false
bge $s1, N_COLS, update_snake_return_false
li $s7, N_COLS #bool apple = (grid[new_head_row][new_head_col] == APPLE);
mul $s7, $s0, $s7 #15*row
add $s7, $s1, $s7 #15*row+col
lb $s2, grid($s7) # load bool apple into register
lw $s4, snake_body_len #snake_tail = snake_body_len - 1;
addi $s4, $s4, -1
sw $s4, snake_tail
move $a0, $s0 #if (! move_snake_in_grid(new_head_row, new_head_col))
move $a1, $s1
jal move_snake_in_grid
beqz $v0, update_snake_return_false #branch to return false if return not 0
jal move_snake_in_array #move_snake_in_array(new_head_row, new_head_col);
li $s3, APPLE
bne $s2, $s3, update_snake__epilogue # if (apple == FALSE) return true
lw $s5, snake_growth #snake_growth += 3;
addi $s5, $s5, 3
sw $s5, snake_growth
jal update_apple #update_apple();
j update_snake__epilogue #return true;
update_snake_return_false:
# tear down stack frame
lw $s7, ($sp)
lw $s6, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)
lw $ra, 32($sp)
addiu $sp, $sp, 36
li $v0, 0
jr $ra # return false;
update_snake__epilogue:
# tear down stack frame
lw $s7, ($sp)
lw $s6, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)
lw $ra, 32($sp)
addiu $sp, $sp, 36
li $v0, 1
jr $ra # return true;
########################################################################
# .TEXT <move_snake_in_grid>
.text
move_snake_in_grid:
# Args:
# - $a0: new_head_row
# - $a1: new_head_col
# Returns:
# - $v0: bool
#
# Frame: $ra, [$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
# Uses: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
# Clobbers: [$a0,$a1,$v0,$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
#
# Locals:
# - [$s0,$s1,$s2,$s3,$s4,$s5,$s6,$s7]
#
# Structure:
# move_snake_in_grid
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
move_snake_in_grid__prologue:
# set up stack frame
addiu $sp, $sp, -36
sw $ra, 32($sp)
sw $s0, 28($sp)
sw $s1, 24($sp)
sw $s2, 20($sp)
sw $s3, 16($sp)
sw $s4, 12($sp)
sw $s5, 8($sp)
sw $s6, 4($sp)
sw $s7, ($sp)
move_snake_in_grid__body:
lw $s3, snake_growth
blez $s3, snake_grow_else # snake_growth >= 0 jump to else
lw $s2, snake_tail # snake_tail++;
addi $s2, $s2, 1
sw $s2, snake_tail # if snake_tail <=0 go to else
lw $s2, snake_body_len # snake_body_len++;
addi $s2, $s2, 1
sw $s2, snake_body_len
lw $s2, snake_growth # snake_growth--;
addi $s2, $s2, -1
sw $s2, snake_growth
j snake_grow_else_after
snake_grow_else:
lw $s2, snake_tail #int tail = snake_tail;
lb $s0, snake_body_row($s2) #int tail_row = snake_body_row[tail];
lb $s1, snake_body_col($s2) #int tail_row = snake_body_row[tail];
li $s7, N_COLS #grid[tail_row][tail_col] = EMPTY
mul $s7, $s0, $s7
add $s7, $s1, $s7
li $s4, EMPTY
sb $s4, grid($s7)
snake_grow_else_after:
li $s6, N_COLS #if (grid[new_head_row][new_head_col] == SNAKE_BODY)
mul $s6, $a0, $s6
add $s6, $a1, $s6
lb $s5, grid($s6)
li $s4, SNAKE_BODY
beq $s5, $s4, move_snake_in_grid_false #return false;
li $s2, SNAKE_HEAD #grid[new_head_row][new_head_col] = SNAKE_HEAD;
sb $s2, grid($s6)
j move_snake_in_grid__epilogue #return true;
move_snake_in_grid_false:
# tear down stack frame
lw $s7, ($sp)
lw $s6, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)
lw $ra, 32($sp)
addiu $sp, $sp, 36
li $v0, 0
jr $ra # return false;
move_snake_in_grid__epilogue:
# tear down stack frame
lw $s7, ($sp)
lw $s6, 4($sp)
lw $s5, 8($sp)
lw $s4, 12($sp)
lw $s3, 16($sp)
lw $s2, 20($sp)
lw $s1, 24($sp)
lw $s0, 28($sp)
lw $ra, 32($sp)
addiu $sp, $sp, 36
li $v0, 1
jr $ra # return true;
########################################################################
# .TEXT <move_snake_in_array>
.text
move_snake_in_array:
# Arguments:
# - $a0: int new_head_row
# - $a1: int new_head_col
# Returns: void
#
# Frame: $ra, [$s0,$s1,$s2,$s3]
# Uses: [$a0,$a1,$s0,$s1,$s2,$s3]
# Clobbers: [$s0,$s1,$s2,$s3]
#
# Locals:
# - [$s0,$s1,$s2,$s3]
#
# Structure:
# move_snake_in_array
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
move_snake_in_array__prologue:
# set up stack frame
addiu $sp, $sp, -20
sw $ra, 16($sp)
sw $s0, 12($sp)
sw $s1, 8($sp)
sw $s2, 4($sp)
sw $s3, ($sp)
move_snake_in_array__body:
move $s0, $a0 # save arguments on to stack for later use
move $s1, $a1
lw $s3, snake_tail #int i = snake_tail
move_snake_array_loop:
blt $s3, 1, move_snake_array_loop_after # if i<1 end loop
addi $s2, $s3, -1 # int x = i-1;
lb $a0, snake_body_row($s2) # $a0 = snake_body_row[i - 1]
lb $a1, snake_body_col($s2) # $a1 = snake_body_col[i - 1]
move $a2, $s3 # $a2 = i
#set_snake_array(snake_body_row[i - 1],snake_body_col[i - 1],i)
jal set_snake_array
addi $s3, $s3, -1 # i--;
j move_snake_array_loop # goto loop;
move_snake_array_loop_after:
move $a0, $s0
move $a1, $s1
li $a2, 0 #set_snake_array(new_head_row, new_head_col, 0);
jal set_snake_array
move_snake_in_array__epilogue:
# tear down stack frame
lw $s3, ($sp)
lw $s2, 4($sp)
lw $s1, 8($sp)
lw $s0, 12($sp)
lw $ra, 16($sp)
addiu $sp, $sp, 20
jr $ra # return;
########################################################################
#### ####
#### STOP HERE ... YOU HAVE COMPLETED THE ASSIGNMENT! ####
#### ####
########################################################################
##
## The following is various utility functions provided for you.
##
## You don't need to modify any of the following. But you may find it
## useful to read through --- you'll be calling some of these functions
## from your code.
##
.data
last_direction:
.word EAST
rand_seed:
.word 0
input_direction__invalid_direction:
.asciiz "invalid direction: "
input_direction__bonk:
.asciiz "bonk! cannot turn around 180 degrees\n"
.align 2
input_direction__buf:
.space 2
########################################################################
# .TEXT <set_snake>
.text
set_snake:
# Args:
# - $a0: int row
# - $a1: int col
# - $a2: int body_piece
# Returns: void
#
# Frame: $ra, $s0, $s1
# Uses: $a0, $a1, $a2, $t0, $s0, $s1
# Clobbers: $t0
#
# Locals:
# - `int row` in $s0
# - `int col` in $s1
#
# Structure:
# set_snake
# -> [prologue]
# -> body
# -> [epilogue]
# Code:
set_snake__prologue:
# set up stack frame
addiu $sp, $sp, -12
sw $ra, 8($sp)
sw $s0, 4($sp)
sw $s1, ($sp)
set_snake__body:
move $s0, $a0 # $s0 = row
move $s1, $a1 # $s1 = col
jal set_snake_grid # set_snake_grid(row, col, body_piece);
move $a0, $s0
move $a1, $s1
lw $a2, snake_body_len
jal set_snake_array # set_snake_array(row, col, snake_body_len);
lw $t0, snake_body_len
addiu $t0, $t0, 1
sw $t0, snake_body_len # snake_body_len++;
set_snake__epilogue:
# tear down stack frame
lw $s1, ($sp)
lw $s0, 4($sp)
lw $ra, 8($sp)
addiu $sp, $sp, 12
jr $ra # return;
########################################################################
# .TEXT <set_snake_grid>
.text
set_snake_grid:
# Args:
# - $a0: int row
# - $a1: int col
# - $a2: int body_piece
# Returns: void
#
# Frame: None
# Uses: $a0, $a1, $a2, $t0
# Clobbers: $t0
#
# Locals: None
#
# Structure:
# set_snake
# -> body
# Code:
li $t0, N_COLS
mul $t0, $t0, $a0 # 15 * row
add $t0, $t0, $a1 # (15 * row) + col
sb $a2, grid($t0) # grid[row][col] = body_piece;
jr $ra # return;
########################################################################
# .TEXT <set_snake_array>
.text
set_snake_array:
# Args:
# - $a0: int row
# - $a1: int col
# - $a2: int nth_body_piece
# Returns: void
#
# Frame: None
# Uses: $a0, $a1, $a2
# Clobbers: None
#
# Locals: None
#
# Structure:
# set_snake_array
# -> body
# Code:
sb $a0, snake_body_row($a2) # snake_body_row[nth_body_piece] = row;
sb $a1, snake_body_col($a2) # snake_body_col[nth_body_piece] = col;
jr $ra # return;
########################################################################
# .TEXT <print_grid>
.text
print_grid:
# Args: void
# Returns: void
#
# Frame: None
# Uses: $v0, $a0, $t0, $t1, $t2
# Clobbers: $v0, $a0, $t0, $t1, $t2
#
# Locals:
# - `int i` in $t0
# - `int j` in $t1
# - `char symbol` in $t2
#
# Structure:
# print_grid
# -> for_i_cond
# -> for_j_cond
# -> for_j_end
# -> for_i_end
# Code:
li $v0, 11 # syscall 11: print_character
li $a0, '\n'
syscall # putchar('\n');
li $t0, 0 # int i = 0;
print_grid__for_i_cond:
bge $t0, N_ROWS, print_grid__for_i_end # while (i < N_ROWS)
li $t1, 0 # int j = 0;
print_grid__for_j_cond:
bge $t1, N_COLS, print_grid__for_j_end # while (j < N_COLS)
li $t2, N_COLS
mul $t2, $t2, $t0 # 15 * i
add $t2, $t2, $t1 # (15 * i) + j
lb $t2, grid($t2) # grid[(15 * i) + j]
lb $t2, symbols($t2) # char symbol = symbols[grid[(15 * i) + j]]
li $v0, 11 # syscall 11: print_character
move $a0, $t2
syscall # putchar(symbol);
addiu $t1, $t1, 1 # j++;
j print_grid__for_j_cond
print_grid__for_j_end:
li $v0, 11 # syscall 11: print_character
li $a0, '\n'
syscall # putchar('\n');
addiu $t0, $t0, 1 # i++;
j print_grid__for_i_cond
print_grid__for_i_end:
jr $ra # return;
########################################################################
# .TEXT <input_direction>
.text
input_direction:
# Args: void
# Returns:
# - $v0: int
#
# Frame: None
# Uses: $v0, $a0, $a1, $t0, $t1
# Clobbers: $v0, $a0, $a1, $t0, $t1
#
# Locals:
# - `int direction` in $t0
#
# Structure:
# input_direction
# -> input_direction__do
# -> input_direction__switch
# -> input_direction__switch_w
# -> input_direction__switch_a
# -> input_direction__switch_s
# -> input_direction__switch_d
# -> input_direction__switch_newline
# -> input_direction__switch_null
# -> input_direction__switch_eot
# -> input_direction__switch_default
# -> input_direction__switch_post
# -> input_direction__bonk_branch
# -> input_direction__while
# Code:
input_direction__do:
li $v0, 8 # syscall 8: read_string
la $a0, input_direction__buf
li $a1, 2
syscall # direction = getchar()
lb $t0, input_direction__buf
input_direction__switch:
beq $t0, 'w', input_direction__switch_w # case 'w':
beq $t0, 'a', input_direction__switch_a # case 'a':
beq $t0, 's', input_direction__switch_s # case 's':
beq $t0, 'd', input_direction__switch_d # case 'd':
beq $t0, '\n', input_direction__switch_newline # case '\n':
beq $t0, 0, input_direction__switch_null # case '\0':
beq $t0, 4, input_direction__switch_eot # case '\004':
j input_direction__switch_default # default:
input_direction__switch_w:
li $t0, NORTH # direction = NORTH;
j input_direction__switch_post # break;
input_direction__switch_a:
li $t0, WEST # direction = WEST;
j input_direction__switch_post # break;
input_direction__switch_s:
li $t0, SOUTH # direction = SOUTH;
j input_direction__switch_post # break;
input_direction__switch_d:
li $t0, EAST # direction = EAST;
j input_direction__switch_post # break;
input_direction__switch_newline:
j input_direction__do # continue;
input_direction__switch_null:
input_direction__switch_eot:
li $v0, 17 # syscall 17: exit2
li $a0, 0
syscall # exit(0);
input_direction__switch_default:
li $v0, 4 # syscall 4: print_string
la $a0, input_direction__invalid_direction
syscall # printf("invalid direction: ");
li $v0, 11 # syscall 11: print_character
move $a0, $t0
syscall # printf("%c", direction);
li $v0, 11 # syscall 11: print_character
li $a0, '\n'
syscall # printf("\n");
j input_direction__do # continue;
input_direction__switch_post:
blt $t0, 0, input_direction__bonk_branch # if (0 <= direction ...
bgt $t0, 3, input_direction__bonk_branch # ... && direction <= 3 ...
lw $t1, last_direction # last_direction
sub $t1, $t1, $t0 # last_direction - direction
abs $t1, $t1 # abs(last_direction - direction)
beq $t1, 2, input_direction__bonk_branch # ... && abs(last_direction - direction) != 2)
sw $t0, last_direction # last_direction = direction;
move $v0, $t0
jr $ra # return direction;
input_direction__bonk_branch:
li $v0, 4 # syscall 4: print_string
la $a0, input_direction__bonk
syscall # printf("bonk! cannot turn around 180 degrees\n");
input_direction__while:
j input_direction__do # while (true);
########################################################################
# .TEXT <get_d_row>
.text
get_d_row:
# Args:
# - $a0: int direction
# Returns:
# - $v0: int
#
# Frame: None
# Uses: $v0, $a0
# Clobbers: $v0
#
# Locals: None
#
# Structure:
# get_d_row
# -> get_d_row__south:
# -> get_d_row__north:
# -> get_d_row__else:
# Code:
beq $a0, SOUTH, get_d_row__south # if (direction == SOUTH)
beq $a0, NORTH, get_d_row__north # else if (direction == NORTH)
j get_d_row__else # else
get_d_row__south:
li $v0, 1
jr $ra # return 1;
get_d_row__north:
li $v0, -1
jr $ra # return -1;
get_d_row__else:
li $v0, 0
jr $ra # return 0;
########################################################################
# .TEXT <get_d_col>
.text
get_d_col:
# Args:
# - $a0: int direction
# Returns:
# - $v0: int
#