Skip to the task
Layout
Back to classic view

You are looking at lesson 6. Your place is still lesson 3, task 1: Conditions.

CHECKTask 26 of 28 · step 10: Extra: how does the CPU see it?

Who checks the bounds?

Is there a check among a[i]'s instructions that i < n?

Stuck?

No. Only the for condition checks i < n (cmp and jl); a[i] itself just computes the address and reads. With a wrong i it would read someone else's memory, which is why the sanitizers are needed. # ── topic 8 (lesson 46, step 10): practice/08/pointers.c ────────────

opened for this task

Extra · x86-64 · Intel syntax · Windows (MinGW GCC)

-O0 -O1

C source search.c

#include <assert.h>
#include <stdio.h>
#define N 1000
/* How many elements the last search checked. For measuring only. */
int checks = 0;
/* Linear search.
Before: 0 <= n.
After: the index of x, or -1 if x is not there. */
int linear_search(const int a[], int n, int x) {
assert(n >= 0);
for (int i = 0; i < n; i++) {
checks++;
if (a[i] == x) {
return i;
}
}
return -1;
}
/* Binary search.
Before: 0 <= n, a is sorted in ascending order.
After: the index of x, or -1 if x is not there. */
int binary_search(const int a[], int n, int x) {
assert(n >= 0);
int left = 0;
int right = n - 1;
while (left <= right) {
int middle = left + (right - left) / 2;
checks++;
if (a[middle] == x) {
return middle;
}
if (a[middle] < x) {
left = middle + 1;
} else {
right = middle - 1;
}
}
return -1;
}
int main(void) {
int numbers[N];
for (int i = 0; i < N; i++) {
numbers[i] = 2 * i;
}
int targets[] = {0, 998, 1998, 7};
int count = (int)(sizeof targets / sizeof targets[0]);
printf("Target Linear Binary\n");
for (int k = 0; k < count; k++) {
checks = 0;
int pos = linear_search(numbers, N, targets[k]);
int linear_checks = checks;
checks = 0;
int pos2 = binary_search(numbers, N, targets[k]);
assert(pos2 == pos);
printf("%6d %6d %6d\n", targets[k], linear_checks, checks);
}
return 0;
}

x86-64 assembly -O0

.LC0:
.ascii "practice/06/search.c\0"
.LC1:
.ascii "n >= 0\0"
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
Note 1: a is an address (QWORD, 8 bytes), not the array itself.
mov DWORD PTR 24[rbp], edx
mov DWORD PTR 32[rbp], r8d
cmp DWORD PTR 24[rbp], 0
jns .L6
mov r8d, 13
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L6:
mov DWORD PTR -4[rbp], 0
jmp .L7
.L10:
mov eax, DWORD PTR checks[rip]
Note 2: A global is reached by its name, not through rbp.
add eax, 1
mov DWORD PTR checks[rip], eax
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
Note 3: a[i]: i · 4, because an int takes 4 bytes.
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov eax, DWORD PTR [rax]
Note 4: Read with no bounds check: nothing here checks i < n.
cmp DWORD PTR 32[rbp], eax
jne .L8
mov eax, DWORD PTR -4[rbp]
jmp .L9
.L8:
add DWORD PTR -4[rbp], 1
.L7:
mov eax, DWORD PTR -4[rbp]
cmp eax, DWORD PTR 24[rbp]
jl .L10
mov eax, -1
.L9:
leave
ret
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov DWORD PTR 24[rbp], edx
mov DWORD PTR 32[rbp], r8d
cmp DWORD PTR 24[rbp], 0
jns .L14
mov r8d, 27
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L14:
mov DWORD PTR -4[rbp], 0
mov eax, DWORD PTR 24[rbp]
sub eax, 1
mov DWORD PTR -8[rbp], eax
jmp .L15
.L19:
mov eax, DWORD PTR -8[rbp]
sub eax, DWORD PTR -4[rbp]
mov edx, eax
shr edx, 31
add eax, edx
sar eax
Note 5: / 2 is a shift by one bit (shr and add handle negatives).
mov edx, eax
mov eax, DWORD PTR -4[rbp]
add eax, edx
mov DWORD PTR -12[rbp], eax
mov eax, DWORD PTR checks[rip]
add eax, 1
mov DWORD PTR checks[rip], eax
mov eax, DWORD PTR -12[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov eax, DWORD PTR [rax]
cmp DWORD PTR 32[rbp], eax
jne .L16
mov eax, DWORD PTR -12[rbp]
jmp .L17
.L16:
mov eax, DWORD PTR -12[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov eax, DWORD PTR [rax]
cmp DWORD PTR 32[rbp], eax
jle .L18
mov eax, DWORD PTR -12[rbp]
add eax, 1
mov DWORD PTR -4[rbp], eax
jmp .L15
.L18:
mov eax, DWORD PTR -12[rbp]
sub eax, 1
mov DWORD PTR -8[rbp], eax
.L15:
mov eax, DWORD PTR -4[rbp]
cmp eax, DWORD PTR -8[rbp]
jle .L19
mov eax, -1
.L17:
leave
ret
.LC2:
.ascii "Target Linear Binary\12\0"
.LC3:
.ascii "pos2 == pos\0"
.LC4:
.ascii "%6d %6d %6d\12\0"
main:
push rbp
mov rbp, rsp
mov eax, 4080
call ___chkstk_ms
sub rsp, rax
call __main
mov DWORD PTR -4[rbp], 0
jmp .L21
.L22:
mov eax, DWORD PTR -4[rbp]
lea edx, [rax+rax]
mov eax, DWORD PTR -4[rbp]
cdqe
mov DWORD PTR -4032[rbp+rax*4], edx
add DWORD PTR -4[rbp], 1
.L21:
cmp DWORD PTR -4[rbp], 999
jle .L22
mov DWORD PTR -4048[rbp], 0
mov DWORD PTR -4044[rbp], 998
mov DWORD PTR -4040[rbp], 1998
mov DWORD PTR -4036[rbp], 7
mov DWORD PTR -12[rbp], 4
lea rcx, .LC2[rip]
call printf
mov DWORD PTR -8[rbp], 0
jmp .L23
.L27:
mov DWORD PTR checks[rip], 0
mov eax, DWORD PTR -8[rbp]
cdqe
mov edx, DWORD PTR -4048[rbp+rax*4]
lea rax, -4032[rbp]
mov r8d, edx
mov edx, 1000
mov rcx, rax
call linear_search
mov DWORD PTR -16[rbp], eax
mov eax, DWORD PTR checks[rip]
mov DWORD PTR -20[rbp], eax
mov DWORD PTR checks[rip], 0
mov eax, DWORD PTR -8[rbp]
cdqe
mov edx, DWORD PTR -4048[rbp+rax*4]
lea rax, -4032[rbp]
mov r8d, edx
mov edx, 1000
mov rcx, rax
call binary_search
mov DWORD PTR -24[rbp], eax
mov eax, DWORD PTR -24[rbp]
cmp eax, DWORD PTR -16[rbp]
je .L26
mov r8d, 62
lea rdx, .LC0[rip]
lea rcx, .LC3[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L26:
mov ecx, DWORD PTR checks[rip]
mov eax, DWORD PTR -8[rbp]
cdqe
mov eax, DWORD PTR -4048[rbp+rax*4]
mov edx, DWORD PTR -20[rbp]
mov r9d, ecx
mov r8d, edx
mov edx, eax
lea rcx, .LC4[rip]
call printf
add DWORD PTR -8[rbp], 1
.L23:
mov eax, DWORD PTR -8[rbp]
cmp eax, DWORD PTR -12[rbp]
jl .L27
mov eax, 0
leave
ret