-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinalProjectP.asm
1208 lines (884 loc) · 34.5 KB
/
FinalProjectP.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
.data
### Provided stack data ###
stack_beg: .word 0 : 100
stack_end:
### Board Data
BoardPositions: .word 0:9 # stores the occupied/available positions on the board
UserPositions: .word 0:9 # stores the user's current positions on the board
MachinePositions: .word 0:9 # stores the machine's current positions on the board
# Ascii messages ###
user_goes_first_message: .asciiz "Based on the (pseudo) random drawing, you go first! \n"
machine_goes_first_message: .asciiz "Based on the (pseudo) random drawing, the machine goest first... \n"
user_object_type_prompt: .asciiz "Would you like to be O's or X's? (0 for 0: O's and 1 for X's) \n"
user_input_column_prompt: .asciiz "Enter the column you wish place your X or O in: \n"
user_input_row_prompt: .asciiz "Enter the row you wish place your X or O in: \n"
postion_occupied_message: .asciiz "Sorry, that position is already occupied! \n"
lose_message: .asciiz "Sorry, you lost. Try again sometime! \n"
win_message: .asciiz "Congratulations, you win! \n"
tie_message: .asciiz "Looks like a tie... try again sometime! \n"
bad_object_input_message: .asciiz "Please enter a valid object option! \n"
bad_row_column_input_message: .asciiz "Please enter row and column values that are between 1 and 3 \n"
### Lookup tables ###
RowAndColumnToBoardNumber: .word 1, 1, 0 # row | column | board number spot
.word 1, 2, 1 # row | column | board number spot
.word 1, 3, 2 # row | column | board number spot
.word 2, 1, 3 # row | column | board number spot
.word 2, 2, 4 # row | column | board number spot
.word 2, 3, 5 # row | column | board number spot
.word 3, 1, 6 # row | column | board number spot
.word 3, 2, 7 # row | column | board number spot
.word 3, 3, 8 # row | column | board number spot
WinningCombinations: .word 0, 1, 2
.word 0, 3, 6
.word 0, 4, 8
.word 1, 4, 7
.word 2, 5, 8
.word 2, 4, 6
.word 3, 4, 5
.word 6, 7, 8
# start permuations for machine position selection
.word 0, 2, 1
.word 1, 0, 2
.word 1, 2, 0
.word 2, 0, 1
.word 2, 1, 0
.word 0, 6, 3
.word 3, 0, 6
.word 3, 6, 0
.word 6, 0, 3
.word 6, 3, 0
.word 0, 8, 4
.word 4, 0, 8
.word 4, 8, 0
.word 8, 0, 4
.word 8, 4, 0
.word 1, 7, 4
.word 4, 1, 7
.word 4, 7, 1
.word 7, 1, 4
.word 7, 4, 1
.word 2, 8, 5
.word 5, 2, 8
.word 5, 8, 2
.word 8, 2, 5
.word 8, 5, 2
.word 2, 6, 4
.word 4, 2, 6
.word 4, 6, 2
.word 6, 2, 4
.word 6, 4, 2
.word 3, 5, 4
.word 4, 3, 5
.word 4, 5, 3
.word 5, 3, 4
.word 5, 4, 3
.word 6, 8, 7
.word 7, 6, 8
.word 7, 8, 6
.word 8, 6, 7
.word 8, 7, 6
OTable: .word 1, 1, 43, 12 # row | column | x coordinate start | y coordinate start
.word 1, 2, 129, 12 # row | column | x coordinate start | y coordinate start
.word 1, 3, 215, 12 # row | column | x coordinate start | y coordinate start
.word 2, 1, 43, 98 # row | column | x coordinate start | y coordinate start
.word 2, 2, 129, 98 # row | column | x coordinate start | y coordinate start
.word 2, 3, 215, 98 # row | column | x coordinate start | y coordinate start
.word 3, 1, 43, 184 # row | column | x coordinate start | y coordinate start
.word 3, 2, 129, 184 # row | column | x coordinate start | y coordinate start
.word 3, 3, 215, 184 # row | column | x coordinate start | y coordinate start
XTable: .word 1, 1, 12, 12 # row | column | x coordinate start | y coordinate start
.word 1, 2, 98, 12 # row | column | x coordinate start | y coordinate start
.word 1, 3, 184, 12 # row | column | x coordinate start | y coordinate start
.word 2, 1, 12, 98 # row | column | x coordinate start | y coordinate start
.word 2, 2, 98, 98 # row | column | x coordinate start | y coordinate start
.word 2, 3, 184, 98 # row | column | x coordinate start | y coordinate start
.word 3, 1, 12, 184 # row | column | x coordinate start | y coordinate start
.word 3, 2, 98, 184 # row | column | x coordinate start | y coordinate start
.word 3, 3, 184, 184 # row | column | x coordinate start | y coordinate start
SquaresTable: .word 0
.word 1
.word 4
.word 9
.word 16
.word 25
.word 36
.word 49
.word 64
.word 81
.word 100
.word 121
.word 144
.word 169
.word 196
.word 225
.word 256
.word 289
.word 324
.word 361
.word 400
.word 441
.word 484
.word 529
.word 576
.word 625
.word 676
.word 729
.word 784
.word 841
.word 900
.word 961
.word 1024
ColorTable: .word 'G', 0x0000ff00 # Green
.word 'Y', 0x00ffff00 # Yellow
.word 'W', 0x00ffffff # White
.word '0', 0x00000000 # Black
.text
main:
init:
la $sp, stack_end
la $s6, 0x10040000 # base address of display
li $s0, 0 # assume user will use O's but update if neccesary
li $s1, 0 # number of positions user currently occupying
li $s2, 0 # number of positions machine currently occupying
jal drawBoard
jal getRandomNumber # $v0 contains the ranodom number [0,1]
move $s3, $v0
prePlay:
li $v0, 4 # print string
la $a0, user_goes_first_message
beq $s3, 0, userGoesFirst
la $a0, machine_goes_first_message
syscall
b machinePlay # if the random number is 1, machine goes first
userGoesFirst:
syscall
play:
userPlay:
# $v0 and $v1 will have the user's row/col input respetively after this method
jal getUserInput
move $a0, $v0
move $a1, $v1
jal checkPositionIsValidAndUpdatePositions
beq $v0, 1, play
# $v0 will have the x-coordinate memory address and the y-coordinate address is one word down
li $a2, 0 # user indicator
jal convertInputToCoords
lw $a0, 0($v0)
lw $a1, 4($v0)
li $a2, 0 # user indicator
jal drawXOrO
li $a1, 0 # user indicator
li $a3, 0 # checkType indicator (winner check)
jal checkIfWinner
machinePlay:
# $v0 will have the x-coordinate memory address and the y-coordinate address is one word down
jal getMachineInput
lw $a0, ($v0)
lw $a1, 4($v0)
li $a2, 1 # machine indicator
jal convertInputToCoords
lw $a0, 0($v0)
lw $a1, 4($v0)
li $a2, 1 # machine indicator
jal drawXOrO
li $a1, 1 # machine indicator
li $a3, 0 # checkType indicator (winner check)
jal checkIfWinner
b play
# generates random number
# argument: none
# return: (int) $v0 contains the random number
getRandomNumber:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
# get seed
li $a0, 0 # generator number/id
li, $v0, 30 # sys call for system time
syscall
move $t0, $a0 # store the LSB returned by sys time call to $t0
# get random number
li $a0, 0 # generator number/id
move $a1, $t0 # seed for generator
li, $v0, 40 # seed random number generator
syscall
li $a1, 2 # upper bound of random number
li $v0, 42 # generate random number
syscall
finishRandomNumberRoutine:
move $v0, $a0 # move the random number to the proper return regsiter
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# get the user's input
# argument: none
# return: $s0 = user's chosen object type, $v0 = user's row input, $v1 = user's col inpout
getUserInput:
# stack stuff
subi $sp, $sp, 8 # make room for the saved items below
sw $ra, 0($sp) # store $ra
add $t0, $s1, $s2 # used in check below to see if this is the first iteration
bnez $t0, getRowAndColumnInput
li $v0, 4
la $a0, user_object_type_prompt
syscall
li $v0, 5 # read int from user (read value goes into $v0)
syscall
beqz $v0, finishGettingUserObjectInput
beq $v0, 1, finishGettingUserObjectInput
li $v0, 4 # print string
la $a0, bad_object_input_message
syscall
b getUserInput
finishGettingUserObjectInput:
move $s0, $v0 # store user object choice input in $s0
getRowAndColumnInput:
li $v0, 4 # print string
la $a0, user_input_row_prompt # prompt address user for column
syscall
li $v0, 5 # read int from user (read value goes into $v0)
syscall
bgt $v0, 3, badInputGiven
bltz $v0, badInputGiven
sw $v0, 4($sp) # store $v0 before it's changed below
li $v0, 4 # print string
la $a0, user_input_column_prompt # prompt address user for row
syscall
li $v0, 5 # read int from user (read value goes into $v0)
syscall
bgt $v0, 3, badInputGiven
bltz $v0, badInputGiven
move $v1, $v0 # store user row input in $t1
lw $ra, 0($sp) # restore $ra
lw $v0, 4($sp) # restore $v0
addi $sp, $sp, 8 # set the $sp back to its original state
jr $ra
badInputGiven:
li $v0, 4 # print string
la $a0, bad_row_column_input_message
syscall
b getRowAndColumnInput
# get the machine's input
# argument: none
# return: $v0 = pointer to machine's row input (next word is the col input)
getMachineInput:
# stack stuff
subi $sp, $sp, 16 # make room for the saved items below
sw $ra, 0($sp) # store $ra
bnez $s2, findBestPositionPrep
# this is the first time the machine is choosing, so take either
# the center spot, board number 4 or the first spot board number 1
# reason being is that the center spot is involved in 4 winning combos and the 1st spot is involved in 3,
# and those are the highest winning combo amounts for any given positions
la $t7, BoardPositions
li $v1, 4 # set the selected board position to the center spot
lw $t7, 16($t7) # center spot of board
beqz $t7, selectedMachinePostionToRowAndColumn
li $v1, 0 # if the center spot is taken, then choose th first spot
b selectedMachinePostionToRowAndColumn
findBestPositionPrep:
li $t6, 2 # counter
li $a1, 1 # first check for winning machine positions (offensive)
findBestPositionLoop:
move $a3, $t6 # checkType arg for method
sw $t6, 8($sp) # store $t6 to the stack
sw $a1, 12($sp) # store $a1 to the stack
jal checkIfWinner
lw $t6, 8($sp) # restore $t6 from the stack
lw $a1, 12($sp) # restore $a1 from the stack
bne $v1, 9, selectedMachinePostionToRowAndColumn
beqz $a1, bestPositionLoopAdjustments # if $a0 equals zero this is the first iteration
addi $t6, $t6, 1 # this negates the substraction below as we still need to check for two in a row at this point
li $a1, 0 # update $a0 to user indicator (defensive)
beq $t6, 3, bestPositionLoopAdjustments # switch back to machine check for the one in a row situation (offensive)
li $a1, 1
bestPositionLoopAdjustments:
subi $t6, $t6, 1 # decement the counter
bnez $t6, findBestPositionLoop
### this is where we end up if no positions were found by the above, so we just want to take first available board
### position at this point
li $t6, 0 # counter
la $t7, BoardPositions
getFirstAvailablePositionLoop:
lw $t8, ($t7)
move $v1, $t6 # set it equal to the counter as that's the board number
beqz $t8, selectedMachinePostionToRowAndColumn
addi $t7, $t7, 4 # move to next board position to see if it is available
addi $t6, $t6, 1 # increment counter
b getFirstAvailablePositionLoop
selectedMachinePostionToRowAndColumn:
move $a0, $v1 # board position
li $a1, 1 # machine indicator
sw $v1, 4($sp) # store $v1
jal updateBoardPositionsAndUserPositions
lw $v1, 4($sp) # restore $v1
la $t0, RowAndColumnToBoardNumber
mul $v1, $v1, 12 # multiply the board number by 12 to get the proper offset for the RowColumn table
nop
nop
add $v0, $t0, $v1 # set the pointer to the correct location in the RowColumn table for the return value
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 16 # set the $sp back to its original state
jr $ra
# determine if the user's position input is valid
# arguments: $a0 as user input row, $a1 has user input column
# return: $vo equals zero if position is not occupied or 1 if it is occupied
checkPositionIsValidAndUpdatePositions:
# stack stuff
subi $sp, $sp, 12 # make room for the saved items below
sw $ra, 0($sp) # store $ra
la $t0, RowAndColumnToBoardNumber
getBoardNumberOuterLoop:
lw $t1, ($t0)
beq $a0, $t1, prepareGetBoardNumberInnerLoop
addi $t0, $t0, 12 # move to next row in lookup table
b getBoardNumberOuterLoop
prepareGetBoardNumberInnerLoop:
addi $t0, $t0, 4 # move to the next word in the table which hold the column number
getBoardNumberInnerLoop:
lw $t1, ($t0)
beq $a1, $t1, prepareFinalPositionIsValidCheck
addi $t0, $t0, 12 # prepare to to next row in lookup table
b getBoardNumberInnerLoop
prepareFinalPositionIsValidCheck:
addi $t3, $t0, 4 # move to the next word in the table which holds the table number
lw $t3, ($t3) # load the word table number word into $t3
sll $t3, $t3, 2 # multiply it by 4 so it's word-aligned
la $t4, BoardPositions
add $t4, $t4, $t3 # set the board positions pointer to proper word
lw $t6, ($t4) # load the actual word
beqz $t6, continueToUpdatingBoardPositions
sw $a0, 4($sp) # store $a0
la $a0, postion_occupied_message
li $v0, 4
syscall
li $v0, 1 # set return to one, meaning the position is occupied
b finishCheckPositionIsValid
continueToUpdatingBoardPositions:
li $v0, 0 # set return to zero, meaning the position is not occupied
sw $a0, 4($sp) # store $a0
sw $a1, 8($sp) # store $a1
lw $t1, 4($t0) # load the selected board position word of the table
li $a0, 0
add $a0, $a0, $t1 # the board position
li $a1, 0 # user indicator
jal updateBoardPositionsAndUserPositions
finishCheckPositionIsValid:
lw $ra, 0($sp) # restore $ra
lw $a0, 4($sp) # restore $a0
lw $a1, 8($sp) # restore $a1
addi $sp, $sp, 12 # set the $sp back to its original state
jr $ra
# updates the occupied board positions and user/machine positions and increments their number of positions
# arguments: $a0 = position, $a1 = user/machine (0 user, 1 machine)
# return: none
updateBoardPositionsAndUserPositions:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
sll $a0, $a0, 2 # multiply the position by four to get it word-aligned
beqz $a1, userProtocol
addi $s2, $s2, 1
addNewPostionToMachinePositions:
la $t0, MachinePositions
add $t0, $t0, $a0 # add the board position to $t0
li $t1, 1
sw $t1, ($t0)
b addNewPostionToBoardPositions
userProtocol:
addi $s1, $s1, 1
addNewPostionToUserPositions:
la $t0, UserPositions
add $t0, $t0, $a0
li $t1, 1
sw $t1, ($t0)
addNewPostionToBoardPositions:
la $t0, BoardPositions
add $t0, $t0, $a0
li $t1, 1
sw $t1, ($t0)
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# determine if the game has been won, also used as in machine's position selection logic
# arguments: $a1 = user/machine (0 = user, 1 = machine),
# args cont... $a3 = checkType (0 = winner check, 1 = (1 in a row) 2 = (2 in a row)
# return: $v1 = board number for machine to take (or 9 if none available or not applicable) -> only for $a3 args > 0
checkIfWinner:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
add $t0, $s1, $s2
beq $t0, 9, TieMessage
li $v1, 9 # used for typeCheck only, 9 is not a possible board position to return, so if that's returned there was no pos found
la $t0, UserPositions
beqz $a1, startWinnerCheck
la $t0, MachinePositions
startWinnerCheck:
la $t1, WinningCombinations
li $t2, 0 # counter
li $t3, 0 # summer
li $t9, 0 # long term counter
winningComboCheckerLoop:
lw $t4, ($t1) # winning position from current row
sll $t4, $t4, 2 # multiply by 4 so it's word-aligned
add $t5, $t0, $t4 # pointer to user/machine's postion at this index
lw $t5, ($t5) # the word at the position for the user/machine
beqz $t5, nextWinnerComboLoop
addi, $t3, $t3, 1 # the user/machine has this spot so add one to the summer
nextWinnerComboLoop:
addi $t2, $t2, 1 # increment counter
addi $t9, $t9, 1 # increment long term counter
addi $t1, $t1, 4 # move the Winnning Combo pointer down one Word for next iteration
checkTypeTwo:
bne $a3, 2, checkTypeOne # this is the 2 in a row situation
bne $t3, 2, regularCheck # if the summer is not at two
beq $t2, 3, regularCheck # if short counter is at three then there is no position to take in this row
b checkIfValidBoardPositionForMachineAndReturnIfSo
checkTypeOne:
bne $a3, 1, regularCheck # this is the 1 in a row situation
bne $t3, 1, regularCheck # if the summer is not at one
beq $t2, 3, regularCheck # if short counter is at three then there is no position to take in this row
checkIfValidBoardPositionForMachineAndReturnIfSo:
la $t7, BoardPositions
lw $t8, ($t1) # get the next board number from the current winning combo iteration
move $v1, $t8 # set the return value to the board number that the machine should take
sll $t8, $t8, 2 # multiply the board number by 4 to word-align it for below addition
add $t7, $t7, $t8 # move the board position's pointer to the correct spot
lw $t7, ($t7) # hold the value of the next winning combo board postion (0 if empty, 1 if occupied)
bnez $t7, regularCheckPrep
b finishCheckIfWinner
regularCheckPrep:
li $v1, 9 # set the return value back to 9 as this spot was not available
regularCheck:
beq $t3, 3, WinOrLoseMessage # if summer equals 3, that means we have a winner, $a1 arugment already correctly set!
blt $t2, 3, winningComboCheckerLoop
li $t2, 0 # reset counter
li $t3, 0 # reset summer
beqz $a3, skipPermuationChecks
blt $t9, 144, winningComboCheckerLoop # go through standard winning combos and permuations
b finishCheckIfWinner
skipPermuationChecks:
blt $t9, 24, winningComboCheckerLoop # pnly go through standard winning combos (not permuations)
finishCheckIfWinner:
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# tell the user that they won (or lost)
# arguments: $a1 = user/machine (0 = user, 1 = machine)
# none
WinOrLoseMessage:
jal prepMakeSound # make the Win or Lose sound, $a1 already set to the correct state
la $a0, win_message
beqz $a1, printWinOrLoseMessage
la $a0, lose_message
printWinOrLoseMessage:
li $v0, 4
syscall
li $v0, 10
syscall
# tell the user that they tied the machine
# arguments: none
# none
TieMessage:
li $a1 2 # tie indicatior
jal prepMakeSound
la $a0, tie_message
li $v0, 4
syscall
li $v0, 10
syscall
# make a Win, Lose or Tie sound
# argument: $a1: Win = 0, Lose = 1, Tie = 2
# return: none
prepMakeSound:
# save to stack
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
move $t0, $a1 # move the arg to $t0 as it is changed below
li $a1, 1200 # time in ms
li $a3, 110 # volume
## Winner (you'll never hear this...)
li $a0, 100 # pitch
li $a2, 50 # instrument
beqz $t0, makeTheSound
## Lose
li $a0, 35 # pitch
li $a2, 7 # instrument
beq $t0, 1, makeTheSound
## Tie
li $a0, 65 # pitch
li $a2, 20 # instrument
makeTheSound:
jal makeSound
# set stack back
lw $ra, 0($sp) # put the correct $ra back to get to this method's caller below
addi $sp, $sp, 4 # adjust $sp back to its original state
jr $ra
# make a sound
# argument: $a0: pitch, $a1: duration, $a2: instrument, $a3: volume
# return: none
makeSound:
# save to stack
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
li $v0, 31 # syscall for MIDI out
syscall
# set stack back
lw $ra, 0($sp) # put the correct $ra back to get to this method's caller below
addi $sp, $sp, 4 # adjust $sp back to its original state
jr $ra
# use the user's input to locate the correct memory location of the coords for drawing the object
# arguments: $a0 = row input, $a1 = column input, $a2 = user/machine (0 = user, 1 = machine)
# return: $v0 = memory address for user's given row/col at the x-coord point (y-coord is one word down)
convertInputToCoords:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
la $t0, OTable
beqz $a2, userInputCoordsProtocol
bnez $s0, getObjectAddressOuterLoop
la $t0, XTable
b getObjectAddressOuterLoop
userInputCoordsProtocol:
beqz $s0, getObjectAddressOuterLoop # if the user is X's update $t0 to the correct label address
la $t0, XTable
getObjectAddressOuterLoop:
lw $t1, 0($t0)
beq $t1, $a0, prepareForObjectAddressInnerLoop # user's row input matched the given lookup table's row move to inner loop (col)
addi $t0, $t0, 16 # move to the next row in the lookup table
b getObjectAddressOuterLoop
prepareForObjectAddressInnerLoop:
addi $t0, $t0, 4 # move to the next word in the lookuptable (columns)
getObjectAddressInnerLoop:
lw $t1, 0($t0) # load the col word from the lookup table
beq $t1, $a1, finishconvertUserInputToCoords # user's col input matched the given lookup table's col
addi $t0, $t0, 16 # move to the next row in the lookup table
b getObjectAddressInnerLoop
finishconvertUserInputToCoords:
la $v0, 4($t0) # set the proper return value
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# decide whether to draw an X or an O
# arguments: $a0 = row input, $a1 = column input $a2, user/machine
# return: none
drawXOrO:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
beqz $a2, userObjectDrawProtocol
beqz $s0, checkDrawXMachine
jal drawO
checkDrawXMachine:
bnez $s0, finishDrawXOrO
jal drawX
b finishDrawXOrO
userObjectDrawProtocol:
bnez $s0, checkDrawX
jal drawO
checkDrawX:
bne $s0, 1, finishDrawXOrO
jal drawX
finishDrawXOrO:
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# draw the game board
# argument: none
# return: none
drawBoard:
# stack stuff
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
jal clearDisplay
li $a0, 86 # x-coordinate
li $a1, 0 # y-coordinate
li $a2, 'W' # white color
li $a3, 255 # length of line
gameBoarDrawingVericalLines:
jal drawVerticalLine
beq $a0, 172, prepareGameBoarDrawingHorizontalLines
sll $a0, $a0, 1 # multiply x-coordinate by 2 for next iteration
li $a1, 0 # y-coordinate back to zero
li $a3, 255 # length of line refresh to 255
b gameBoarDrawingVericalLines
prepareGameBoarDrawingHorizontalLines:
li $a0, 0 # x-coordinate
li $a1, 86 # y-coordinate
li $a3, 255 # length of line refresh to 255
gameBoarDrawingHorizontalLines:
jal drawHorizontalLine
beq $a1, 172, finishGameBoarDrawingLines
sll $a1, $a1, 1 # multiply y-coordinate by 2 for next iteration
li $a0, 0 # x-coordinate back to zero
li $a3, 255 # length of line refresh to 255
b gameBoarDrawingHorizontalLines
finishGameBoarDrawingLines:
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 4 # set the $sp back to its original state
jr $ra
# draw a black box over the entire display
# argument: none
# return: none
clearDisplay:
subi $sp, $sp, 4 # make room for the saved items below
sw $ra, 0($sp) # store $ra
li $a0, 0 # set $a0 to zero
li $a1, 0 # set $a1 to zero
li $a2, '0' # color should be black which is char 0
li $a3, 256 # full screen, all 256 rows
jal drawBox
lw $ra, 0($sp) # put the correct $ra back to get to this method's caller below
addi $sp, $sp, 4 # adjust $sp back to its original state
jr $ra
# draw the X in the correct box
# arguments : $a0 x-coordinate where X begins, $a1 y-coordinate where X begins
# return: nothing
drawX:
# stack stuff
subi $sp, $sp, 12 # make room for the saved items below
sw $ra, 0($sp) # store $ra
sw $a0, 4($sp) # store $a0
sw $a1, 8($sp) # store $a1
li $a2, 'Y' # yellow color
li $a3, 0 # negative slope
jal drawSlopedLine
lw $a0, 4($sp) # restore $a0
lw $a1, 8($sp) # restore $a1
addi $a1, $a1, 62 # y-coordinate
li $a2, 'Y' # yellow color
li $a3, 1 # postive slope
jal drawSlopedLine
lw $ra, 0($sp) # restore $ra
addi $sp, $sp, 12 # set the $sp back to its original state
jr $ra
# draw a horizontal lines of arbitraty length to make a circle
# argument: $a0: x-coordinate, $a1: y-coordinate
# return: none
drawO:
li $a2, 'G' # green color
li $a3, 32 # radius
subi $sp, $sp, 32 # make room for the saved items below
sw $ra, 0($sp) # store $ra
sw $s0, 20($sp) # store $s0 before changing below
sw $s1, 24($sp) # store $s1 before changing below
sw $s5, 28($sp) # store $s1 before changing below
move $s1, $a3 # y-offset
li $s5, 0 # counter
circleLoop:
sw $a1, 4($sp) # store y-coord
sw $a2, 8($sp) # store color
sw $a0, 12($sp) # store x-coord
sw $a3, 16($sp) # store radius
la $t0, SquaresTable # load squares table address into $t0
li $t1, 1024 # set $t0 equal to fixed radius squared (32)^2
# determine the x-offset
sll $t2, $s1, 2 # multiply y-offset by 4 to get the proper offset below (stored as words)
add $t2, $t2, $t0 # set the SquareTable Pointer to the correct value
lw $t3, 0($t2) # set $t3 to the value of the square of the y-offset
sub $t3, $t1, $t3 # substract the square of the y-offset from the square of the radius
li $t4, 0 # counter for loop below, hold the (floor) value of the square root
la $t0, SquaresTable # load the address of the squares table to $t2
determineXPositonOffsetLoop:
lw $t2, 0($t0) # load the next square into $t2 for comparison below
bge $t2, $t3, setCoordinates
addi $t0, $t0, 4 # move the squares table to the next word
addi $t4, $t4, 1 # increment the counter
b determineXPositonOffsetLoop
setCoordinates:
move $t0, $t4 # x-coordinate offset
li $a3, 4 # if zero, make the line length four
subi $a0, $a0, 2 # proper offset for x-coord with 4 as length
beqz $t0, drawLine
addi $a0, $a0, 2 # correct the substraction from above for the zero case
sub $a0, $a0, $t0 # this is the x-coodinate start position
sll $a3, $t0, 1 # multiplying the offset by two gives the length of the line, in $a3
drawLine:
jal drawHorizontalOutline
lw $a1, 4($sp) # load y-coord
lw $a2, 8($sp) # load color
lw $a0, 12($sp) # load x-coord
lw $a3, 16($sp) # load radius
li $t9, -1 # we are first decrementing the y offset on each iteration as we haven't passed the (0,0) coordinate
blt $s5, $a3, stillTopOfCircle
li $t9, 1 # we are now incrementing the y offset on each iteration as we passed the (0,0) coordinate
stillTopOfCircle:
addi $a1, $a1, 1 # increment y-coordinate
add $s1, $s1, $t9 # increment/decrement y-offset
addi $s5, $s5, 1 # increment counter
sll $t2, $a3, 1 # multiply the radius by two to get the diameter for below comparison
ble $s5, $t2, circleLoop
lw $s0, 20($sp) # restore $s0 to its state before this method
lw $s1, 24($sp) # restore $s1 to its state before this method
lw $s5, 28($sp) # restore $s5 to its state before this method
lw $ra, 0($sp) # put the correct $ra back to get to this method's caller below
addi $sp, $sp, 32 # adjust $sp back to its original state
jr $ra
# draw a horizontal line of an arbitraty length to make a rectangle
# argument: $a0: x-coordinate, $a1: y-coordinate, $a2: color, $a3: rows of box
# return: none
drawBox:
subi $sp, $sp, 24 # make room for the saved items below
sw $ra, 0($sp) # store $ra
sw $s0, 20($sp) # store $s0 before changing below
move $s0, $a3 # set $s0 equal to $a3
boxLoop:
sw $a1, 4($sp) # store $a1
sw $a2, 8($sp) # store $a2
sw $a0, 12($sp) # store $a0
sw $a3, 16($sp) # store $a3
jal drawHorizontalLine
lw $a1, 4($sp) # load $a1
lw $a2, 8($sp) # load $a2
lw $a0, 12($sp) # load $a0
lw $a3, 16($sp) # load $a3
addi $a1, $a1, 1 # increment the y-coordinate
subi $s0, $s0, 1 # decrement box row count
bnez $s0, boxLoop
lw $s0, 20($sp) # restore $s0 to its state before this method