Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Arrays.

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 ────────────

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 -O1

.LC0:
.ascii "practice/06/search.c\0"
.LC1:
.ascii "n >= 0\0"
test edx, edx
js .L13
jle .L7
mov edx, edx
mov eax, 0
.L6:
mov r9d, eax
add DWORD PTR checks[rip], 1
cmp DWORD PTR [rcx+rax*4], r8d
Note 1: All of a[i] == x is one instruction.
je .L10
add rax, 1
cmp rax, rdx
jne .L6
mov r9d, -1
.L10:
mov eax, r9d
ret
.L13:
sub rsp, 40
mov r8d, 13
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
mov r9d, -1
mov eax, r9d
add rsp, 40
ret
.L7:
mov r9d, -1
jmp .L10
test edx, edx
js .L26
mov r10, rcx
mov ecx, edx
sub ecx, 1
js .L20
mov edx, 0
jmp .L19
.L26:
sub rsp, 40
mov r8d, 27
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
mov eax, -1
add rsp, 40
ret
.L17:
lea ecx, -1[rax]
.L18:
cmp edx, ecx
jg .L27
.L19:
mov r9d, ecx
sub r9d, edx
mov eax, r9d
shr eax, 31
add eax, r9d
sar eax
add eax, edx
add DWORD PTR checks[rip], 1
movsx r9, eax
mov r9d, DWORD PTR [r10+r9*4]
cmp r9d, r8d
je .L23
jge .L17
lea edx, 1[rax]
jmp .L18
.L27:
mov eax, -1
ret
.L20:
mov eax, -1
ret
.L23:
ret
.LC2:
.ascii "Target Linear Binary\12\0"
.LC3:
.ascii "pos2 == pos\0"
.LC4:
.ascii "%6d %6d %6d\12\0"
main:
push r15
push r14
push r13
push r12
push rbp
push rdi
push rsi
push rbx
mov eax, 4056
call ___chkstk_ms
sub rsp, rax
call __main
lea rax, 48[rsp]
lea rcx, 4048[rsp]
mov edx, 0
.L29:
mov DWORD PTR [rax], edx
add edx, 2
add rax, 4
cmp rax, rcx
jne .L29
mov DWORD PTR 32[rsp], 0
mov DWORD PTR 36[rsp], 998
mov DWORD PTR 40[rsp], 1998
mov DWORD PTR 44[rsp], 7
lea rcx, .LC2[rip]
call printf
lea rbx, 32[rsp]
lea r13, 48[rsp]
mov rbp, r13
lea r15, .LC0[rip]
mov r14, QWORD PTR __imp__assert[rip]
jmp .L31
.L30:
mov r9d, DWORD PTR checks[rip]
mov r8d, r12d
mov edx, esi
lea rcx, .LC4[rip]
call printf
add rbx, 4
cmp rbx, r13
je .L35
.L31:
mov DWORD PTR checks[rip], 0
mov esi, DWORD PTR [rbx]
mov r8d, esi
mov edx, 1000
mov rcx, rbp
call linear_search
mov edi, eax
mov r12d, DWORD PTR checks[rip]
mov DWORD PTR checks[rip], 0
mov r8d, esi
mov edx, 1000
mov rcx, rbp
call binary_search
cmp edi, eax
je .L30
mov r8d, 62
mov rdx, r15
lea rcx, .LC3[rip]
call r14
jmp .L30
.L35:
mov eax, 0
add rsp, 4056
pop rbx
pop rsi
pop rdi
pop rbp
pop r12
pop r13
pop r14
pop r15
ret