-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.s
80 lines (75 loc) · 1.75 KB
/
functions.s
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
#function to find the index of a number in array sequentially
#-----inputs:
#a0 = array address,
#a1 = numbert to find,
#a2 = array len;
#-----outputs:
#a0 = index of the number
#-----used registers:
#t1 = index to scroll the array
#t2 = the value in a[t1]
#-----used labels:
#ls1 = loop to scroll array
#els_err = return if the number isn't in array
#sFound = return the index of the value
sequential_search:
li t1, 0
lb t2, 0(a0)
ls1:
bge t1, a2, els_err
beq t2, a1, sFound
addi t1, t1, 1
add t2, a0, t1
lb t2, 0(t2)
beq zero, zero, ls1
els_err:
li a0, -1
jalr zero, ra
sFound:
mv a0, t1
jalr zero, ra
#function to find the index of a number in array binary:
#a0 = array address,
#a1 = numbert to find,
#a2 = low
#a3 = high
#-----outputs:
#a0 = index of the number
#-----used registers:
#t1 = index to scroll the array
#t2 = the value in a[t1]
#-----used labels:
#elseLow = recall the function recursively if the value to find is lower than the a[i]
#elseGrt = recall the function recursively if the value to find is higher than the a[i]
#elb_err = return if the number isn't in array
binary_search:
addi sp, sp, -8
sd ra, 0(sp)
blt a3, a2, elb_err
add t2, a2, a3
srli t1, t2, 1
add t2, a0, t1
lb t3, 0(t2)
bgt t3, a1, elseLow
blt t3, a1, elseGrt
mv a0, t1
ld ra, 0(sp)
addi sp, sp, 8
jalr zero, ra
elseLow:
addi a3, t1, -1
jal ra, binary_search
ld ra, 0(sp)
addi sp, sp, 8
jalr zero, ra
elseGrt:
addi a2, t1, 1
jal ra, binary_search
ld ra, 0(sp)
addi sp, sp, 8
jalr zero, ra
elb_err:
li a0, -1
ld ra, 0(sp)
addi sp, sp, 8
jalr zero, ra