-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcd.asm
More file actions
63 lines (57 loc) · 871 Bytes
/
gcd.asm
File metadata and controls
63 lines (57 loc) · 871 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
60
61
62
63
# gcd.asm — Greatest common divisor for unsigned inputs (RV32I, RARS).
# In: a0/x10, a1/x11. Out: a0 = gcd, or -1 if either input is 0.
.globl gcd
gcd:
addi sp, sp, -4
sw ra, 0(sp)
beqz x10, invalid
beqz x11, invalid
beq x10, x11, equal
blt x10, x11, x10smaller
blt x11, x10, x11smaller
x10smaller:
li t0, 1
mv t2, x10
loop_head1:
bge t0, t2, loop_end1
rem t3, x11, t0
beqz t3, update1
j noupdate1
update1:
mv t4, t0
addi t0, t0, 1
j loop_head1
noupdate1:
addi t0, t0, 1
j loop_head1
loop_end1:
j print
x11smaller:
li t0, 1
mv t2, x11
loop_head2:
bge t0, t2, loop_end2
rem t3, x10, t0
beqz t3, update2
j noupdate2
update2:
mv t4, t0
addi t0, t0, 1
j loop_head2
noupdate2:
addi t0, t0, 1
j loop_head2
loop_end2:
j print
print:
mv a0, t4
j exit
equal:
mv a0, a1
j exit
invalid:
li a0, -1
exit:
lw ra, 0(sp)
addi sp, sp, 4
ret