-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.asm
More file actions
85 lines (78 loc) · 1.54 KB
/
sort.asm
File metadata and controls
85 lines (78 loc) · 1.54 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
# sort.asm — In-place selection sort on word array (RV32I, RARS).
# In: a0/x10 = base address, a1/x11 = length. Array is modified in memory.
.globl sort
sort:
addi sp, sp, -4
sw ra, 0(sp)
li t0, 0
sortmain:
addi s2, x11, -1
beq t0, s2, exit
li s7, 0
li s8, 0
li s9, 0
li s10, 0
li s11, 0
add t0, t0, x0
mv t1, x10
mv s9, x11
sortActual:
li t3, 0
findarri:
beq t0, t3, loadi # check if outerloop counter and i are equal, then load i
addi t1, t1, 4 # increment array address by 4
addi t3, t3, 1 # increment outerloop counter by 1
j findarri
loadi:
lw s7, 0(t1) #load arr[i] into s7
mv s8, s7
li t3, 0 #reset outerloop counter to t3
addi t4, t0, 1 # set j = i+1
mv t5, t0 # set innerloop counter = i since thats where array address is at
j findarrj
findarrjprep:
addi t4, t4, 1
beq s9, t4, swap
findarrj:
beq t5, t4, loadj
addi t1, t1, 4
addi t5, t5, 1
j findarrj
loadj:
lw s11, 0(t1)
blt s11, s8, storememory
j findarrjprep
storememory:
mv s10, t1
mv s8, s11
j findarrjprep
swap:
beq s10, x0, notlessthan
sw s7, 0(s10)
mv t1, x10
li t3, 0
findarriswap:
beq t0, t3, loadiswap # check if outerloop counter and i are equal, then load i
addi t1, t1, 4 # increment array address by 4
addi t3, t3, 1 # increment outerloop counter by 1
j findarriswap
loadiswap:
sw s8, 0(t1)
neg s3, t0
slli s3, s3, 2
add t1, t1, s3
mv x10, t1
addi t0, t0, 1
j sortmain
notlessthan:
addi t4, t4, -1
neg s3, t4
slli s3, s3, 2
add t1, t1, s3
mv x10, t1
addi t0, t0, 1
j sortmain
exit:
lw ra, 0(sp)
addi sp, sp, 4
ret