-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds52384_B.asm
410 lines (178 loc) · 5.05 KB
/
ds52384_B.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
.ORIG x3000
BEGIN AND R6, R6, #0
AND R0, R0, #0 ;INITIALIZATION
AND R3, R3, #0
LD R0, CASE1 ;LOAD R0 WITH ADDRESS OF FIRST ENTRY
LD R3, CASE2 ;LOAD R3 WITH ADDRESS OF SECOND ENTRY
LOOP LDR R1, R0, #0 ;R1 IS LOADED WITH VALUE OF FIRST ENTRY
LD R7, BTMSK1 ;BTMSK 1:xFF00
AND R2, R1, R7 ;R2 CONTAINS ISOLATED BITS [15:8] OF FIRST ENTRY
BRnp WHILST ;WHILE THE FIRST ENTRY ID IS NOT 0, CHECK THE NEXT ENTRY
BRz SORT1 ;WHILE THE FIRST ENTRY IS 0, BEGIN HISTOGRAM
WHILST LDR R4, R3, #0 ;R4 CONTAINS THE VALUE AFTER THE FIRST ENTRY
AND R5, R4, R7 ;R5 CONTAINS ISOLATED BITS [15:8] OF THE SECOND ENTRY
BRnp IF ;WHILE THE SECOND ENTRY'S ID IS NOT 0, COMPARE
BRz CHECK ;REACHED END OF LIST, CLEAR AND CHECK NEXT SWAPS
CHECK ADD R6, R6, #0
BRz SORT1
BRnp BEGIN
IF LD R7, BTMSK2 ;BTMSK2: X00FF
AND R2, R1, R7 ;R2 NOW CONTAINS ISOLATED BITS [7:0] OF THE FIRST ENTRY
AND R5, R4, R7 ;R5 NOW CONTAINS ISOLATED BITS [7:0] OF THE SECOND ENTRY
NOT R5, R5 ;SUBTRACT SECOND SCORE FROM FIRST
ADD R5, R5, #1
ADD R5, R5, R2
BRn SWAP ;IF NEGATIVE, SWAP
ADD R3, R3, #1 ;IF POSITIVE OR ZERO, INCREMENT BOTH POINTERS
ADD R0, R0, #1
BRzp LOOP
SWAP STR R1, R3, #0 ;FIRST ENTRY IS STORED IN SECOND ENTRY'S PREVIOUS PLACE
STR R4, R0, #0 ;SECOND ENTRY IS STORED IN FIRST ENTRY'S PREVIOUS PLACE
ADD R0, R0, #1 ;INCREMENT ADDRESS OF FIRST ENTRY
ADD R6, R6, #1 ;ADD ONE TO SWAP COUNTER
ADD R3, R3, #1 ;INCREMENT ADDRESS OF SECOND ENTRY TO POINT DIRECTLY AFTER FIRST
BRnzp LOOP ;SORT AGAIN STARTING FROM NEXT ENTRY
;HISTOGRAM
SORT1 LD R6, BENCH
LD R0, CASE1
LD R5, HIST
LD R4, OFFSET
AND R7, R7, #0
AND R2, R2, #0
SORT LDR R1, R0, #0
LD R3, BTMSK1
AND R3, R1, R3
BRz STRRES
LD R3, BTMSK2
AND R1, R1, R3
ADD R1, R1, R6
BRn STRRES
ADD R0, R0, #1
ADD R7, R7, #1
BRzp SORT
STRRES STR R7, R5, #0
ADD R6, R6, R4
ADD R2, R2, #1
AND R7, R7, #0
ADD R5, R5, #-1
ADD R3, R2, #-5
BRn SORT
LD R0, HIST
LDR R1, R0, #0
LD R2, A
ADD R1, R2, R1
STR R1, R0, #0
ADD R0, R0, #-1
LDR R1, R0, #0
LD R2, B
ADD R1, R2, R1
STR R1, R0, #0
ADD R0, R0, #-1
LDR R1, R0, #0
LD R2, C
ADD R1, R2, R1
STR R1, R0, #0
ADD R0, R0, #-1
LDR R1, R0, #0
LD R2, D
ADD R1, R2, R1
STR R1, R0, #0
ADD R0, R0, #-1
LDR R1, R0, #0
LD R2, F
ADD R1, R2, R1
STR R1, R0, #0
AND R3, R3, #0
LD R0, CASE1 ;R0 POINTS TO FIRST ENTRY
STATS ADD R1, R0, #1 ;R1 POINTS TO NEXT ENTRY
LDR R2, R1, #0
LD R5, BTMSK1
AND R2, R2, R5 ;R2 HAS ID OF NEXT ENTRY
ADD R2, R2, #0
BRz COUNT
ADD R3, R3, #1
ADD R0, R0, #1
BRnzp STATS
;CALCULATE RANGE
COUNT LD R0, CASE1 ;R3 CONTAINS THE LENGTH OF THE ARRAY MINUS 1
LD R5, BTMSK2
LDR R1, R0, #0 ;R1 CONTAINS VALUE OF ENTRY 1
AND R6, R1, R5 ;R6 CONTAINS SCORE OF ENTRY 1
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
ADD R6, R6, R6
LD R2, RANGE ;R6 CONTAINS HIGHEST VALUE IN BITS [15:8]
ADD R2, R0, R3 ;R2 CONTAINS THE ADDRESS OF THE LAST ENTRY
LDR R4, R2, #0
AND R5, R4, R5 ;R5 CONTAINS SCORE OF LAST ENTRY
LD R1, RANGE
ADD R6, R6, R5
STR R6, R1, #0 ;RANGE CONTAINS LOWEST VALUE IN BITS [7:0]
;CALCULATE MEAN
LD R0, CASE1
LDR R1, R0, #0
LDR R2, R0, #1
LD R4, BTMSK2
AND R6, R1, R4 ;R6 CONTAINS FIRST ENTRY'S SCORE
AND R2, R2, R4 ;R2 CONTAINS NEXT ENTRY'S SCORE
SUM ADD R2, R2, R6 ;R2 NOW CONTAINS THE SUM OF THE NUMBERS PRECEDING IT
ADD R0, R0, #1 ;ENTRY 1 IS INCREMENTED
LDR R1, R0, #1
AND R6, R1, R4 ;R6 NOW CONTAINS THE NEXT ENTRY'S SCORE
LD R5, BTMSK1
AND R7, R1, R5
BRz DIVIDE
BRnp SUM
DIVIDE AND R6, R6, #0
AND R5, R5, #0
NOT R4, R3 ;R2 CONTAINS THE SUM OF ALL SCORES
ADD R4, R4, #1 ;R4 IS THE DIVISOR
KEEPDIV ADD R2, R4, R2
BRp COUNTR
BRnz STRMN
COUNTR ADD R6, R6, #1 ;R6 IS THE COUNTER
BRnzp KEEPDIV
STRMN AND R0, R0, #0
LD R0, MEAN
STR R6, R0, #0
;CALCULATE MEDIAN
LD R0, CASE1 ;R0 POINTS TO THE FIRST ADDRESS
LD R1, TWO
AND R2, R2, #0
AND R5, R5, #0
AND R6, R6, #0
MOVE NOT R2, R1
ADD R2, R2, #1 ;R2 CONTAINS NEGATIVE TWO
DIVIDE2 ADD R3, R2, R3 ;DIVISION BY 2
BRnz CALCMED
ADD R5, R5, #1
BRnzp DIVIDE2
CALCMED ADD R6, R0, R5
LDR R6, R6, #0
LD R4, BTMSK2
AND R6, R6, R4
LD R0, MEDIAN
STR R6, R0, #0
HALT
CASE1 .fill x4005
CASE2 .fill X4006
BTMSK1 .fill XFF00
BTMSK2 .fill X00FF
BENCH .fill #-80
HIST .fill X4004
OFFSET .fill #20
F .fill x4600
D .fill x4400
C .fill x4300
B .fill x4200
A .fill x4100
RANGE .fill x6000
MEAN .fill x6001
MEDIAN .fill x6002
TWO .fill x0002
.END