-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap_project.asm
1087 lines (906 loc) · 21.4 KB
/
bitmap_project.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
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
# Bitmap Homework 4
# Alexis Kaufman
# March 23, 2022
#
# Usage Instructions:
# set pixel dim to 4x4
# set display dim to 512x512
# set Base address for display to 0x10040000 (heap)
# Connect to MIPS and run
# set up some constants
# width of screen in pixels
# 512 / 4 = 128
.eqv WIDTH 128
# height of screen in pixels
.eqv HEIGHT 128
# move distance for arrow
.eqv ARROWMOVE 43
# mem loc
.eqv MEM 0x10040000
# color distance for arrow
.eqv COLORMOVE 18
# padding ammt
.eqv PADDING 5
# colors
.eqv RED 0x00FF0000
.eqv ORANGE 0x00EBAB34
.eqv GREEN 0x0000FF00
.eqv BLUE 0x000000FF
.eqv YELLOW 0x00FFFF00
.eqv MAGENTA 0x00FF00FF
.eqv BLACK 0x00000000
.eqv WHITE 0x00FFFFFF
.data
rainbow: .word RED, ORANGE, YELLOW, GREEN, BLUE, MAGENTA
rainbowLen: .word 6
gameBoard: .word 0, 0, 0, 0, 0, 0, 0, 0, 0
playerOneColor: .word WHITE
playerTwoColor: .word WHITE
newLine: .asciiz "\n"
playerOneWon: .asciiz "Player one (X's) has won! Congrats!"
playerTwoWon: .asciiz "Player two (O's) has won! Congrats!"
tieMessage: .asciiz "The game has ended in a draw!"
# REGISTERS USED:
# a1-a3 used as arguments for the draw_pixel function
# t0 used as a loop counter in various functions
# t6 holds for input
# s1 stores address of a pixel
# s2 processes input
# s3 stores program input status:
# 0 means top needs to be selected
# 1 means side needs to be selected
# 2 means color needs to be selected for player 1
# 3 means color needs to be selected for player 2
# s4 stores player status:
# 0 means it's player 1's turn
# 1 means it's player 2's turn
# s5 stores TOP arrow position
# 1 - 3, or # 1 - 6
# s6 stores SIDE arrow position
# 1 - 3
# s7 stores total play counter
# 1 - 9
# t1-t4 # Used in nested arithmetic
# v0 and a0 used for syscalls
.text
.macro draw_color_selector
lw $t1, rainbowLen
addi $t1, $t1, 1
li $a1, WIDTH
div $a1, $a1, $t1
subi $a1, $a1, 3
li $a2, HEIGHT # a2 = height
srl $a2, $a2, 1
subi $a2, $a2, 9
li $a3, WHITE # a3 = WHITE
li $a0, 0 # down triangle
jal draw_triangle # draw initial down_triangle above red box in WHITE color
.end_macro
draw_color_options_1:
# initialize save values
li $s3, 2
li $s4, 0
li $s5, 1
jal draw_color_options
draw_color_selector
j check_in
draw_color_options_2:
# modify save values
li $s3, 3
li $s4, 1
li $s5, 1
jal clear_board
jal draw_color_options
draw_color_selector
j check_in
draw_game_board:
jal reset_player_one # change status to start game
jal clear_board
# start game board
li $a1, WIDTH
srl $a1, $a1, 1
sub $a1, $a1, 2
li $a2, 5 # a2 = 5
li $s4, 0
li $s5, 2 # middle
jal set_a3_player_color
li $a0, 0 # down triangle
jal draw_triangle # draw initial down_triangle at (CENTER, 5) in player color
li $s3, 0 # Game starts with player one on top side
li $s7, 0 # Play count starts at 0
jal draw_down_lines
jal draw_side_lines
j check_in
##############################################
# Macros
.macro restore_pos
move $a1, $t3 # Restore a1
move $a2, $t4 # Restore a2
.end_macro
.macro restore_ra
lw $ra, ($sp) # obtain saved stack value
addi $sp, $sp, 4 # move up by 4
.end_macro
.macro save_ra
addi $sp, $sp, -4 # move up by 4 bytes in the stack
sw $ra, 0($sp) # overwrite value at sp with word at ra
.end_macro
.macro save_pos_ra
# Save some values
save_ra
move $t3, $a1
move $t4, $a2
.end_macro
.macro restore_pos_ra
# Restore some values
restore_ra
restore_pos
.end_macro
.macro check_space_unoccupied
mul $t5, $s5, 4
subi $t5, $t5, 4 # t5 = 4($s5) - 4
subi $t6, $s6, 1
mul $t6, $t6, 12 # t6 = ($s6 - 1)*12
add $t7, $t5, $t6 # t7 = t5 + t6
la $t8, gameBoard # t8 = address of spot 1,1
add $t8, $t8, $t7 # add calculated offset
lw $t9, ($t8) # load value at address into t9
bne $t9, $0, check_in # space is occupied, return to check_in
.end_macro
.macro occupy_space
mul $t5, $s5, 4
subi $t5, $t5, 4 # t5 = 4($s5) - 4
subi $t6, $s6, 1
mul $t6, $t6, 12 # t6 = ($s6 - 1)*12
add $t7, $t5, $t6 # t7 = t5 + t6
la $t8, gameBoard # t8 = address of spot 1,1
add $t8, $t8, $t7 # add calculated offset
addi $t9, $s4, 1
sw $t9, ($t8) # load player value (1 or 2) into address at $t8
addi $s7, $s7, 1 # increment play count
.end_macro
.macro clear_top_arrows
li $a0, 0
li $a1, WIDTH
srl $a1, $a1, 1
sub $a1, $a1, 2
li $a2, 5 # a2 = 5
li $a3, BLACK
jal draw_triangle
li $a0, 0
li $a1, WIDTH
srl $a1, $a1, 1
sub $a1, $a1, 2
li $a2, 5 # a2 = 5
sub $a1, $a1, ARROWMOVE
li $a3, BLACK
jal draw_triangle
li $a0, 0
li $a1, WIDTH
srl $a1, $a1, 1
sub $a1, $a1, 2
li $a2, 5 # a2 = 5
add $a1, $a1, ARROWMOVE
li $a3, BLACK
jal draw_triangle
.end_macro
.macro clear_side_arrows
li $a0, 1
li $a2, HEIGHT
srl $a2, $a2, 1
sub $a2, $a2, 2 # a2 = middle
li $a1, 5 # a1 = 5
li $a3, BLACK
jal draw_triangle
li $a0, 1
li $a2, HEIGHT
srl $a2, $a2, 1
sub $a2, $a2, 2 # a2 = middle
li $a1, 5 # a1 = 5
sub $a2, $a2, ARROWMOVE
li $a3, BLACK
jal draw_triangle
li $a0, 1
li $a2, HEIGHT
srl $a2, $a2, 1
sub $a2, $a2, 2 # a2 = middle
li $a1, 5 # a1 = 5
add $a2, $a2, ARROWMOVE
li $a3, BLACK
jal draw_triangle
.end_macro
##############################################
reset_player_one: # to be called with jal; resets multiple registers for player 1's beginning of turn
li $s3, 0
li $s4, 1 # set player to player 2
li $s5, 2
li $s6, 2
jr $ra
reset_player_two: # to be called with jal; resets multiple registers for player 2's beginning of turn
li $s3, 0
li $s4, 0 # set player to player 1
li $s5, 2 # reset top arrow to middle
li $s6, 2 # reset side arrow to middle
jr $ra
##############################################
set_a3_player_color: # to be called with jal; sets a3 to respective color for current player
bne $s4, $zero, player_two_a3
lw $a3, playerOneColor
jr $ra
player_two_a3:
lw $a3, playerTwoColor
jr $ra
##############################################
check_winner: # to be called with jal
save_pos_ra
la $t3, gameBoard # t3 = address of first grid point
lw $t4, ($t3) # t4 = value at first checkpoint, initally first grid pt
li $t5, 0
li $t6, 0
j check_verticals
.macro calc_win_helper (%depth_value)
mul $t8, $t6, %depth_value # t8 = depth * depth_value
add $t7, $t5, $t8 # t7 = starting value + calculated offset
add $t9, $t3, $t7 # t9 = original memory location + t7
lw $t9, ($t9) # load value of address at t9 into t9
.end_macro
return_to_loop:
jr $ra
##############################################
check_verticals:
jal check_verticals_loop # with initial vals
addi $t5, $t5, 4
lw $t4, 4($t3)
li $t6, 0
jal check_verticals_loop
addi $t5, $t5, 4
lw $t4, 8($t3)
li $t6, 0
jal check_verticals_loop
j check_horizontals
# $t5 = starting value for top column (0, 4, 8)
# $t6 = current height (0, 1, 2)
check_verticals_loop:
beq $t6, 3, full_match_found
calc_win_helper (12)
bne $t9, $t4, return_to_loop # different found
beq $t9, $zero, return_to_loop # empty found
addi $t6, $t6, 1
j check_verticals_loop
#############################################
check_horizontals:
li $t5, 0
li $t6, 0
jal check_horizontal_loop # with initial vals
addi $t5, $t5, 12
lw $t4, 12($t3)
li $t6, 0
jal check_horizontal_loop
addi $t5, $t5, 12
lw $t4, 24($t3)
li $t6, 0
jal check_horizontal_loop
j check_diagonals
# $t5 = starting value for side column (0, 12, 24)
# $t6 = current depth (0, 1, 2)
check_horizontal_loop:
beq $t6, 3, full_match_found
calc_win_helper (4)
bne $t9, $t4, return_to_loop # different found
beq $t9, $zero, return_to_loop # empty found
addi $t6, $t6, 1
j check_horizontal_loop
return_to_horizontals:
jr $ra
#############################################
check_diagonals:
li $t5, 0
lw $t4, 0($t3)
li $t6, 0
jal check_left_diagonals_loop # with initial vals
li $t5, 8
lw $t4, 8($t3)
li $t6, 0
jal check_right_diagonals_loop
j done_checking_winner
# $t5 = starting value for top left spot (0, 0)
# $t6 = current depth (0, 1, 2)
check_left_diagonals_loop:
beq $t6, 3, full_match_found
calc_win_helper (16)
bne $t9, $t4, return_to_loop # different found
beq $t9, $zero, return_to_loop # empty found
addi $t6, $t6, 1
j check_left_diagonals_loop
# $t5 = starting value for top right spot (0, 8)
# $t6 = current depth (0, 1, 2)
check_right_diagonals_loop:
beq $t6, 3, full_match_found
calc_win_helper (8)
bne $t9, $t4, return_to_loop # different found
beq $t9, $zero, return_to_loop # empty found
addi $t6, $t6, 1
j check_right_diagonals_loop
return_to_diagonals:
jr $ra
#############################################
done_checking_winner:
beq $s7, 9, game_tied
j done_return_nested
full_match_found: #END GAME
beq $t9, 1, player_one_won
beq $t9, 2, player_two_won
j exit # if error occurs
player_one_won:
li $v0, 55
la $a0, playerOneWon
li $a1, 1
syscall
j exit
player_two_won:
li $v0, 55
li $a1, 1
la $a0, playerTwoWon
syscall
j exit
game_tied:
li $v0, 55
li $a1, 1
la $a0, tieMessage
syscall
j exit
##############################################
check_in:
# check for input
lw $t6, 0xffff0000 #t6 holds if input available
beq $t6, 0, check_in #If no input, keep displaying
# process input
lw $s2, 0xffff0004
beq $s2, 32, process_chosen # input space
beq $s2 119, up # input w
beq $s2, 115, down # input s
beq $s2, 97, left # input a
beq $s2, 100, right # input d
beq $s2, 27, exit # input esc
j check_in
process_chosen:
beq $s3, 2, player_one_color_chosen
beq $s3, 3, player_two_color_chosen
# logic to determine if it is player one's turn, and they have selected top side
seq $t1, $s4, 0
seq $t2, $s3, $zero
and $t3, $t1, $t2
beq $t3, 1, player_to_side
# logic to determine if it is player one's turn, and they have selected the side side
seq $t1, $s4, 0
seq $t2, $s3, 1
and $t3, $t1, $t2
beq $t3, 1, player_one_chosen_spot
# logic to determine if it is player two's turn, and they have selected the top side
seq $t1, $s4, 1
seq $t2, $s3, 0
and $t3, $t1, $t2
beq $t3, 1, player_to_side
# logic to determine if it is player two's turn, and they have selected the side side
seq $t1, $s4, 1
seq $t2, $s3, 1
and $t3, $t1, $t2
beq $t3, 1, player_two_chosen_spot
j check_in
player_to_side:
li $s3, 1 # set side needs to be selected
li $a0, 1 # right pointing arrow
li $a2, HEIGHT
srl $a2, $a2, 1
sub $a2, $a2, 2 # a2 = middle
li $a1, 5 # a1 = 5
jal set_a3_player_color
jal draw_triangle
li $s6, 2 # set arrow position marker to middle
j check_in
player_to_top:
li $a1, WIDTH
srl $a1, $a1, 1
sub $a1, $a1, 2
li $a2, 5 # a2 = 5
li $s5, 2 # middle
jal set_a3_player_color
li $a0, 0 # down triangle
jal draw_triangle # draw initial down_triangle at (CENTER, 5) in player color
j check_in
player_one_chosen_spot:
check_space_unoccupied
occupy_space
li $a2, HEIGHT
srl $a2, $a2, 1 # a2 = middle height
subi $t5, $s6, 2
mul $t5, $t5, ARROWMOVE
add $a2, $a2, $t5
li $a1, WIDTH
srl $a1, $a1, 1 # a2 = middle width
subi $t5, $s5, 2
mul $t5, $t5, ARROWMOVE
add $a1, $a1, $t5
jal set_a3_player_color
jal draw_centered_x
jal reset_player_one
clear_top_arrows
clear_side_arrows
jal check_winner
j player_to_top
player_two_chosen_spot:
check_space_unoccupied
occupy_space
li $a2, HEIGHT
srl $a2, $a2, 1 # a2 = middle height
subi $t5, $s6, 2
mul $t5, $t5, ARROWMOVE
add $a2, $a2, $t5
li $a1, WIDTH
srl $a1, $a1, 1 # a2 = middle width
subi $t5, $s5, 2
mul $t5, $t5, ARROWMOVE
add $a1, $a1, $t5
jal set_a3_player_color
jal draw_centered_o
jal reset_player_two
clear_top_arrows
clear_side_arrows
jal check_winner
j player_to_top
up:
beq $s3, $0, check_in
beq $s6, 1, check_in
li $a3, BLACK
li $a0, 1 # right triangle
jal draw_triangle
addi $a2, $a2, -ARROWMOVE
addi $s6, $s6, -1 # update s6 to move one spot up
jal set_a3_player_color
li $a0, 1 # right triangle
jal draw_triangle
j check_in
down:
beq $s3, $0, check_in
beq $s6, 3, check_in
li $a3, BLACK
li $a0, 1 # right triangle
jal draw_triangle
addi $a2, $a2, ARROWMOVE
addi $s6, $s6, 1 # update s6 to move one spot down
jal set_a3_player_color
li $a0, 1 # right triangle
jal draw_triangle
j check_in
left:
beq $s3, 2, color_left
beq $s3, 3, color_left
beq $s5, 1, check_in
bne $s3, $0, check_in
li $a3, BLACK
li $a0, 0 # down triangle
jal draw_triangle
addi $a1, $a1, -ARROWMOVE
addi $s5, $s5, -1 # update s5 to move one spot left
jal set_a3_player_color
li $a0, 0 # down triangle
jal draw_triangle
j check_in
right:
beq $s3, 2, color_right
beq $s3, 3, color_right
beq $s5, 3, check_in
bne $s3, $0, check_in
li $a3, BLACK
li $a0, 0 # down triangle
jal draw_triangle
addi $a1, $a1, ARROWMOVE
addi $s5, $s5, 1 # update s5 to move one spot right
jal set_a3_player_color
li $a0, 0 # down triangle
jal draw_triangle
j check_in
color_left:
beq $s5, 1, check_in
li $a3, BLACK
li $a0, 0 # down triangle
jal draw_triangle
addi $a1, $a1, -COLORMOVE
addi $s5, $s5, -1 # update s5 to move one spot left
li $a3, WHITE
li $a0, 0 # down triangle
jal draw_triangle
j check_in
color_right:
beq $s5, 6, check_in
li $a3, BLACK
li $a0, 0 # down triangle
jal draw_triangle
addi $a1, $a1, COLORMOVE
addi $s5, $s5, 1 # update s5 to move one spot left
li $a3, WHITE
li $a0, 0 # down triangle
jal draw_triangle
j check_in
player_one_color_chosen:
la $t5, rainbow
addi $t6, $s5, -1
mul $t6, $t6, 4
add $t5, $t6, $t5
lw $t6, ($t5)
sw $t6, playerOneColor
j draw_color_options_2
player_two_color_chosen:
la $t5, rainbow
addi $t6, $s5, -1
mul $t6, $t6, 4
add $t5, $t6, $t5
lw $t6, ($t5)
lw $t7, playerOneColor
beq $t7, $t6, check_in
sw $t6, playerTwoColor
j draw_game_board
######################################
# draw a 5 x 3 triangle, starting at given x and y
# $a0 = direction to draw triangle;
# 0 = down
# 1 = right
# $a1 = x
# $a2 = y
# $a3 = color
draw_triangle:
save_pos_ra
li $t0, 0
# start looping
beq $a0, $zero, down_triangle_loop_top
bne $a0, $zero, right_triangle_loop_top
#############################################
# down-pointing triangle functions
reset_middle_helper_down:
subi $a1, $a1, 5
li $t0, 1
addi $a2, $a2, 1
j down_triangle_loop_middle
reset_bottom_helper_down:
subi $a1, $a1, 3
li $t0, 3
addi $a2, $a2, 1
j down_triangle_loop_bottom
###
done_drawing_down_triangle:
# return
restore_pos_ra
jr $ra
# $t0 = current length
down_triangle_loop_top:
jal draw_pixel
addi $a1, $a1, 1
beq $t0, 4, reset_middle_helper_down
addi $t0, $t0, 1
j down_triangle_loop_top
# $t0 = current length
down_triangle_loop_middle:
addi $a1, $a1, 1
jal draw_pixel
beq $t0, 3, reset_bottom_helper_down
addi $t0, $t0, 1
j down_triangle_loop_middle
down_triangle_loop_bottom:
addi $a1, $a1, 2
jal draw_pixel
j done_drawing_down_triangle
######################################
# right-pointing triangle functions
reset_middle_helper_right:
subi $a2, $a2, 5
li $t0, 1
addi $a1, $a1, 1
j right_triangle_loop_middle
reset_bottom_helper_right:
subi $a2, $a2, 3
li $t0, 3
addi $a1, $a1, 1
j right_triangle_loop_bottom
###
done_drawing_right_triangle:
# return
restore_pos_ra
jr $ra
# $t0 = current length
right_triangle_loop_top:
jal draw_pixel
addi $a2, $a2, 1
beq $t0, 4, reset_middle_helper_right
addi $t0, $t0, 1
j right_triangle_loop_top
# $t0 = current length
right_triangle_loop_middle:
addi $a2, $a2, 1
jal draw_pixel
beq $t0, 3, reset_bottom_helper_right
addi $t0, $t0, 1
j right_triangle_loop_middle
right_triangle_loop_bottom:
addi $a2, $a2, 2
jal draw_pixel
j done_drawing_right_triangle
######################################
# draw the games's vertical borders
# $t0 = current length
# $t1 = length to draw
# $t2 = temporary arithmetic operand
# $t3 = previous x
# $t4 = previous y
draw_down_lines:
save_pos_ra
# Set up arguments for draw_pixel
li $a1, WIDTH
div $a1, $a1, 3
li $a2, PADDING
li $a3, WHITE
# Set up arguments for left line
li $t0, 0
li $t1, HEIGHT
li $t2, PADDING
mul $t2, $t2, 2
sub $t1, $t1, $t2
jal draw_vertical_line
# Set up arguments for right line
li $t0, 0
li $a1, WIDTH
mul $a1, $a1, 2
div $a1, $a1, 3
li $a2, PADDING
jal draw_vertical_line
# Restore values
restore_pos
j done_return_nested
draw_vertical_line:
save_ra
j loop_vertical_line
loop_vertical_line:
bge $t0, $t1, done_return_nested
jal draw_pixel
addi $a2, $a2, 1
addi $t0, $t0, 1
j loop_vertical_line
######################################
# draw the game's horizontal borders
# $t0 = current length
# $t1 = length to draw
# $t2 = temporary arithmetic operand
# $t3 = previous x
# $t4 = previous y
draw_side_lines:
# Save some values
save_pos_ra
# Set up arguments for draw_pixel
li $a1, PADDING
li $a2, HEIGHT
div $a2, $a2, 3
li $a3, WHITE
# Set up arguments for top line
li $t0, 0
li $t1, WIDTH
li $t2, PADDING
mul $t2, $t2, 2
sub $t1, $t1, $t2
jal draw_line_horizontal
# Set up arguments for bottom line
li $t0, 0
li $a2, HEIGHT
mul $a2, $a2, 2
div $a2, $a2, 3
li $a1, PADDING
jal draw_line_horizontal
# Restore values
restore_pos
j done_return_nested
draw_line_horizontal:
save_ra
j loop_horizontal_line
loop_horizontal_line:
bge $t0, $t1, done_return_nested
jal draw_pixel
addi $a1, $a1, 1
addi $t0, $t0, 1
j loop_horizontal_line
######################################
# t0 - iteration number
# t1 - times to iterate
# t2 - address of first array value
# t3 - previous x to save
# t4 - previous y to save
# t5-t6 - used in arithmetic
# $t7 - holds color NOT to draw
draw_color_options:
li $t0, 1
lw $t1, rainbowLen
addi $t1, $t1, 1
la $t2, rainbow
# save values
save_pos_ra
j draw_rainbow_loop
draw_rainbow_loop:
# check condition:
beq $t0, $t1, done_return_nested
# determine y value
li $a2, HEIGHT
srl $a2, $a2, 1
# determine x value
li $t5, WIDTH
div $t5, $t5, $t1
mul $t5, $t5, $t0
move $a1, $t5
# determine color value
lw $a3, ($t2)
addi $t2, $t2, 4
# increment values
addi $t0, $t0, 1
lw $t7, playerOneColor
beq $t7, $a3, draw_rainbow_loop
# draw pixels
jal draw_centered_square
j draw_rainbow_loop
######################################
# Draws a centered square at current x,y with a size of 5 x 5.
# Should be called with a jal
# t6 = top iterator (-2 to 2)
# t7 = side iterator (-2 to 2)
# t5 = used in arithmetic
draw_centered_square:
save_pos_ra # save position and return
li $t6, -2
li $t7, -2
j loop_draw_centered_square
loop_draw_centered_square:
and $t5, $t6, $t7
beq $t5, 2, done_return_nested # if both iterators are = 2
add $a1, $t3, $t6
add $a2, $t4, $t7
addi $t6, $t6, 1
jal draw_pixel
beq $t6, 2, square_increment_height
j loop_draw_centered_square
square_increment_height:
li $t6, -2
addi $t7, $t7, 1
j loop_draw_centered_square
######################################
# Draws a centered x at current x,y with a size of 9 x 9.
# Should be called with a jal
# Set before calling:
# t3 - incoming x to save
# t4 - incoming y to save
# Used in function:
# t6 = top iterator (-6 to 6)
# t7 = side iterator (-6 to 6)
# t5 = used in arithmetic
draw_centered_x:
save_pos_ra # save position and return
li $t6, -6
li $t7, -6
j loop_draw_centered_x
loop_draw_centered_x:
and $t5, $t6, $t7
beq $t5, 7, x_draw_other # if both iterators are = 7
add $a1, $t3, $t6
add $a2, $t4, $t7
addi $t6, $t6, 1
addi $t7, $t7, 1
jal draw_pixel
j loop_draw_centered_x
x_draw_other:
li $t6, 6
li $t7, -6
j loop_draw_other_x
loop_draw_other_x:
beq $t7, 7, done_return_nested # if side iterator is = 7
add $a1, $t3, $t6
add $a2, $t4, $t7
addi $t6, $t6, -1
addi $t7, $t7, 1
jal draw_pixel
j loop_draw_other_x
######################################
draw_centered_o:
save_pos_ra
# start at upper left-hand corner
li $t5, -7
li $t6, -7
# move to the right
li $t7, 1
li $t8, 0
li $t0, 0
jal loop_centered_o
# move down
li $t7, 0
li $t8, 1
li $t0, 0
jal loop_centered_o
# move to the left
li $t7, -1
li $t8, 0
li $t0, 0
jal loop_centered_o
# move up
li $t7, 0
li $t8, -1
li $t0, 0
jal loop_centered_o
j done_return_nested
# call with intended starting x,y in $t5, $t6
# call with intended x-direction, y-direction in $t7, $t8