-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathematics.s
323 lines (247 loc) · 4.43 KB
/
mathematics.s
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
# YOUR-NAME-HERE, DD/MM/YYYY
########################################################################
# .DATA
# Here are some handy strings for use in your code.
.data
prompt_str:
.asciiz "Enter a random seed: "
result_str:
.asciiz "The random result is: "
########################################################################
# .TEXT <main>
.text
main:
# Args: void
# Returns: int
#
# Frame: [...]
# Uses: [...]
# Clobbers: [...]
#
# Locals:
# - ...
#
# Structure:
# - main
# -> [prologue]
# -> [body]
# -> [epilogue]
main__prologue:
begin
push $ra
push $s0
# TODO: add code to set up your stack frame here
main__body:
la $a0, prompt_str
li $v0, 4
syscall
li $v0, 5
syscall
move $a0, $v0
jal seed_rand
li $a0, 100
jal rand
move $s0, $v0
move $a0, $v0
jal add_rand
move $a0, $v0
jal sub_rand
move $a0, $v0
jal seq_rand
move $t0, $v0
la $a0, result_str
li $v0, 4
syscall
move $a0, $t0
li $v0, 1
syscall
li $a0, '\n'
li $v0, 11
syscall
main__epilogue:
# TODO: add code to clean up stack frame here
pop $s0
pop $ra
end
li $v0, 0
jr $ra # return 0;
########################################################################
# .TEXT <add_rand>
.text
add_rand:
# Args:
# - $a0: int value
# Returns: int
#
# Frame: [...]
# Uses: [...]
# Clobbers: [...]
#
# Locals:
# - ...
#
# Structure:
# - add_rand
# -> [prologue]
# -> [body]
# -> [epilogue]
add_rand__prologue:
begin
push $ra
push $a0
# TODO: add code to set up your stack frame here
add_rand__body:
li $a0, 0xFFFF
jal rand
pop $a0
add $v0, $v0, $a0
add_rand__epilogue:
pop $ra
end
jr $ra
########################################################################
# .TEXT <sub_rand>
.text
sub_rand:
# Args:
# - $a0: int value
# Returns: int
#
# Frame: [...]
# Uses: [...]
# Clobbers: [...]
#
# Locals:
# - ...
#
# Structure:
# - sub_rand
# -> [prologue]
# -> [body]
# -> [epilogue]
sub_rand__prologue:
begin
push $ra
push $a0
# TODO: add code to set up your stack frame here
sub_rand__body:
jal rand
sub_rand__epilogue:
pop $a0
sub $v0, $a0, $v0
pop $ra
# TODO: add code to clean up stack frame here
end
jr $ra
########################################################################
# .TEXT <seq_rand>
.text
seq_rand:
# Args:
# - $a0: int value
# Returns: int
#
# Frame: [...]
# Uses: [...]
# Clobbers: [...]
#
# Locals:
# - ...
#
# Structure:
# - seq_rand
# -> [prologue]
# -> [body]
# -> [epilogue]
seq_rand__prologue:
begin
push $ra
push $a0
seq_rand__body:
li $a0, 100
jal rand
move $t1, $v0
pop $a0
li $t0, 0
loop_cond:
bge $t0, $t1, loop_end
loop_body:
push $t0
jal add_rand
move $a0, $v0
b loop_increment
loop_increment:
pop $t0
addi $t0, $t0, 1
b loop_cond
loop_end:
move $v0, $a0
b seq_rand__epilogue
seq_rand__epilogue:
pop $ra
# TODO: add code to clean up stack frame here
end
jr $ra
##
## The following are two utility functions, provided for you.
##
## You don't need to modify any of the following,
## but you may find it useful to read through.
## You'll be calling these functions from your code.
##
OFFLINE_SEED = 0x7F10FB5B
########################################################################
# .DATA
.data
# int random_seed;
.align 2
random_seed:
.space 4
########################################################################
# .TEXT <seed_rand>
.text
seed_rand:
# DO NOT CHANGE THIS FUNCTION
# Args:
# - $a0: unsigned int seed
# Returns: void
#
# Frame: []
# Uses: [$a0, $t0]
# Clobbers: [$t0]
#
# Locals:
# - $t0: offline_seed
#
# Structure:
# - seed_rand
li $t0, OFFLINE_SEED # const unsigned int offline_seed = OFFLINE_SEED;
xor $t0, $a0 # random_seed = seed ^ offline_seed;
sw $t0, random_seed
jr $ra # return;
########################################################################
# .TEXT <rand>
.text
rand:
# DO NOT CHANGE THIS FUNCTION
# Args:
# - $a0: unsigned int n
# Returns:
# - $v0: int
#
# Frame: []
# Uses: [$a0, $v0, $t0]
# Clobbers: [$v0, $t0]
#
# Locals:
# - $t0: int rand
#
# Structure:
# - rand
lw $t0, random_seed # unsigned int rand = random_seed;
multu $t0, 0x5bd1e995 # rand *= 0x5bd1e995;
mflo $t0
addiu $t0, 12345 # rand += 12345;
sw $t0, random_seed # random_seed = rand;
remu $v0, $t0, $a0
jr $ra # return rand % n;