-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.s
51 lines (45 loc) · 1.08 KB
/
binary.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
.include "functions.s"
#-----------constant-----------#
.global main
.equ _SYS_WR, 64
.equ _SYS_EX, 93
.section .rodata
msgStart: .string "Number to find: %d, the array length is %d\n"
msgend: .string "The index of the number is: a[%d]\n"
msgerr: .string "The number isn't in the array\n"
#-----------global symbol-----------#
.section .data
array: .byte -25, -23, -21, -11, 0, 31, 39, 40, 47, 50, 51, 52, 63, 100, 127
num: .byte -25
len: .byte 15
#-----------code-----------#
.section .text
main:
#starting message
la a0, msgStart
la a1, num
lb a1, 0(a1)
la a2, len
lb a2, 0(a2)
jal ra, printf
#call binary_search
la a0, array #array address
la a1, num
lb a1, 0(a1) #number to find
li a2, 0 #low
la a3, len
lb a3, 0(a3)
addi a3, a3, -1 #high: array length - 1
jal ra, binary_search
#return binary_search
blt a0, zero, else
mv a1, a0
la a0, msgend
jal ra, printf
beq zero, zero, _end
else:
la a0, msgerr
jal ra, printf
_end:
li a7, _SYS_EX
ecall