-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoroushpoor-asha-610397170-binarySearch.asm
142 lines (113 loc) · 2.25 KB
/
Soroushpoor-asha-610397170-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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
%include "in_out.asm"
section .data
Msg db 'NaN', 0
section .bss
s resq 1;start of a
sE resq 1;a End
n resq 1
x resq 1
section .text
global _start
binarySearch:
;we have 2 passed variable L and R
;psuedo code
; function binary_search_leftmost(A, n, T):
; L := 0
; R := n
; while L < R:
; m := floor((L + R) / 2)
; if A[m] < T:
; L := m + 1
; else:
; R := m
; return L
;because we are retarded we tend to do this recursively :)
enter 0,0
push rax;temp register
push rbx;base ragister
push rdx;data
push rcx;index register
; push r8
mov rax, [rbp+16];*a or L
mov rcx, [rbp+24];*aEnd or R
cmp rax,rcx
JNL binarySearchCaseEnd
add rcx, rax
shr rcx,1; floor((L + R) / 2)
mov rbx, [s]
neg rcx
mov rdx, [rbx + rcx*8]
neg rcx
mov rax, qword [x]
cmp rdx, rax
JL binarySearchCase1
JMP binarySearchCase2
binarySearchCase1:
mov rax, [rbp+24];R=R
push rax
inc rcx
push rcx;m+1 = L
; mov rcx, [rbp+24]
; push rcx;R = R
call binarySearch
JMP binarySearchExit
binarySearchCase2:
mov rax, [rbp+16]
push rcx; R=M
push rax; L = L
call binarySearch
JMP binarySearchExit
binarySearchCaseEnd:
mov rbx, [s]
mov rcx, rax
neg rcx
mov rdx, [rbx + rcx*8]
; neg rcx
cmp rdx, [x]
JE binarySearchCaseEndFound
mov rsi, Msg
call printString
call newLine
JMP binarySearchExit
binarySearchCaseEndFound:
call writeNum
call newLine
JMP binarySearchExit
binarySearchExit:
; pop r8
pop rcx
pop rdx
pop rbx
pop rax
leave
ret 16
_start:
mov rax, rsp
sub rax, 8
mov [s], rax; we store start of array
call readNum; read n
mov rcx, rax
mov [n],rax
inputArray:
call readNum
push rax
loop inputArray
add rsp, 8
mov [sE], rsp;store end of array
sub rsp,8
call readNum
mov rcx, rax
queryLoop:
call readNum
mov [x], rax
mov rax, [n]
; dec rax
push rax;push R first
xor rax,rax
push rax;push L next
call binarySearch
loop queryLoop
Exit:
mov rax, 1
mov rbx, 0
int 0x80