-
Notifications
You must be signed in to change notification settings - Fork 0
/
binarySearch.asm
58 lines (47 loc) · 870 Bytes
/
binarySearch.asm
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
main:
#set memory word 0($gp) = [55, 44, 20, 99, 3]
move $a0, $gp
#set $a1 = 0
#set $a2 = 4
#set $a3 = 99
jal binarysearch
#show $v0 signed decimal
#stop
binarysearch:
addi $sp, $sp, -12
sw $ra, 0($sp)
sw $s0, 4($sp); l
sw $s1, 8($sp); r
slt $t0, $a2, $a1
bne $t0, $zero, return_minus
sub $t0, $a2, $a1
srl $t0, $t0, 1
add $t0, $t0, $a1; mid
sll $t1, $t0, 2
add $t1, $a0, $t1 ; &arr[mid]
lw $t2, 0($t1); arr[mid]
bne $t2, $a3, second_if
addi $v0, $t0, 0
j epilogo
second_if:
slt $t3, $a3, $t2
beq $t3, $zero, return_recursive
move $s0, $a1
move $s1, $a2
addi $a2, $t0, -1
jal binarysearch
j epilogo
return_recursive:
move $s0, $a1
move $s1, $a2
addi $a1, $t0, -1
jal binarysearch
j epilogo
return_minus:
addi $v0, $zero, -1
epilogo:
lw $ra, 0($sp)
lw $s0, 4($sp); l
lw $s1, 8($sp); r
addi $sp, $sp, 12
jr $ra