-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmaze.mips
147 lines (137 loc) · 2.89 KB
/
maze.mips
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
# Maze generator in 64-bit MIPS assembly language
# Joe Wingbermuehle
# 2017-08-27
# Compile with: gcc -x assembler maze.mips
# Size of the maze (must be odd).
.equ width, 39
.equ height, 23
.equ seed, 123
.section .text
.global main
main:
daddiu $sp, $sp, -32
sd $ra, 24($sp)
jal .initialize
ori $a0, $a0, seed
jal srand
dla $a0, .maze + width * 2 + 2
jal .carve
dla $t0, .maze + width + 2
ori $t1, $0, space_value
sb $t1, 0($t0)
dla $t0, .maze + width * (height - 1) - 3
sb $t1, 0($t0)
jal .draw
move $v0, $0
ld $ra, 24($sp)
daddiu $sp, $sp, 32
jr $ra
# Initialize the maze array
.initialize:
move $t0, $0
dla $t1, .maze
ori $t3, $0, width * height
.init_fill:
dadd $t8, $t0, $t1
sb $0, 0($t8)
daddiu $t0, $t0, 1
bne $t0, $t3, .init_fill
move $t0, $0
ori $t2, $0, space_value
ori $t3, $0, width
.init_top_bottom:
dadd $t8, $t0, $t1
sb $t2, width * height - width($t8)
sb $t2, 0($t8)
daddiu $t0, $t0, 1
bne $t0, $t3, .init_top_bottom
move $t0, $0
ori $t3, $0, width * height
.init_sides:
dadd $t8, $t0, $t1
sb $t2, width - 1($t8)
sb $t2, 0($t8)
daddiu $t0, $t0, width
bne $t0, $t3, .init_sides
jr $ra
# Carve the maze starting at a0 (a pointer into the maze array).
.carve:
daddiu $sp, $sp, -56
sd $ra, 24($sp)
sd $s0, 32($sp)
sd $s1, 40($sp)
sd $s2, 48($sp)
move $s0, $a0 # Maze pointer in $s0
ori $t0, $0, space_value
sb $t0, 0($s0)
jal rand
andi $s1, $v0, 3 # Direction in $s1
ori $s2, $0, 4 # Tries in $s2
.carve_loop:
dla $t0, .offsets
add $t0, $t0, $s1
lb $t0, 0($t0) # Offset in $t0
add $t1, $s0, $t0 # First address in $t1
add $a0, $t1, $t0 # Second address in $a0
lb $t2, 0($t1)
lb $t3, 0($a0)
or $t2, $t2, $t3
bne $t2, $0, .carve_next
ori $t0, $0, space_value
sb $t0, 0($t1)
jal .carve
.carve_next:
addi $s1, $s1, 1 # Next direction
andi $s1, $s1, 3
addi $s2, $s2, -1
bne $s2, $0, .carve_loop
ld $s0, 32($sp)
ld $s1, 40($sp)
ld $s2, 48($sp)
ld $ra, 24($sp)
daddiu $sp, $sp, 56
jr $ra
# Draw the maze.
.draw:
daddiu $sp, $sp, -56
sd $ra, 24($sp)
sd $s0, 32($sp)
sd $s1, 40($sp)
sd $s2, 48($sp)
dla $s0, .maze
move $s2, $0
.draw_line:
move $s1, $0
.draw_cell:
lb $t0, 0($s0)
dla $a0, .wall
add $a0, $a0, $t0
jal printf
addiu $s1, $s1, 1
ori $t0, width
addiu $s0, $s0, 1
bne $s1, $t0, .draw_cell
dla $a0, .nl
jal printf
addiu $s2, $s2, 1
ori $t0, height
bne $s2, $t0, .draw_line
ld $s0, 32($sp)
ld $s1, 40($sp)
ld $s2, 48($sp)
ld $ra, 24($sp)
daddiu $sp, $sp, 56
jr $ra
.section .rodata
.offsets:
.byte -1, 1, -width, width
.nl: .asciz "\n"
.wall: .asciz "[]"
.space: .asciz " "
msg: .asciz "test!\n"
# The size of the ".wall" is used for spaces.
# This allows us to just add this value to get to the
# right string to display when drawing.
.equ space_value, .space - .wall
.section .bss
.comm .maze, width * height