I am learning assembly, and I have found the following assembly code for binary research.
bsearch:
xorl %r8d, %r8d
.L4:
movl %esi, %eax
subl %r8d, %eax
testl %eax, %eax
jle .L9
.L6:
sarl %eax
addl $1, %eax
movslq %eax, %rcx
movl (%rdi,%rcx,4), %ecx
cmpl %edx, %ecx
jle .L3
movl %eax, %esi
movl %esi, %eax
subl %r8d, %eax
testl %eax, %eax
jg .L6
.L9:
movl $-1, %eax
.L5:
rep ret
.L3:
cmpl %ecx, %edx
jle .L5
movl %eax, %r8d
jmp .L4
The above code is generated on goldbolt.
In tag .L6
, there are two lines:
movl %eax, %esi
movl %esi, %eax
I am confused about what is going on here. What is the point of moving %eax
to %esi
then move it back