-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.asm
More file actions
59 lines (50 loc) · 867 Bytes
/
insert.asm
File metadata and controls
59 lines (50 loc) · 867 Bytes
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
# insert.asm — Insertion sort of 1024 32-bit words (RV32I, RARS).
# In: a0 = output buffer, a1 = input buffer (1024 words). Out: sorted data in a0.
# O(n²) passes over 1024 elements; expect noticeable runtime in RARS.
.globl insert
insert:
addi sp, sp, -4
sw ra, 0(sp)
mv s9, a0
mv s8, a1
lw t0, 0(a1)
sw t0, 0(a0)
li t0, 0
forloop:
addi a1, a1, 4
slli s4, t0, 2
add a0, s9, s4
addi t0, t0, 1
li t5, 1024
beq t0, t5, end
lw t1, 0(a1)
mv t2, t0
addi t2, t2, -1
li s1, 0
whileloop:
addi a1, a1, -4
lw t3, 0(a0)
addi s1, s1, 1
blt t3, t1, updatekey
bltz t2, updatekey
addi a0, a0, 4
sw t3, 0(a0)
addi t2, t2, -1
addi a0, a0, -8
j whileloop
updatekey:
addi a0, a0, 4
sw t1, 0(a0)
slli s1, s1, 2
add a1, a1, s1
j forloop
end:
mv a0, s9
mv a1, s8
li s1, 0
li s4, 0
li s8, 0
li s9, 0
lw ra, 0(sp)
addi sp, sp, 4
ret