-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.asm
949 lines (868 loc) · 19.3 KB
/
game.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
#####################################################################
#
# CSCB58 Winter 2021 Assembly Final Project
# University of Toronto, Scarborough
#
# Student: Shengsong Xu, 1005788970, Xushengs
#
# Bitmap Display Configuration:
# - Unit width in pixels: 8 (update this as needed)
# - Unit height in pixels: 8 (update this as needed)
# - Display width in pixels: 256 (update this as needed)
# - Display height in pixels: 256 (update this as needed)
# - Base Address for Display: 0x10008000 ($gp)
#
# Which milestones have been reached in this submission?
# (See the assignment handout for descriptions of the milestones)
# - Milestone 4 (choose the one the applies)
#
# Which approved features have been implemented for milestone 4?
# (See the assignment handout for the list of additional features)
# 1. Increase in difficulty as game progresses. Difficulty can be achieved by making things faster, adding more obstacles
# 2. Smooth graphics: prevent flicker by carefully erasing and/or redrawing only the parts to the frame buffer that have changed
# 3. Player can use special skill that can slow down time, three times per game.
# 4. Improved control system: only need to press movement key once, then ship will continually move towards that direction,
# press keys other than p, w, a, s, d, e to stop the ship
#
# Link to video demonstration for final submission:
# - https://www.youtube.com/watch?v=7sUfFJ8aa7g
#
# Are you OK with us sharing the video with people outside course staff?
# - yes
#
# Any additional information that the TA needs to know:
# - I have built a scoring system and can show the score on the game-over screen once finished the function: ShowScore.
# I did not have time to finish the score feature for this assignment. But you can see the pseudocode I wrote for future coding.
# #####################################################################
.eqv Width 32
.eqv Height 32
.eqv Dis_add 0x10008000
.eqv KBD_Base 0xffff0000
.eqv InitPos 1936
.eqv LastP 4092
# Color lists
.eqv Black 0x00000000
.eqv Red 0x00E74C3C
.eqv LRed 0x00EC7063
.eqv DGrey 0x00515A5A
.eqv LGrey 0x00839192
.eqv Blue 0x005DADE2
.eqv Purple 0x00A569BD
# a, w, d, s, p
.eqv A 0x61
.eqv D 0x64
.eqv S 0x73
.eqv W 0x77
.eqv P 0x70
.eqv E 0x65
# Game setiing
.eqv healthV 59
.eqv Tinterval 40
.eqv Slow 100
.eqv SlowT 50
.eqv First 1824
.eqv Second 1840
.eqv Third 1856
.eqv Fourth 1872
.data
CurrPos: .word 0 # CurrPos = (y*Width + x)*4, Current position of the ship
# Index (0, 0) <= (x, y) <= (31, 31): (y*Width + x)*4
# x = (index/4)%Width ie: Remainder
# y = (index/4)/Width ie: Integer Devision
ObstPos: .word -1:10 # Position of obstacles
ObstSpeed: .word 0: 10 # Speed of obstacles, slow if <= 4; o/w fast
Health: .word 50 # Max Health of the ship
Instruction: .word 0 # A = -4, W = -128, S = 128, D = 4
TimeCount: .word 0 # increase by after one every iteration
Level: .word 5 # from easy to hard: 5 -> 20
ObstAmount: .word 3 # current number of obstacles
IfHit: .word 0 # if the obstacles hit the ship
SlowMotion: .word 3 # number of slow motion skill left
SlowMotionT: .word 50 # slow motion time
IfSlowMotion: .word 0 # if in slow motion
.globl main
.text
# Initialize the state of the game
# $s0 - $s2 <- tempVar
Initialize:
jal ClearScreen
# build ship at InitPos
li $s0, InitPos
# Update the CurrPos
la $s1, CurrPos
sw $s0, 0($s1)
# push InitPos to the stack
addi $sp, $sp, -4
sw $s0, 0($sp)
# call BuildShip
jal BuildShip
# initialize health to healthV
la $s0, Health
li $s1, healthV
sw $s1, 0($s0)
# show Health bar
li $s0, Dis_add
li $s3, Red
sw $s3, 0($s0)
sw $s3, 4($s0)
sw $s3, 8($s0)
sw $s3, 12($s0)
sw $s3, 16($s0)
# initialize time to 0
la $s0, TimeCount
li $s1, 0
sw $s1, 0($s0)
# initialize Level to 5
la $s0, Level
li $s1, 5
sw $s1, 0($s0)
# initialize ObstAmount to 3
la $s0, ObstAmount
li $s1, 2
sw $s1, 0($s0)
# initialize IfHit to 0
la $s0, IfHit
li $s1, 0
sw $s1, 0($s0)
# initialize SlowMotion to 3
la $s0, SlowMotion
li $s1, 3
sw $s1, 0($s0)
# show SlowMotion
li $s0, Dis_add
li $s3, Purple
sw $s3, 124($s0)
sw $s3, 116($s0)
sw $s3, 108($s0)
# initialize SlowMotionT to SlowT
la $s0, SlowMotionT
li $s1, SlowT
sw $s1, 0($s0)
# initialize IfSlowMotion to 0
la $s0, IfSlowMotion
li $s1, 0
sw $s1, 0($s0)
# build obstacles
li $t1, 0
la $t2, ObstAmount
lw $t2, 0($t2)
ObstLoop:
# push i to the stack
addi $sp, $sp, -4
sw $t1, 0($sp)
jal BuildObst
beq $t1, $t2, WaitStart
addi $t1, $t1, 1
j ObstLoop
WaitStart:
# Press P to start the game
li $t0, KBD_Base
lw $t1, 0($t0)
beq $t1, 1, Start
Start:
lw $t1, 4($t0) # get the input
beq $t1, S, main
j WaitStart
# $t0 <- kBD_Base
# $t1 <- contents of KBD_Base
# $t2 <- Instruction
# $t3 <- x or y
# $t4 <- CurrPos
# $s0 - $s3 <- tempVar
main:
# remove the hit affect
li $t0, Dis_add
la $t1, CurrPos
lw $t1, 0($t1)
add $t1, $t1, $t0 # $t1 = Dis_add + CurrPos
li $s0, Black
sw $s0, -128($t1)
sw $s0, 128($t1)
sw $s0, 4($t1)
sw $s0, -4($t1)
# Redraw ship at CurrPos
jal BuildShip
# show Health
jal ShowHealth
# Check for keyboard input and update ship location.
# wait for input
li $t0, KBD_Base
lw $t1, 0($t0)
beq $t1, 1, KBD_happened
# j ObstLocation
KBD_happened:
lw $t1, 4($t0) # get the input
la $t4, CurrPos # $t4 = CurrPos
lw $t4, 0($t4)
li $s0, 32 # $s0 = 32
beq $t1, A, input_A
beq $t1, W, input_W
beq $t1, S, input_S
beq $t1, D, input_D
beq $t1, E, input_E
beq $t1, P, Initialize #restart the game
j ObstLocation
# Move the ship and check if move is valid
input_A:
# check if move right is valid by calculate x = (CurrPos/4)%32
srl $t4, $t4, 2 # divide CurrPos by 4
divu $t4, $s0 # divide (CurrPos/4) by 32
mfhi $t3 # $t3 = x
beq $t3, 1, ObstLocation
la $t2, Instruction #valid move, move ship
li $s0, -4
sw $s0, 0($t2)
jal MoveShip
j ObstLocation
input_W:
srl $t4, $t4, 2 # divide CurrPos by 4
divu $t4, $s0 # divide (CurrPos/4) by 32
mflo $t3 # $t3 = y
beq $t3, 2, ObstLocation
la $t2, Instruction #valid move, move ship
li $s0, -128
sw $s0, 0($t2)
jal MoveShip
j ObstLocation
input_S:
srl $t4, $t4, 2 # divide CurrPos by 4
divu $t4, $s0 # divide (CurrPos/4) by 32
mflo $t3 # $t3 = y
beq $t3, 30, ObstLocation
la $t2, Instruction #valid move, move ship
li $s0, 128
sw $s0, 0($t2)
jal MoveShip
j ObstLocation
input_D:
srl $t4, $t4, 2 # divide CurrPos by 4
divu $t4, $s0 # divide (CurrPos/4) by 32
mfhi $t3 # $t3 = x
beq $t3, 30, ObstLocation
la $t2, Instruction #valid move, move ship
li $s0, 4
sw $s0, 0($t2)
jal MoveShip
j ObstLocation
input_E:
# slow motion
# check if SlowMotion > 0
la $s0, SlowMotion
lw $s1, 0($s0)
beqz $s1, ObstLocation
# check if currently in IfSlowMotion
la $s2, IfSlowMotion
lw $s3, 0($s2)
beq $s3, 1, ObstLocation
# Update IfSlowMotion and SlowMotion
addi $s1, $s1, -1
sw $s1, 0($s0)
li $s3, 1
sw $s3, 0($s2)
li $s3, Dis_add
li $s4, Black
# show SlowTime bar
sll $s1, $s1, 3
add $s3, $s3, $s1
sw $s4, 108($s3)
# Update obstacle location.
# $t1 <- i
# $t2 <- ObstPos
# $t3 <- ObstPos[i]
# $s0 <- tempVar
ObstLocation:
li $t1, 0 # i = 0
la $t4, ObstAmount
lw $t4, 0($t4)
Loop1:
# push i to the stack
addi $sp, $sp, -4
sw $t1, 0($sp)
# access ObstPos[i]
la $t2, ObstPos
sll $s0, $t1, 2 # $s0 = 4*i
add $t2, $t2, $s0 # $t2 = ObstPos + 4*i
lw $t3, 0($t2) # $t3 = ObstPos[i]
# if ObstPos[i] == -1: call BuildObst
beq $t3, -1, Call_buildObst
# else call MoveObst
jal MoveObst
IncreaseI:
beq $t1, $t4, Detect_Collision
addi $t1, $t1, 1
j Loop1
Call_buildObst:
jal BuildObst
j IncreaseI
# Check for various collisions (e.g., between ship and obstacles).
# $t1 <- i
# $t2 <- ObstPos
# $t3 <- ObstPos[i]
# $t4 <- ObstAmount
# $t5 <- Xs
# $t6 <- Ys
# $t7 <- Xo, Xo - Xs
# $t8 <- Ys, Yo - Ys
# $t9 <- tempVar
# $s0 <- 32
Detect_Collision:
# set i = 0
li $t1, 0 # i = 0
la $t4, ObstAmount
lw $t4, 0($t4)
# acsess CurrPos, $t9 = CurrPos
la $t9, CurrPos
lw $t9, 0($t9)
# calculate Xs, Ys
li $s0, 32
srl $t5, $t9, 2 # divide CurrPos by 4
divu $t5, $s0 # divide (CurrPos/4) by 32
mfhi $t5 # $t5 = Xs
mflo $t6 # $t6 = Ys
# for each ObstPos:
Loop2:
# access ObstPos[i]
la $t2, ObstPos
sll $s0, $t1, 2 # $s0 = 4*i
add $t2, $t2, $s0 # $t2 = ObstPos + 4*i
lw $t3, 0($t2) # $t3 = ObstPos[i]
# Calculate Xo, Yo of ObstPos[i]
li $s0, 32
srl $t7, $t3, 2 # divide ObstPos[i] by 4
divu $t7, $s0 # divide (ObstPos[i]/4) by 32
mfhi $t7 # $t7 = Xo
mflo $t8 # $t8 = Yo
# Calculate $t7 = Xo - Xs
sub $t7, $t7, $t5
# Calculate $t8 = Yo - Ys
sub $t8, $t8, $t6
# if $t7 > 2: j Updata_status
bgt $t7, 3, Increase2
# if $t7 < 0: j Update_status
blt $t7, -1, Increase2
# if $t8 > 1: j Updata_status
bgt $t8, 2, Increase2
# if $t8 < -1: j Update_status
blt $t8, -2, Increase2
# else: IfHit = 1
la $s0, IfHit
li $t9, 1
sw $t9, 0($s0)
# Increase i
Increase2:
beq $t1, $t4, Updata_status
addi $t1, $t1, 1
j Loop2
# Update other game state and end of game: Health; Level; TimeCount
# $s0 <- IfHit, color
# $s1 <- tempvar
# $t0 <- Dis_add
# $t1 <- CurrPos
# $t2 <- Health
Updata_status:
# show hit effect
la $s0, IfHit
lw $s0, 0($s0)
bne $s0, 1, NotHit
# load Dis_add
li $t0, Dis_add
la $t1, CurrPos
lw $t1, 0($t1)
add $t1, $t1, $t0 # $t1 = Dis_add + CurrPos
# draw effect
li $s0, Red
sw $s0, 0($t1)
sw $s0, -128($t1)
sw $s0, 128($t1)
sw $s0, 4($t1)
sw $s0, -4($t1)
NotHit:
# access IfHit
la $s0, IfHit
lw $s0, 0($s0)
# access Health
la $t2, Health
lw $t2, 0($t2)
# Update Health
sub $s1, $t2, $s0 # $s1 = Health - IfHit
# sub $s1, $t2, 0 # for testing
la $t2, Health
sw $s1, 0($t2)
beqz $s1, GameOver
# reset IfHit = 0
la $s0, IfHit
li $s1, 0
sw $s1, 0($s0)
# access TimeCount: don't change $s0, $s1
la $s0, TimeCount
lw $s1, 0($s0)
# calculate TimeCount % 15000
li $t0, 500
divu $s1, $t0
mfhi $t0
# Check if need to Update Level and ObstAmount
beqz $s1, Sleep
bnez $t0, Sleep
# check if reach the highest ObstAmount
la $t1, Level
lw $t3, 0($t1)
addi $t3, $t3, 1
sw $t3, 0($t1) # Level ++
# access ObstAmount
la $t1, ObstAmount
lw $t3, 0($t1)
beq $t3, 7, Sleep
addi $t3, $t3, 1
sw $t3, 0($t1)
Sleep:
# Update TimeCount
addi $s1, $s1, 1
sw $s1, 0($s0)
# check IfSlowMotion
la $t1, IfSlowMotion
lw $t2, 0($t1)
beq $t2, 0, NormalSleep
# check SlowMotionT == 0
la $t3, SlowMotionT
lw $t4, 0($t3)
beqz $t4, UpdateSlowMotion
# in SlowMotion, update SlowMotionT
addi $t4, $t4, -1
sw $t4, 0($t3)
# Sleep in SlowMotion
li $v0, 32
li $a0, Slow
syscall
# iterate again
j main
UpdateSlowMotion:
# reset SlowMotionT
li $s2, SlowT
sw $s2, 0($t3)
# reset IfSlowMotion
li $s2, 0
sw $s2, 0($t1)
NormalSleep:
# Sleep for Tinterval seconds.
li $v0, 32
li $a0, Tinterval
syscall
# Iterate again
j main
# GameOver: show final score: TimeCount/10,
GameOver:
li $s0, 0 #i = 0
li $s1, LRed
li $s2, Dis_add
Loop4:
bgt $s0, LastP, SScore #if (i > LastP): Return
sw $s1, 0($s2)
addi $s0, $s0, 4 #i =+ 4
addi $s2, $s2, 4
j Loop4
SScore:
# load TimeCount
la $s0, TimeCount
lw $s1, 0($s0)
# TimeCount / 8
# $v0 = TimeCount / 8
srl $v0, $s1, 3
# jal ShowScore
Restart:
li $t0, KBD_Base
lw $t1, 0($t0)
beq $t1, 1, Restart_happen
# j DebugEND
Restart_happen:
lw $t1, 4($t0) # get the input
beq $t1, P, Initialize
j Restart
DebugEND:
li $v0, 1
la $t0, TimeCount
lw $t0, 0($t0)
move $a0, $t0
syscall
li $v0, 10
syscall
# Functions are here:
#Function: void ClearScreen(), Empty the Entire Screen
ClearScreen:
li $s0, 0 #i = 0
li $s1, Black
li $s2, Dis_add
Loop:
bgt $s0, LastP, Return #if (i > LastP): Return
sw $s1, 0($s2)
addi $s0, $s0, 4 #i =+ 4
addi $s2, $s2, 4
j Loop
Return:
jr $ra
#Function: void BuildShip()
# $s0 <- CurrPos (make sure is valid) ie: 1 <= x <= 20, 1 <= y <= 30
# $s1 <- dis_add
# $s2 <- color
BuildShip:
# get CurrPos
la $s0, CurrPos
lw $s0, 0($s0)
# build the ship
li $s1, Dis_add
add $s1, $s1, $s0
# pick red
li $s2, Purple
sw $s2, 0($s1)
li $s2, Blue
sw $s2, -124($s1)
sw $s2, -132($s1)
sw $s2, 132($s1)
sw $s2, 124($s1)
jr $ra
# function: void MoveShip() fetch Instruction and CurrPos, Make Sure move is valid
# $s0 <- tempVar
# $s1 <- Instruction
# $s2 <- CurrPos
# $s3 <- dis_add
MoveShip:
# get Instruction from .data
la $s0, Instruction
lw $s1, 0($s0)
# get CurrPos from .data
la $s0, CurrPos
lw $s2, 0($s0)
# erase the ship at CurrPos
li $s3, Dis_add
add $s3, $s3, $s2 # $s3 = CurrPos
li $s0, Black
sw $s0, 0($s3)
sw $s0, -124($s3)
sw $s0, -132($s3)
sw $s0, 132($s3)
sw $s0, 124($s3)
# update the CurrPos
add $s2, $s2, $s1
la $s0, CurrPos
sw $s2, 0($s0)
# build the ship by Instrcution
add $s3, $s3, $s1
li $s0, Purple
sw $s0, 0($s3)
li $s0, Blue
sw $s0, -124($s3)
sw $s0, -132($s3)
sw $s0, 132($s3)
sw $s0, 124($s3)
jr $ra
# Function: void BuildObst(int i): Generate and build obstcle at ObstPos[i]
# $s0 <- ObstPos
# $s1 <- color
# $s2 <- Diss_add
# $s3 <- Random Number
# $s4 <- ObstSpeed
# $s5 <- i
BuildObst:
# pop i from stack
lw $s5, 0($sp)
addi $sp, $sp, 4
sll $s5, $s5, 2 # i = i*4
# load ObstPos and Dis_add
la $s0, ObstPos
li $s2, Dis_add
la $s4, ObstSpeed
# generate random speed
li $v0, 42
li $a0, 0
la $a1, Level
lw $a1, 0($a1) # $a1 = Level
syscall
# store speed in ObstSpeed
move $s3, $a0
add $s4, $s4, $s5 # ObstSpeed + i*4
sw $s3, 0($s4) # ObstSpeed[i] = Random Speed
# generate random index
li $v0, 42
li $a0, 0
li $a1, 29
syscall
# store speed in ObstPos
move $s3, $a0
addi $s3, $s3, 2
sll $s3, $s3, 5
addi $s3, $s3, 31
sll $s3, $s3, 2
add $s0, $s0, $s5 # ObstPos + i*4
sw $s3, 0($s0) # ObstPos[i] = Random index
# build obstcles at ObstPos[i]
add $s2, $s2, $s3
li $s1, LGrey
sw $s1, -8($s2)
sw $s1, 124($s2)
li $s1, DGrey
sw $s1, -4($s2)
sw $s1, -132($s2)
sw $s1, 0($s2)
# return
jr $ra
# Function: void MoveObst(int i): Move obstcles
# $s0 <- ObstPos
# $s1 <- 32
# $s3 <- ObstPos[i]
# $s4 <- ObstSpeed[i]
# $s5 <- i
# $s6 <- x of ObstPos[i]
MoveObst:
# pop i from stack
lw $s5, 0($sp)
addi $sp, $sp, 4
sll $s5, $s5, 2 # i = i*4
# load ObstPos and ObstSpeed
la $s0, ObstPos
la $s4, ObstSpeed
# access ObstPos[i] and ObstSpeed[i]
add $s0, $s0, $s5 # $s3 = ObstPos + i*4
lw $s3, 0($s0) # $s3 = ObstPos[i]
add $s4, $s4, $s5 # $s4 = ObstSpeed + i*4
lw $s4, 0($s4) # $s4 = ObstSpeed[i]
# Calculate x of ObstPos[i]: x = (ObstPos[i] / 4) % 32
li $s1, 32
srl $s6, $s3, 2 # divide ObstPos[i] by 4
divu $s6, $s1 # divide (ObstPos[i]/4) by 32
mfhi $s6 # $s6 = x
# Check for four different move cases
# case1: x == 2; ObstPos[i] =- 4*Speed[i]
beq $s6, 2, Move_Case1
# case2: x == 1; ObstPos[i] =- 4*Speed[i] or -1
beq $s6, 1, Move_Case2
# case3: x == 0; ObstPos[i] = -1
beq $s6, 0, Move_Case3
# case4: x > 3; ObstPos[i] =- 4*Speed[i]
bgt $s6, 2, Move_Case4
Move_Case1:
# push old $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# $a2 = ObstPos[i] = $s3
move $a2, $s3
# call MoveObst1 with mode = 0 for erase
li $a0, 0
jal MoveObst1
# ObstPos[i] = ObstPos[i] - 4
addi $s3, $s3, -4
sw $s3, 0($s0)
move $a2, $s3
# call MoveObst2 with mode = 1 for build
li $a0, 1
jal MoveObst2
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
Move_Case2:
# push old $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# $a2 = ObstPos[i] = $s3
move $a2, $s3
# call MoveObst2 with mode = 0 for erase
li $a0, 0
jal MoveObst2
# if speed > 4:
bgt $s4, 4, MoveFast2
# else: speed <= 4:
# ObstPos[i] = ObstPos[i] - 4
addi $s3, $s3, -4
sw $s3, 0($s0)
move $a2, $s3
# call MoveObst3 with mode = 1 for build
li $a0, 1
jal MoveObst3
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
MoveFast2:
# ObstPos[i] = -1
li $s3, -1
sw $s3, 0($s0)
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
Move_Case3:
# push old $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# $a2 = ObstPos[i] = $s3
move $a2, $s3
# call MoveObst3 with mode = 0 for erase
li $a0, 0
jal MoveObst3
# ObstPos[i] = -1
li $s3, -1
sw $s3, 0($s0)
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
Move_Case4:
# push old $ra
addi $sp, $sp, -4
sw $ra, 0($sp)
# $a2 = ObstPos[i] = $s3
move $a2, $s3
# call MoveObst1 with mode = 0 for erase
li $a0, 0
jal MoveObst1
# if speed > 4:
bgt $s4, 4, MoveFast4
# else: speed <= 4:
# ObstPos[i] = ObstPos[i] - 4
addi $s3, $s3, -4
sw $s3, 0($s0)
move $a2, $s3
# call MoveObst1 with mode = 1 for build
li $a0, 1
jal MoveObst1
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
MoveFast4:
# ObstPos[i] = ObstPos[i] - 8
addi $s3, $s3, -8
sw $s3, 0($s0)
move $a2, $s3
li $a0, 1
# if $s1 = x == 3: Move to shape2
beq $s6, 3, EdgeCase
# call MoveObst1 with mode = 1 for build
jal MoveObst1
j Popra
EdgeCase:
jal MoveObst2
Popra:
# pop old $ra
lw $ra, 0($sp)
addi $sp, $sp, 4
# jump to return1
j return1
return1:
jr $ra
# Function: MoveObst1(int mode)
# $a0 <- mode: 0: erase 1: build
# $s2 <- Dis_add
# $a1 <- color, tempVar
# $a2 <- ObstPos[i]
MoveObst1:
# load Dis_add, calculate Dis_add + ObstPos[i]
li $s2, Dis_add
add $s2, $s2, $a2
# if mode == 0: erase the obstacle at ObstPos[i]
beqz $a0, Erase1
# else: build the obstacle at ObstPos[i]
li $a1, LGrey
sw $a1, -8($s2)
sw $a1, 124($s2)
li $a1, DGrey
sw $a1, -4($s2)
sw $a1, -132($s2)
sw $a1, 0($s2)
j ReturnShip1
Erase1:
li $a1, Black
sw $a1, -8($s2)
sw $a1, 124($s2)
sw $a1, -4($s2)
sw $a1, -132($s2)
sw $a1, 0($s2)
ReturnShip1:
jr $ra
# Function: MoveObst2(int mode)
# $a0 <- mode: 0: erase 1: build
# $s2 <- Dis_add
# $a1 <- color, tempVar
# $a2 <- ObstPos[i]
MoveObst2:
# load Dis_add, calculate Dis_add + ObstPos[i]
li $s2, Dis_add
add $s2, $s2, $a2
# if mode == 0: erase the obstacle at ObstPos[i]
beqz $a0, Erase2
# else: build the obstacle at ObstPos[i]
li $a1, LGrey
sw $a1, 124($s2)
li $a1, DGrey
sw $a1, -4($s2)
sw $a1, -132($s2)
sw $a1, 0($s2)
j ReturnShip2
Erase2:
li $a1, Black
sw $a1, 124($s2)
sw $a1, -4($s2)
sw $a1, -132($s2)
sw $a1, 0($s2)
ReturnShip2:
jr $ra
# Function: MoveObst3(int mode)
# $a0 <- mode: 0: erase 1: build
# $s2 <- Dis_add
# $a1 <- color, tempVar
# $a2 <- ObstPos[i]
MoveObst3:
# load Dis_add, calculate Dis_add + ObstPos[i]
li $s2, Dis_add
add $s2, $s2, $a2
# if mode == 0: erase the obstacle at ObstPos[i]
beqz $a0, Erase3
# else: build the obstacle at ObstPos[i]
li $a1, DGrey
sw $a1, 0($s2)
j ReturnShip3
Erase3:
li $a1, Black
sw $a1, 0($s2)
ReturnShip3:
jr $ra
# Function: ShowHealth()
# $a0 <- Dis_add
# $a1 <- i
# $a2 <- Health
# $a3 <- Color
ShowHealth:
# load Dis_add
li $a0, Dis_add
addi $a0, $a0, 16
# load Health
la $a2, Health
lw $a2, 0($a2)
li $a1, 10
divu $a2, $a1 # $a1 = Health / 10
mflo $a2
li $a1, 5
sub $a1, $a1, $a2 # i = 5 - Health
Loop3:
beqz $a1, return3
li $a3, LRed
sw $a3, 0($a0)
# i++
addi $a0, $a0, -4
addi $a1, $a1, -1
j Loop3
return3:
jr $ra
# Function: ShowScore(int score)
# $v0 <- score
# $v1 <- digit
# $v2 <- tempVar
ShowScore:
# first digit = score / 1000
# second digit = score - 1000 * first digit / 100
# third digit = score - 100 * second digit / 10
# fourth digit = score - 10 * third digit / 1