.section .note.GNU-stack,"",%progbits
.text
.global binary_search
binary_search:
push %rbp; // move rbp to stack
mov %rsp, %rbp; // move rsp to rbp
mov %rdi, %rax; // move head
mov %rsi, %rcx; //move len
mov %rcx, %r12;
dec %rcx;
mov %rdx, %r9; //move target
mov %rcx, %r10; //mid=right
mov $0, %r8; //left = %r8, %rcx = right, %r10 = mid
loop:
cmp %rcx, %r8; //if left > right, -1
jg final;
mov %r10, %rcx; //mid=right
sub %r8, %r10; //mid=right-left
sar $1, %r10; //mid/2
add %r8, %r10; //mid+left
mov (%rax, %r10, 4), %rdx;
cmp %r9, %rdx;
jne check;
je ret_mid;
jb inc_left;
dec %r10;
mov %r10, %rcx;
jmp loop;
check:
mov $10, %rax; // set curr->next = prev
pop %rbp;
ret;
inc_left:
inc %r10;
mov %r10, %r8;
jmp loop;
ret_mid:
mov %r10, %rax; // set curr->next = prev
pop %rbp;
ret;
final:
mov $-1,%rax;
pop %rbp;
ret;
I've been testing this binary search algo with the array (0,1,2,3,4). he arguments I gave it were that array, 5, 2. I checked that (%rax, %r10, 4) is also 2, but why is that and %r9 not equal? It never goes to ret_mid. I don't understand why they are not equal even though the value in both is 2.