-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoveenemy.asm
719 lines (574 loc) · 13.7 KB
/
moveenemy.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
# Bitmap Display Configuration:
# - Unit width in pixels: 8
# - Unit height in pixels: 8
# - Display width in pixels: 256
# - Display height in pixels: 256
# - Base Address for Display: 0x10008000 ($gp)
#
.eqv lightBrown 0xa0522d
.eqv darkBrown 0x654321
.eqv baseAddress 0x10008000
.data
obstacle: .word 0, 0 #x, y coordinates of the obstacle
positions: .word 0, 0, 0, 0, 0 #Contains the positions of all the obstacles
dspwn1: .word 0, 0, 0, 0 #Contains the distance from the point at which the obstacle needs to vanish and be generated again elsewhere
.text
la $k0, positions #Initialize the two arrys into registers
la $k1, dspwn1
makeEnemies: #Create three obstacles at three random locations
jal enemyLocation
addi $0, $0, 0
jal enemyLocation
addi $0, $0, 0
jal enemyLocation
addi $0, $0, 0
moveEnemies: #Move the obstacles forward one pixel
jal moveEnemy1 #Draw each obstacle while updating their position in the array
addi $0, $0, 0
jal moveEnemy2
addi $0, $0, 0
jal moveEnemy3
addi $0, $0, 0
li $v0, 32 #Sleep the simulator to slow eveerything down
li $a0, 100
syscall
jal cleanEnemy1 #"Clear" the enemy by redrawing their position in black
addi $0, $0, 0
jal cleanEnemy2
addi $0, $0, 0
jal cleanEnemy3
addi $0, $0, 0
lw $t8, 4($k0) #Increment the positions of each obstacle by shifting them one pixel to the left
addi $t8, $t8, -4
sw $t8, 4($k0)
lw $t8, 8($k0)
addi $t8, $t8, -4
sw $t8, 8($k0)
lw $t8, 12($k0)
addi $t8, $t8, -4
sw $t8, 12($k0)
jal checkDespawn #Jumps to the portion of the code which checks to see if an obstacle should be removed
addi $0, $0, 0
j moveEnemies #Jump back to the top of the loop
addi $0, $0, 0
enemyLocation:
addi $sp, $sp, -4 # move stack up
sw $ra, 0($sp) # save the return address
li $v0, 42 # Service 42, random int range
li $a0, 0 # Select random generator 0
li $a1, 26 # Upper bound of random number generator is 30
syscall # Generate random int (returns in $a0)
la $t0, obstacle #$t0 holds the address of the enemy's top right corner
sw $a0, 4($t0) #save random y coordinate into enemy array
li $v0, 42 # Service 42, random int range
li $a0, 0 # Select random generator 0
li $a1, 2 # Upper bound of random number generator is 2
syscall # Generate random int (returns in $a0)
addi $a0, $a0, 29 # random int between 29 and 31
sw $a0, 0($t0) #save random x coordinate (between 16 and 32) into the enemy array
la $t0, darkBrown # $t0 stores the dark brown colour
addi $sp, $sp, -4 # move up stack
sw $t0, 0($sp) #store the rdark brown colour into the stack
la $t0, lightBrown # t0 stores the light brown colour
addi $sp, $sp, -4 # move up stack
sw $t0, 0($sp) #store the light brown colour into the stack
jal drawEnemy
addi $0, $0, 0
lw $ra, 0($sp) # restor this function's return adress
addi $sp, $sp, 4 # move the stack down
jr $ra
addi $0, $0, 0
drawEnemy:
lw $t0, 0($sp) # $t0 holds colour popped off from the stack
addi $sp, $sp, 4 # update stack down
lw $t4, 0($sp) # $t4 holds colour popped off from the stack
addi $sp, $sp, 4 # update stack down
la $t1, baseAddress # $t1 has the display address
la $t2, obstacle # $t2 holds the top right corner of the enemy block
lw $t2, 4($t2) # $t2 holds the y coordinate
sll $t3, $t2, 5 # $t3 holds y coordinate * 32
la $t2, obstacle # $t2 holds the top right corner of the enemy block
lw $t2, 0($t2) # $t2 holds the x coordinate
add $t3, $t3, $t2 # $t3 holds 32*y + x
sll $t3, $t3, 2 # $t3 holds 4*(32*y + x)
add $t3, $t3, $t1 # $t3 holds 4*(32*y + x) + baseAddress
lw $t8, 4($k0)
beqz $t8, LP1
lw $t8, 8($k0)
beqz $t8, LP2
lw $t8, 12($k0)
beqz $t8, LP3
addi $0, $0, 0
LP1: sw $t3, 4($k0) #Stores the position of the first obstacle in the first slot in the array, if there isn't one
j C
addi $0, $0, 0
LP2: sw $t3, 8($k0) #Stores the position of the second obstacle in the second slot in the array, if there isn't one
j C
addi $0, $0, 0
LP3: sw $t3, 12($k0) #Stores the position of the third obstacle in the third slot in the array, if there isn't one
j C
addi $0, $0, 0
C: addi $t9, $t3, 0 #Calculate the number of pixels that the obstacle is from the left edge of the screen upon generation
andi $t9, 0x7f
srl $t9, $t9, 2
addi $t9, $t9, -5
lw $t8, 4($k1)
beqz $t8, LD1
lw $t8, 8($k1)
beqz $t8, LD2
lw $t8, 12($k1)
beqz $t8, LD3
addi $0, $0, 0
LD1: sw $t9, 4($k1) #Stores the despawn distance of the first obstacle in the first slot in the array, if there isn't one
j C1
addi $0, $0, 0
LD2: sw $t9, 8($k1) #Stores the despawn distance of the second obstacle in the second slot in the array, if there isn't one
j C1
addi $0, $0, 0
LD3: sw $t9, 12($k1) #Stores the despawn distance of the third obstacle in the third slot in the array, if there isn't one
j C1
addi $0, $0, 0
# first row
#sw $t0, 0($t3)
C1: sw $t0, -4($t3) #Draws the obstacle
sw $t0, -8($t3)
sw $t0, -12($t3)
#sw $t0, -16($t3)
#sw $t0, -20($t3)
#set to next row, rightmost pixel
addi $t3, $t3, 128
# second row
sw $t0, 0($t3)
sw $t0, -4($t3)
sw $t4, -8($t3)
sw $t4, -12($t3)
sw $t0, -16($t3)
#sw $t0, -20($t3)
#set to next row, rightmost pixel
addi $t3, $t3, 128
# third row
sw $t0, 0($t3)
sw $t0, -4($t3)
sw $t0, -8($t3)
sw $t4, -12($t3)
sw $t4, -16($t3)
sw $t0, -20($t3)
#set to next row, rightmost pixel
addi $t3, $t3, 128
# fourth row
sw $t0, 0($t3)
sw $t4, -4($t3)
sw $t0, -8($t3)
sw $t0, -12($t3)
sw $t4, -16($t3)
sw $t0, -20($t3)
#set to next row, rightmost pixel
addi $t3, $t3, 128
# fifth row
sw $t0, 0($t3)
sw $t0, -4($t3)
sw $t4, -8($t3)
sw $t0, -12($t3)
sw $t0, -16($t3)
sw $t0, -20($t3)
#set to next row, rightmost pixel
addi $t3, $t3, 128
# sixth row
#sw $t0, 0($t3)
sw $t0, -4($t3)
sw $t0, -8($t3)
sw $t0, -12($t3)
sw $t0, -16($t3)
#sw $t0, -20($t3)
#return to line after function call
jr $ra
addi $0, $0, 0
moveEnemy1: #Draws the obstacle and decrease the distance from despawn
li $t0, lightBrown
li $t4, darkBrown
lw $s1, 4($k0)
# first row
#sw $t0, 0($s1)
sw $t0, -4($s1)
sw $t0, -8($s1)
sw $t0, -12($s1)
#sw $t0, -16($s1)
#sw $t0, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# second row
sw $t0, 0($s1)
sw $t0, -4($s1)
sw $t4, -8($s1)
sw $t4, -12($s1)
sw $t0, -16($s1)
#sw $t0, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# third row
sw $t0, 0($s1)
sw $t0, -4($s1)
sw $t0, -8($s1)
sw $t4, -12($s1)
sw $t4, -16($s1)
sw $t0, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# fourth row
sw $t0, 0($s1)
sw $t4, -4($s1)
sw $t0, -8($s1)
sw $t0, -12($s1)
sw $t4, -16($s1)
sw $t0, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# fifth row
sw $t0, 0($s1)
sw $t0, -4($s1)
sw $t4, -8($s1)
sw $t0, -12($s1)
sw $t0, -16($s1)
sw $t0, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# sixth row
#sw $t0, 0($s1)
sw $t0, -4($s1)
sw $t0, -8($s1)
sw $t0, -12($s1)
sw $t0, -16($s1)
#sw $t0, -20($s1)
lw $t8, 4($k1) #Decrease the distance from despawn
addi $t8, $t8, -1
sw $t8, 4($k1)
#return to line after function call
jr $ra
addi $0, $0, 0
moveEnemy2:
li $t0, lightBrown
li $t4, darkBrown
lw $s2, 8($k0)
# first row
#sw $t0, 0($s2)
sw $t0, -4($s2)
sw $t0, -8($s2)
sw $t0, -12($s2)
#sw $t0, -16($s2)
#sw $t0, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# second row
sw $t0, 0($s2)
sw $t0, -4($s2)
sw $t4, -8($s2)
sw $t4, -12($s2)
sw $t0, -16($s2)
#sw $t0, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# third row
sw $t0, 0($s2)
sw $t0, -4($s2)
sw $t0, -8($s2)
sw $t4, -12($s2)
sw $t4, -16($s2)
sw $t0, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# fourth row
sw $t0, 0($s2)
sw $t4, -4($s2)
sw $t0, -8($s2)
sw $t0, -12($s2)
sw $t4, -16($s2)
sw $t0, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# fifth row
sw $t0, 0($s2)
sw $t0, -4($s2)
sw $t4, -8($s2)
sw $t0, -12($s2)
sw $t0, -16($s2)
sw $t0, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# sixth row
#sw $t0, 0($s2)
sw $t0, -4($s2)
sw $t0, -8($s2)
sw $t0, -12($s2)
sw $t0, -16($s2)
#sw $t0, -20($s2)
lw $t8, 8($k1)
addi $t8, $t8, -1
sw $t8, 8($k1)
#return to line after function call
jr $ra
addi $0, $0, 0
moveEnemy3:
li $t0, lightBrown
li $t4, darkBrown
lw $s3, 12($k0)
# first row
#sw $t0, 0($s3)
sw $t0, -4($s3)
sw $t0, -8($s3)
sw $t0, -12($s3)
#sw $t0, -16($s3)
#sw $t0, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# second row
sw $t0, 0($s3)
sw $t0, -4($s3)
sw $t4, -8($s3)
sw $t4, -12($s3)
sw $t0, -16($s3)
#sw $t0, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# third row
sw $t0, 0($s3)
sw $t0, -4($s3)
sw $t0, -8($s3)
sw $t4, -12($s3)
sw $t4, -16($s3)
sw $t0, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# fourth row
sw $t0, 0($s3)
sw $t4, -4($s3)
sw $t0, -8($s3)
sw $t0, -12($s3)
sw $t4, -16($s3)
sw $t0, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# fifth row
sw $t0, 0($s3)
sw $t0, -4($s3)
sw $t4, -8($s3)
sw $t0, -12($s3)
sw $t0, -16($s3)
sw $t0, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# sixth row
#sw $t0, 0($s3)
sw $t0, -4($s3)
sw $t0, -8($s3)
sw $t0, -12($s3)
sw $t0, -16($s3)
#sw $t0, -20($s3)
lw $t8, 12($k1)
addi $t8, $t8, -1
sw $t8, 12($k1)
#return to line after function call
jr $ra
addi $0, $0, 0
cleanEnemy1: #Draw over the obstacle (clear)
lw $s1, 4($k0)
# first row
#sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
#sw $k1, -16($s1)
#sw $k1, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# second row
sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
sw $k1, -16($s1)
#sw $k1, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# third row
sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
sw $k1, -16($s1)
sw $k1, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# fourth row
sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
sw $k1, -16($s1)
sw $k1, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# fifth row
sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
sw $k1, -16($s1)
sw $k1, -20($s1)
#set to next row, rightmost pixel
addi $s1, $s1, 128
# sixth row
#sw $k1, 0($s1)
sw $k1, -4($s1)
sw $k1, -8($s1)
sw $k1, -12($s1)
sw $k1, -16($s1)
#sw $k1, -20($s1)
#return to line after function call
jr $ra
addi $0, $0, 0
cleanEnemy2:
lw $s2, 8($k0)
# first row
#sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
#sw $k1, -16($s2)
#sw $k1, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# second row
sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
sw $k1, -16($s2)
#sw $k1, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# third row
sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
sw $k1, -16($s2)
sw $k1, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# fourth row
sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
sw $k1, -16($s2)
sw $k1, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# fifth row
sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
sw $k1, -16($s2)
sw $k1, -20($s2)
#set to next row, rightmost pixel
addi $s2, $s2, 128
# sixth row
#sw $k1, 0($s2)
sw $k1, -4($s2)
sw $k1, -8($s2)
sw $k1, -12($s2)
sw $k1, -16($s2)
#sw $k1, -20($s2)
#return to line after function call
jr $ra
addi $0, $0, 0
cleanEnemy3:
lw $s3, 12($k0)
# first row
#sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
#sw $k1, -16($s3)
#sw $k1, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# second row
sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
sw $k1, -16($s3)
#sw $k1, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# third row
sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
sw $k1, -16($s3)
sw $k1, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# fourth row
sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
sw $k1, -16($s3)
sw $k1, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# fifth row
sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
sw $k1, -16($s3)
sw $k1, -20($s3)
#set to next row, rightmost pixel
addi $s3, $s3, 128
# sixth row
#sw $k1, 0($s3)
sw $k1, -4($s3)
sw $k1, -8($s3)
sw $k1, -12($s3)
sw $k1, -16($s3)
#sw $k1, -20($s3)
#return to line after function call
jr $ra
addi $0, $0, 0
checkDespawn:
sw $ra, 0($k1) # save the return address
lw $t8, 4($k1)
blez $t8, RZ1 #loads and checks to see if any of the obstacles "distance until despawn" values are zero, and then jump to the spot to generate new ones
addi $0, $0, 0
C2: lw $t8, 8($k1)
blez $t8, RZ2
addi $0, $0, 0
C3: lw $t8, 12($k1)
blez $t8, RZ3
addi $0, $0, 0
C4: lw $ra, 0($k1) # restore this function's return adress
jr $ra #goes back to the drawnig loop
addi $0, $0, 0
RZ1: sw $0, 4($k0) #resets the position of the obstacle back to 0
jal enemyLocation #generates a new obstacle and puts the
addi $0, $0, 0
j C2 #jumps back up to check if any of the other obstacles need respawning
addi $0, $0, 0
RZ2: sw $0, 8($k0)
jal enemyLocation
addi $0, $0, 0
j C3
addi $0, $0, 0
RZ3: sw $0, 12($k0)
jal enemyLocation
addi $0, $0, 0
j C4
addi $0, $0, 0
end: # gracefully terminate the program
li $v0, 10
syscall