-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project.asm
488 lines (355 loc) · 10.6 KB
/
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
.data
.globl map
.globl temp
.globl W
.globl H
.globl PlayerPos
.globl total
W: .word 21
H: .word 11
total: .word 231
PlayerPos: .word 1
temp: .space 100
##################################################
#strings
Labyrinth : .asciiz "\nLabyrinth: \n"
enter: .asciiz "\n"
map: .asciiz "I.IIIIIIIIIIIIIIIIIIII....I....I.......I.IIII.IIIII.I.I.III.I.II.I.....I..I..I.....II.I.III.II...II.I.IIII...I...III.I...I...IIIIII.IIIII.III.III.II.............I.I...IIIIIIIIIIIIIIII.I.III@...............I..IIIIIIIIIIIIIIIIIIIIIII"
wincase: .asciiz "\nWinner Winner Chicken Dinner"
positionchange: .asciiz "\nWhere you wanna go? "
a : .asciiz "a"
notvalid: .asciiz "\nnot valid call\n"
I: .asciiz "I"
.text
.globl main
main:
jal printLabyrinth
jal findPath
li $v0, 10
syscall
#--------------------------------------------------------------------------------------------------------------#
printLabyrinth:
# load the adress of the temp array so that we can do what the statement asks
la $t3, temp # stores in the register t3 the address of the first element of the temp array
la $t4, map # same as here
#now we print the labyrinth
li $v0, 4
la $a0, Labyrinth
syscall
li $t0, 0 # initiate the i counter
li $t2, 0 # inititate the k counter
lw $s0, H #load the H from the data segment to use it for comparison
#statement
whilelabel1:
beq $t0, $s0 , end1 # for 1
lw $s1, W # load the W from the data segment to use it for comparison
li $t1, 0 # initiation of the j counter
whilelabel2: #second for (nested)
beq $t1, $s1, end2
# we load playerPos to use it in the if statement
lw $s2, PlayerPos
# now we write the if statement
bne $t2, $s2, elselabel
la $t3, temp # we reset the t3 register to show again on the right spot(first element) so we can move to the right position
#now i have to find the temp[playerPos] and make this position P instead of '.'
add $t3, $t3, $t1 # we go to the address that the temp[j] exists
# we put the letter P in a register so we can use it right after to store it in the temp array
li $t5, 0
add $t5, $t5, 80
sb $t5, 0($t3) # we make the P (in ascii code) to the adress that is currently in the t3 register
j afteriflabel
elselabel:
# we reset the t3 and t4 registers so they dont take other values than the expected ones.
la $t3, temp
la $t4, map
add $t3, $t3, $t1 #make the address of t3 register, the access of the element we want to have
add $t4, $t4, $t2 #same for the map so we can do the processing after
lb $t4, 0($t4) # we take the whole element of the specific address so we can store it right after in the temp array
# now the only thing we have to do is store the
sb $t4, 0($t3) # stores the whole element in the address of the temp array
afteriflabel:
# what we have to do in that function is to increase the counter's values (k, j)
addi $t2, $t2, 1 #k increases by one
addi $t1, $t1, 1 # j increases by one
j whilelabel2
end2:
# now we print the temp we have created ... because of the asciiz on the string we dont need to create another element to put the 0 in it in the temp so we are ok
li $v0, 4
la $a0, temp
syscall
# in the end we want to press enter so the next line prints in the lane right above
li $v0, 4
la $a0, enter
syscall
addi $t0, $t0, 1 # we increase i's value
j whilelabel1 # we do this again and again util the statement above is fullfilled
end1:
jr $ra # return
#----------------------------------------------------------------------------------------------#
makeMove:
# in this function there is a need for stack so we put all the return adresses. We create a stack that in the first element there is the
addi $sp, $sp, -8
sw $ra, ($sp)
sw $s0, 4($sp)
lw $s0, PlayerPos
lw $s1, total
#we dont have the ability to have two statements simultaneously so we write the one after the other
bgez $s0, notreturn0label
blt $s0, $s1, notreturn0label
#when the process returns 0 the ra of the final makemove will return to the 2nd final make move ect
return0:
li $v0, 0 # so that the process returns to 0
lw $s0, 4($sp) #return the playerpos of the 2nd final make move
lw $ra, 0($sp) #returns the ra that is in the first block of the stack
addi $sp, $sp, 8 # deletes the list
jr $ra
notreturn0label:
#we need to load the map again and see the element inside
la, $t1, map
add $t1, $t1, $s0 # move to the right position of the map
#now we have to compare it with the dot. so we use a register to put the dot (ascii code) inside and make the comparison
li $t2, 0 #initiation so the ascii code can be stored
add $t2, $t2, 46 # 46 is the ascii code for the dot
#create the opossite statement compared to c
bne $t1, $t2 elseiflabel
# we reset the map again and then it is time to put the * in the map[playerpos]
la $t1, map
add $t1, $t1, $s0 # now we are at the map[playerPos]
#we create the * in ascii code
li $t4, 0 # we initiate the t4 to put the star in there
add $t4, $t4, 42 # ascii code for the star
sb $t4, 0($t1)
jal printLabyrinth #prints the labyrinth with the * in the playerpos instead of a dot
addi $s0, $s0, 1 # we move the playerPos so the character can go rigth
jal makeMove
beq $v0, $zero, upwards
li $t5, 0
add $t5, $t5, 35
la $t1, map
add $t1, $t1, $s0
sb $t5, 0($t1)
jal printLabyrinth
li $v0, 1 # sends the v0 back
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8 # resets the sp to the previous position
jr $ra
upwards:
lw $t7, W
sub $s0, $s0, $t7 # we move the playerPos so the character can go rigth
jal makeMove
beq $v0, $zero, downwards
li $t5, 0
add $t5, $t5, 35
la $t1, map
add $t1, $t1, $s0
sb $t5, 0($t1)
jal printLabyrinth
li $v0, 1 # sends the v0 back
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8 # resets the sp to the previous position
jr $ra
downwards:
lw $t7, W
add $s0, $s0, $t7 # we move the playerPos so the character can go rigth
jal makeMove
beq $v0, $zero, left
li $t5, 0
add $t5, $t5, 35
la $t1, map
add $t1, $t1, $s0
sb $t5, 0($t1)
jal printLabyrinth
li $v0, 1 # sends the v0 back
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8 # resets the sp to the previous position
jr $ra
left:
li $t7, -1
add $s0, $s0, $t7 # we move the playerPos so the character can go rigth
jal makeMove
beq $v0, $zero, right # case the return is 0 so we can go to the next condition
li $t5, 0
add $t5, $t5, 35
la $t1, map
add $t1, $t1, $s0
sb $t5, 0($t1)
jal printLabyrinth
li $v0, 1 # sends the v0 back
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8 # resets the sp to the previous position
jr $ra
right:
addi $s0, $s0, 1 # we move the playerPos so the character can go rigth
jal makeMove
beq $v0, $zero return0
li $t5, 0
add $t5, $t5, 35
la $t1, map
add $t1, $t1, $s0
sb $t5, 0($t1)
jal printLabyrinth
li $v0, 1 # sends the v0 back
lw $ra, 0($sp)
lw $s0, 4($sp)
addi $sp, $sp, 8 # resets the sp to the previous position
jr $ra
elseiflabel: # case the playerpos element is in the @
li $v0, 10
syscall
#-------------------------------------------------------------------------------------------#
findPath:
# we break the while in two if statements that happen one after another
#first we check that the map[playerpos] is not at the @
while:
# we load the map and the playerPos to find the specific location of the map
la $t3, map
la $t2, PlayerPos
add $t3, $t2, $t3 # we do this to go to the playerPos element of the map
# now we have to load the playerPos element of the map
# now we make the @ trough ascii
li $t1, 0
add $t1, $t1, 64
# first statement
beq $t3, $t1, endloop
li $v0, 4
la $a0, positionchange
syscall
polling:
li $s1, 0xffff0000
li $s2, 0xffff0004
andi $s1, $s1, 1
beq $s1, $zero, polling
lw $v0, 0($s2)
move $t5, $v0
# we move the char to a $t5 register and we create the e to go to the end loop
li $t6, 0 # char initiation
add $t6, $t6, 101 # e char with ascii
#now we create all the possible letters with ascii code to
li $t4, 0 #creation of the w
add $t4, $t4, 119
li $t7, 0 #creation of the s
add $t7, $t7, 115
li $t8, 0 # creation of the a
add $t8, $t8, 97
li $t9, 0 # creation of the d
add $t9, $t9, 100
beq $t5, $t6, endloop
beq $t5, $t4 casew
beq $t5, $t7 cases
beq $t5, $t8 casea
beq $t5, $t9 cased
li $v0, 4
la $a0, notvalid
syscall
j while
# now we build the cases
casew:
la $t3, map # take the map and put it into a register
lw $s1, PlayerPos
la $s2, 0 # char initiation
add $s2, $s2, 46
la $s3, 0
li $s3, 64
addi $s1, $s1, -21 # make the playerPos-W
add $t3, $s1, $t3 # now we are at the playerPos-W element
lb $t3, 0($t3)
beq $t3, $s3 ,winnercasew
bne $t3, $s2 ,notvalidmovew
sb $s1, PlayerPos
jal printLabyrinth
j while
notvalidmovew:
li $v0, 4
la $a0, notvalid
syscall
j while
winnercasew:
li $v0, 4
la $a0, wincase
syscall
li $v0, 10
syscall
cases:
la $t3, map # take the map and put it into a register
lw $s1, PlayerPos
la $s2, 0 # char initiation
add $s2, $s2, 46
la $s3, 0
li $s3, 64
addi $s1, $s1, 21 # make the playerPos+W
add $t3, $s1, $t3 # now we are at the playerPos+W element
lb $t3, 0($t3)
beq $t3, $s3, winnercases
bne $t3, $s2 ,notvalidmoves
sb $s1, PlayerPos
jal printLabyrinth
j while
notvalidmoves:
li $v0, 4
la $a0, notvalid
syscall
j while
winnercases:
li $v0, 4
la $a0, wincase
syscall
li $v0, 10
syscall
casea:
la $t3, map # take the map and put it into a register
lw $s1, PlayerPos
la $s2, 0 # char initiation
add $s2, $s2, 46
la $s3, 0
li $s3, 64
addi $s1, $s1, -1 # make the playerPos-1
add $t3, $s1, $t3 # now we are at the playerPos-1 element
lb $t3, 0($t3)
beq $t3, $s3 ,winnercasea
bne $t3, $s2, notvalidmovea
sb $s1, PlayerPos
jal printLabyrinth
j while
notvalidmovea:
li $v0, 4
la $a0, notvalid
syscall
j while
winnercasea:
li $v0, 4
la $a0, wincase
syscall
li $v0, 10
syscall
cased:
la $t3, map # take the map and put it into a register
lw $s1, PlayerPos
la $s2, 0 # char initiation
add $s2, $s2, 46
la $s3, 0
li $s3, 64
addi $s1, $s1, +1 # make the playerPos+1
add $t3, $s1, $t3 # now we are at the playerPos+1 element
lb $t3, 0($t3)
beq $t3, $s3,winnercased
bne $t3, $s2, notvalidmoved
sb $s1, PlayerPos
jal printLabyrinth
j while
notvalidmoved:
li $v0, 4
la $a0, notvalid
syscall
j while
winnercased:
li $v0, 4
la $a0, wincase
syscall
li $v0, 10
syscall
endloop:
jr $ra