Skip to the task
Layout
Back to classic view

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

CHECKTask 22 of 25 · step 9: Extra: how does the CPU see it?What does swap get?
What does swap get? in practice/06/

main calls swap(&x, &y), and x = 3. What goes into rcx: 3 or something else? Which instruction computes it?

Numbered notes explain the assembly lines.

Details & hint

Stuck? The address of x: lea (load effective address) computes where x lives on the stack without reading its value. That is why swap can write straight into main's variable.

practice 06 search.c Code Diff
UNCHANGED This step does not change this file.
#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;
}
opened for this task

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

-O0 -O1

C source pointers.c

#include <assert.h>
#include <stdio.h>
#define N 5
/* Swaps the values of two variables.
Before: a != NULL, b != NULL. */
void swap(int *a, int *b) {
assert(a != NULL && b != NULL);
int tmp = *a;
*a = *b;
*b = tmp;
}
/* Finds the smallest and the largest value and returns them through pointers.
Before: n >= 1, t, min and max not NULL.
After: *min is the smallest, *max the largest value of t. */
void min_max(const int t[], int n, int *min, int *max) {
assert(n >= 1 && t != NULL && min != NULL && max != NULL);
*min = t[0];
*max = t[0];
for (int i = 1; i < n; i++) {
if (t[i] < *min) {
*min = t[i];
}
if (t[i] > *max) {
*max = t[i];
}
}
}
/* The sum, walking the array with a pointer.
Before: n >= 0, t != NULL. */
int sum(const int *t, int n) {
assert(n >= 0 && t != NULL);
int s = 0;
for (const int *p = t; p < t + n; p++) {
s += *p;
}
return s;
}
int main(void) {
int x = 3;
int y = 8;
printf("Before: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After: x = %d, y = %d\n", x, y);
int t[N] = {4, -2, 9, 0, 7};
int min;
int max;
min_max(t, N, &min, &max);
printf("Smallest: %d, largest: %d\n", min, max);
printf("Sum: %d\n", sum(t, N));
return 0;
}

x86-64 assembly -O0

.LC0:
.ascii "practice/08/pointers.c\0"
.LC1:
.ascii "a != NULL && b != NULL\0"
swap:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
Note 1: A pointer is a QWORD, 8 bytes: an address.
mov QWORD PTR 24[rbp], rdx
cmp QWORD PTR 16[rbp], 0
Note 2: a != NULL is a comparison with 0.
je .L4
cmp QWORD PTR 24[rbp], 0
jne .L8
.L4:
mov r8d, 9
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
jmp .L7
.L8:
nop
.L7:
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR [rax]
Note 3: *a: the int is read through the address in rax.
mov DWORD PTR -4[rbp], eax
mov rax, QWORD PTR 24[rbp]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR [rax], edx
mov rax, QWORD PTR 24[rbp]
mov edx, DWORD PTR -4[rbp]
mov DWORD PTR [rax], edx
nop
leave
ret
.LC2:
.ascii "n >= 1 && t != NULL && min != NULL && max != NULL\0"
min_max:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov DWORD PTR 24[rbp], edx
mov QWORD PTR 32[rbp], r8
mov QWORD PTR 40[rbp], r9
cmp DWORD PTR 24[rbp], 0
jle .L10
cmp QWORD PTR 16[rbp], 0
je .L10
cmp QWORD PTR 32[rbp], 0
je .L10
cmp QWORD PTR 40[rbp], 0
jne .L18
.L10:
mov r8d, 19
lea rdx, .LC0[rip]
lea rcx, .LC2[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
jmp .L13
.L18:
nop
.L13:
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 32[rbp]
mov DWORD PTR [rax], edx
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 40[rbp]
mov DWORD PTR [rax], edx
mov DWORD PTR -4[rbp], 1
jmp .L14
.L17:
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 32[rbp]
mov eax, DWORD PTR [rax]
cmp edx, eax
jge .L15
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 32[rbp]
mov DWORD PTR [rax], edx
.L15:
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 40[rbp]
mov eax, DWORD PTR [rax]
cmp edx, eax
jle .L16
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR 40[rbp]
mov DWORD PTR [rax], edx
.L16:
add DWORD PTR -4[rbp], 1
.L14:
mov eax, DWORD PTR -4[rbp]
cmp eax, DWORD PTR 24[rbp]
jl .L17
nop
nop
leave
ret
.LC3:
.ascii "n >= 0 && t != NULL\0"
sum:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov DWORD PTR 24[rbp], edx
cmp DWORD PTR 24[rbp], 0
js .L20
cmp QWORD PTR 16[rbp], 0
jne .L27
.L20:
mov r8d, 35
lea rdx, .LC0[rip]
lea rcx, .LC3[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
jmp .L23
.L27:
nop
.L23:
mov DWORD PTR -4[rbp], 0
mov rax, QWORD PTR 16[rbp]
mov QWORD PTR -16[rbp], rax
jmp .L24
.L25:
mov rax, QWORD PTR -16[rbp]
mov eax, DWORD PTR [rax]
add DWORD PTR -4[rbp], eax
add QWORD PTR -16[rbp], 4
Note 4: p++ adds 4: one int further on.
.L24:
mov eax, DWORD PTR 24[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
add rax, rdx
cmp QWORD PTR -16[rbp], rax
jb .L25
mov eax, DWORD PTR -4[rbp]
leave
ret
.LC4:
.ascii "Before: x = %d, y = %d\12\0"
.LC5:
.ascii "After: x = %d, y = %d\12\0"
.LC6:
.ascii "Smallest: %d, largest: %d\12\0"
.LC7:
.ascii "Sum: %d\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 80
call __main
mov DWORD PTR -4[rbp], 3
mov DWORD PTR -8[rbp], 8
mov edx, DWORD PTR -8[rbp]
mov eax, DWORD PTR -4[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC4[rip]
call printf
lea rdx, -8[rbp]
Note 5: &y: lea computes the address and does not read the value.
lea rax, -4[rbp]
mov rcx, rax
call swap
mov edx, DWORD PTR -8[rbp]
mov eax, DWORD PTR -4[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC5[rip]
call printf
mov DWORD PTR -32[rbp], 4
mov DWORD PTR -28[rbp], -2
mov DWORD PTR -24[rbp], 9
mov DWORD PTR -20[rbp], 0
mov DWORD PTR -16[rbp], 7
lea rcx, -40[rbp]
lea rdx, -36[rbp]
lea rax, -32[rbp]
mov r9, rcx
mov r8, rdx
mov edx, 5
mov rcx, rax
call min_max
mov edx, DWORD PTR -40[rbp]
mov eax, DWORD PTR -36[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC6[rip]
call printf
lea rax, -32[rbp]
mov edx, 5
mov rcx, rax
call sum
mov edx, eax
lea rcx, .LC7[rip]
call printf
mov eax, 0
leave
ret