Given a binarySearch test that passes 4 arguments to my .s file:
- a pointer to an int array
- an int representing the beginning of the array
- an int representing the end of the array
- the target element
I'm trying to code a binarySearch method in x86-64, but am running into a segmentation fault, for what I assume has to do with trying to access memory that is random or something similar, but I have no idea what specifically is causing it. Are there any online compilers that allow me to compile a .cpp file with a .s file? That would help me to debug this.
But anyway, a look at the code is:
global binarySearch
section .text
binarySearch:
push rsi
push rbx ; beginning of the array
push rdx ; end of the array
push rcx ; target
push rdi
; mov rdx, rbx
dec rdx
; mov rbx, 0
startBinarySearch:
xor rax, rax
cmp rbx, rdx
jg notFound
mov rax, rbx
add rax, rdx
shr rax, 1
mov rdi, rsi
add rdi, rax
add rdi, rax
cmp rcx, [rdi]
je found
jg largePivot
jmp smallPivot
largePivot:
inc rax
mov rbx, rax
jmp startBinarySearch
smallPivot:
dec rax
mov rdx, rax
jmp startBinarySearch
notFound:
mov rax, -1
jmp end
found:
jmp end
end:
pop rdi
pop rcx
pop rdx
pop rbx
pop rsi
ret
which is paraphrased from a .asm version I have written, but the translation apparently didn't rollover correctly.
Are there any glaringly obvious reasons as to what's causing this segmentation fault? I've seen in a couple places that it could have to do with xor'ing the variables after I push them, but that previously hasn't worked.