-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProject.asm
More file actions
683 lines (536 loc) · 9.62 KB
/
Copy pathProject.asm
File metadata and controls
683 lines (536 loc) · 9.62 KB
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
.begin
BASE .equ 0x3fffc0
COUT .equ 0x0
COTSTAT .equ 0x4
CIN .equ 0x8
CINSTAT .equ 0xc
Oct .equ 0x7
Hex .equ 0xf
Selct .equ 3980
Array .equ 4000
Menu .equ 2048
Number .equ 3800
InfoH .equ 2500
InfoQ .equ 2600
Soted .equ 2700
Upper .equ 2800
Lower .equ 2900
InfoOct .equ 3000
InfoHex .equ 3100
Greet .equ 3200
.org 10000
!####Macro Program####
!-----------------------------------------------
.macro pop r
ld %r14, r
add %r14, 4, %r14
.endmacro
.macro push r
sub %r14, 4, %r14
st r, %r14
.endmacro
.macro pushI num
sub %r14, 4, %r14
mov num, %r31
st %r31, %r14
.endmacro
.macro store r, num
sub r, 4, r
st num, r
.endmacro
.macro init
clr %r30
sethi 0x3fffc0, %r30
sethi 0x200000, %r14
.endmacro
.macro return X
pop %r1
.if X > 0
push %rX
.endif
jmpl %r1+4, %r0
.endmacro
.macro left r1, r2
sll r1, 1, r2
add r2, 4, r2
.endmacro
.macro right r1, r2
sll r1, 1, r2
add r2, 8, r2
.endmacro
.macro swap r1, r2, r3
ld [r1+r3], %r28
ld [r2+r3], %r27
st %r27, [r1+r3]
st %r28, [r2+r3]
.endmacro
.macro save
push %r1
push %r2
push %r3
push %r4
push %r5
push %r6
push %r7
push %r8
push %r9
.endmacro
.macro load
pop %r9
pop %r8
pop %r7
pop %r6
pop %r5
pop %r4
pop %r3
pop %r2
pop %r1
.endmacro
!-----------------------------------------------
!####Main Function####
!-----------------------------------------------
init
pushI Greet
call Put
pushI Array !Read data
call Read
call Select
pushI Soted !Sort and select
call Put
pushI Array
call Put
pushI Array !To uppercase
call Uppercase
pushI Upper
call Put
pushI Array
call Put
pushI Array !Get summary
call Sum
pop %r1
push %r1
push %r1
pushI Number !Translate a number to Octal
call GetOctal
pushI InfoOct
call Put
call Put
pushI Number !Translate a number to Hexadecimal
call GetHex
pushI InfoHex
call Put
call Put
halt
!-----------------------------------------------
!####Read Data Function####
!-----------------------------------------------
Read:
clr %r2
pop %r4
push %r15
WaitInput:
ldub [%r30+CINSTAT], %r1
andcc %r1, 0x80, %r1
be WaitInput
ldub [%r30+CIN], %r3
st %r3, [%r2+%r4]
cmp %r3, 27 !If this character is ESC, end input
be EndInput
PutChar:
ldub [%r30+COTSTAT], %r1 !if read a character then print to the screen
andcc %r1, 0x80, %r1
be PutChar
stb %r3, [%r30+COUT]
add %r2, 4, %r2
ba WaitInput
EndInput:
st %r0, [%r2+%r4] !Change the last character to '\0'
return 2
!-----------------------------------------------
!####Put Data Function####
!-----------------------------------------------
Put:
pop %r4
clr %r5
push %r15
WaitOutput:
ldub [%r30+COTSTAT], %r1
andcc %r1, 0x80, %r1
be WaitOutput
ld [%r4+%r5], %r3
cmp %r3, %r0 !If the character not is '\0', mean the string is not end
bne NotEnd
mov 0xa, %r3 !When the character is '\0', then print '\n' to the screen
stb %r3, [%r30+COUT]
ba EndPut
NotEnd:
stb %r3, [%r30+COUT]
add %r5, 4, %r5
ba WaitOutput
EndPut:
return 0
!-----------------------------------------------
!####Selection Function####
!-----------------------------------------------
Select:
pop %r4
push %r15
save !Print menu to screen
pushI Menu
call Put
load
WaitSelect:
ldub [%r30+CINSTAT], %r1
andcc %r1, 0x80, %r1
be WaitSelect
ldub [%r30+CIN], %r3
cmp %r3, 49
be TypeOne
cmp %r3, 50
be TypeTwo
ba WaitSelect
TypeOne:
save
pushI InfoQ
call Put
load
add %r4, Array, %r5
sub %r5, 4, %r5
pushI Array
push %r5
call QuickSort
return 0
TypeTwo:
save
pushI InfoH
call Put
load
pushI Array
push %r4
call HeapSort
return 0
!-----------------------------------------------
!####Heap Srot Function####
!-----------------------------------------------
HeapSort:
pop %r8
pop %r2
push %r15
save
push %r2
push %r8
call CreateHeap !To make a heap
load
MakeSort:
cmp %r8, %r0
be EndSort !When the heap is empty, end the loop
sub %r8, 4, %r7
swap %r0, %r7, %r2 !Delete the root node and move last node to the root node
sub %r8, 4, %r8 !Reduce the size of heap by four
save
push %r2
push %r0
push %r8
call DropNode !To adjust the new root node
load
ba MakeSort
EndSort:
return 0
!-----------------------------------------------
!####Create Heap Function####
!-----------------------------------------------
CreateHeap:
pop %r8
pop %r2
push %r15
srl %r8, 3, %r1
sll %r1, 2, %r1
Adjust:
save
push %r2
push %r1
push %r8
call DropNode
load
cmp %r1, %r0
be EndCreat
sub %r1, 4, %r1
ba Adjust
EndCreat:
return 0
!-----------------------------------------------
!####Drop Node Function####
!-----------------------------------------------
DropNode:
pop %r9
pop %r1
pop %r2
push %r15
mov %r1, %r3
left %r3, %r5
right %r3, %r6
cmp %r5, %r9
bge EndDrop
ld [%r3+%r2], %r7
ld [%r5+%r2], %r8
cmp %r7, %r8
bge LeftInvaid
mov %r5, %r3
LeftInvaid:
cmp %r6, %r9
bge RightInvaid
ld [%r3+%r2], %r7
ld [%r6+%r2], %r8
cmp %r7, %r8
bge RightInvaid
mov %r6, %r3
RightInvaid:
cmp %r3, %r1
be EndDrop
push %r2
push %r3
push %r9
swap %r1, %r3, %r2
call DropNode
EndDrop:
return 0
!-----------------------------------------------
!####Quick Sort Function####
!-----------------------------------------------
QuickSort:
pop %r4
pop %r3
push %r15
ld %r3, %r5
mov %r3, %r1
mov %r4, %r2
WhileA:
cmp %r1, %r2
be DoneA
WhileB:
cmp %r1, %r2
be DoneB
ld %r2, %r6
cmp %r5, %r6
bg DoneB
sub %r2, 4, %r2
ba WhileB
DoneB:
ld %r2, %r7
st %r7, %r1
WhileC:
cmp %r1, %r2
be DoneC
ld %r1, %r6
cmp %r5, %r6
bl DoneC
add %r1, 4, %r1
ba WhileC
DoneC: ld %r1, %r7
st %r7, %r2
ba WhileA
DoneA:
st %r5, %r1
add %r1, 4, %r5
sub %r1, 4, %r6
cmp %r3, %r6
bge NotLeft
save
push %r3
push %r6
call QuickSort !To sort the left
load
NotLeft:
cmp %r4, %r5
ble NotRight
push %r5
push %r4
call QuickSort !To sort the right
NotRight:
return 0
!-----------------------------------------------
!####To Uppercase Function####
!-----------------------------------------------
Uppercase:
pop %r2
push %r15
UpperLoop:
ld %r2, %r3
cmp %r3, %r0
be EndUpper
cmp %r3, 97
bl SkipUpper
cmp %r3, 122
bg SkipUpper
sub %r3, 32, %r3
st %r3, %r2
SkipUpper:
add %r2, 4, %r2
ba UpperLoop
EndUpper:
return 0
!-----------------------------------------------
!####To Lowercase Function####
!-----------------------------------------------
Lowercase:
pop %r2
push %r15
LowerLoop:
ld %r2, %r3
cmp %r3, %r0
be EndLower
cmp %r3, 65
bl SkipLower
cmp %r3, 90
bg SkipLower
add %r3, 32, %r3
st %r3, %r2
SkipLower:
add %r2, 4, %r2
ba LowerLoop
EndLower:
return 0
!-----------------------------------------------
!####Get Octal Number Function####
!-----------------------------------------------
GetOctal:
pop %r2
pop %r1
store %r2, %r0
push %r15
DivideOctal:
cmp %r1, %r0
be EndOctal
and %r1, Oct, %r3
add %r3, 48, %r3
store %r2, %r3
srl %r1, 3, %r1
ba DivideOctal
EndOctal:
return 2
!-----------------------------------------------
!####Get Hexadecimal Number Function####
!-----------------------------------------------
GetHex:
pop %r2
pop %r1
store %r2, %r0
push %r15
DivideHex:
cmp %r1, %r0
be EndHex
and %r1, Hex, %r3 !Get the last four bits
cmp %r3, 10
bl LessTen
add %r3, 7, %r3 !Translate to character
LessTen:
add %r3, 48, %r3
store %r2, %r3
srl %r1, 4, %r1
ba DivideHex
EndHex:
return 2
!-----------------------------------------------
!####Get Sum Function####
!-----------------------------------------------
Sum:
pop %r2
clr %r4
push %r15
GetNext:
ld %r2, %r3
cmp %r3, %r0
be EndSum
add %r3, %r4, %r4
add %r2, 4, %r2
ba GetNext
EndSum:
return 4
!-----------------------------------------------
.org 2048
!####Menu####
!-----------------------------------------------
0x0a
0x53,0x65,0x6c,0x65
0x63,0x74,0x20,0x74
0x68,0x65,0x20,0x73
0x6f,0x72,0x74,0x69
0x6e,0x67,0x20,0x61
0x6c,0x67,0x6f,0x72
0x69,0x74,0x68,0x6d
0x0a,0x31,0x2e,0x20
0x51,0x75,0x69,0x63
0x6b,0x73,0x6f,0x72
0x74,0x0a,0x32,0x2e
0x20,0x48,0x65,0x61
0x70,0x73,0x6f,0x72
0x74
0x00
!-----------------------------------------------
.org 2500
!####Information Heapsort####
!-----------------------------------------------
0x48,0x65,0x61,0x70
0x20,0x73,0x6f,0x72
0x74,0x69,0x6e,0x67
0x00
!-----------------------------------------------
.org 2600
!####Information Quicksort####
!-----------------------------------------------
0x51,0x75,0x69,0x63
0x6b,0x20,0x73,0x6f
0x72,0x74,0x69,0x6e
0x67
0x00
!-----------------------------------------------
.org 2700
!####Information Soted####
!-----------------------------------------------
0x53,0x6f,0x72,0x74
0x65,0x64,0x3a
0x00
!-----------------------------------------------
.org 2800
!####Information Uppercase####
!-----------------------------------------------
0x55,0x70,0x70,0x65
0x72,0x63,0x61,0x73
0x65,0x3a
0x00
!-----------------------------------------------
.org 2900
!####Information Lowercase####
!-----------------------------------------------
0x4c,0x6f,0x77,0x65
0x72,0x63,0x61,0x73
0x65,0x3a
0x00
!-----------------------------------------------
.org 3000
!####Information Oct####
!-----------------------------------------------
0x4f,0x63,0x74,0x61
0x6c,0x3a
0x00
!-----------------------------------------------
.org 3100
!####Information Hex####
!-----------------------------------------------
0x48,0x65,0x78,0x61
0x64,0x65,0x63,0x69
0x6d,0x61,0x6c,0x3a
0x00
!-----------------------------------------------
.org 3200
!####Information greet####
!-----------------------------------------------
0x50,0x6c,0x65,0x61
0x73,0x65,0x20,0x65
0x6e,0x74,0x65,0x72
0x20,0x61,0x20,0x73
0x74,0x72,0x69,0x6e
0x67,0x3a
0x00
!-----------------------------------------------
.end