-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpnum.asm
More file actions
53 lines (48 loc) · 758 Bytes
/
npnum.asm
File metadata and controls
53 lines (48 loc) · 758 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
# npnum.asm — Nth prime (RV32I, RARS).
# In: unsigned N in a0/x10. Out: a0 = Nth prime, or -1 if N == 0.
# Trial division; runtime grows quickly with N (e.g. ~tens of thousands is slow).
.globl npnum
npnum:
addi sp, sp, -4
sw ra, 0(sp)
beqz x10, valuezero
li t1, 1
li t2, 1
loop:
ble t1, x10, label1
j print
label1:
li t3, 2
li t4, 3
beq t2, t3, true
beq t2, t4, true
rem t5, t2, t3
beqz t5, false
blt t2, t3, false
div t3, t2, t3
addi t3, t3, 1
loop_head:
bge t4, t3, loop_end
rem t5, t2, t4
beqz t5, false
addi t4, t4, 2
j loop_head
loop_end:
j true
true:
mv a2, t2
addi t1, t1, 1
addi t2, t2, 1
j loop
false:
addi t2, t2, 1
j loop
print:
mv a0, a2
j exit
valuezero:
li a0, -1
exit:
lw ra, 0(sp)
addi sp, sp, 4
ret