Skip to the task
Layout
Back to classic view

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

READTask 21 of 25 · step 9: Extra: how does the CPU see it?How does the CPU see it?
How does the CPU see it? in practice/06/

Extra: to the processor, &x is lea (computing an address) and *p is a read in square brackets [ ]. Find them in the assembly of practice/08/pointers.c.

Numbered notes explain the assembly lines.

Details & hint

Extra. Not graded. What do & and * look like when the processor runs them?

Open the tool window's Assembly tab. On the left is your practice/08/pointers.c.

An address and what is at it

  • &x becomes lea (load effective address): the variable's address is computed and its value is not read. swap(&x, &y) passes the function two addresses in rcx and rdx, not 3 and 8.
  • *a is two steps. mov rax, QWORD PTR 16[rbp] takes the pointer itself (an 8-byte address), and mov eax, DWORD PTR [rax] the int it points to. Square brackets [...] in assembly are the *.
  • A pointer is a QWORD (8 bytes) and an int a DWORD (4): the same numbers sizeof showed in step 1.

What to notice (-O0)

  • *a = *b; is four instructions: take b, read *b, take a, write to *a.
  • p++ in sum becomes add QWORD PTR ..., 4: a pointer to int moves not by 1 but by 4 bytes, one element.
  • assert(a != NULL ...) is a comparison with 0: NULL is just the address 0.

Switch to -O1: swap shrinks to a few instructions, and the pointers are kept in registers (rbx, rsi) without going through memory: *a is one instruction, mov eax, DWORD PTR [rbx].

practice 06 main.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include "input.h"
#define DAYS 7
/* Reads n temperatures into the array t.
Before: 1 <= n <= DAYS.
After: t[0]..t[n - 1] are filled. */
void read_temperatures(int t[], int n) {
assert(n >= 1 && n <= DAYS);
for (int i = 0; i < n; i++) {
printf("%d. ", i + 1);
t[i] = read_int("Temperature: ");
}
}
/* Prints n values of the array on one line, separated by a space.
Before: 0 <= n <= DAYS. */
void print_array(const int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (i > 0) {
printf(" ");
}
printf("%d", t[i]);
}
printf("\n");
}
/* The arithmetic mean.
Before: 1 <= n <= DAYS.
After: (t[0] + ... + t[n - 1]) / n. */
double average(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += t[i];
}
return (double)sum / n;
}
/* The index of the warmest day (the first one, if several are equal).
Before: 1 <= n <= DAYS.
After: t[i] >= t[j] for every j. */
int warmest(const int t[], int n) {
assert(n >= 1 && n <= DAYS);
int best = 0;
for (int i = 1; i < n; i++) {
if (t[i] > t[best]) {
best = i;
}
}
return best;
}
/* Linear search: the index of the first such value.
Before: 0 <= n <= DAYS.
After: i such that t[i] == value, or -1 if there is none. */
int find(const int t[], int n, int value) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n; i++) {
if (t[i] == value) {
return i;
}
}
return -1;
}
/* Sorts in ascending order, by selection.
Before: 0 <= n <= DAYS.
After: t[0] <= t[1] <= ... <= t[n - 1]. */
void sort_array(int t[], int n) {
assert(n >= 0 && n <= DAYS);
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++) {
if (t[j] < t[min]) {
min = j;
}
}
int temp = t[i];
t[i] = t[min];
t[min] = temp;
}
}
int main(void) {
int temperatures[DAYS];
read_temperatures(temperatures, DAYS);
printf("Temperatures: ");
print_array(temperatures, DAYS);
printf("Average: %.2f\n", average(temperatures, DAYS));
int w = warmest(temperatures, DAYS);
printf("Warmest: day %d, %d C\n", w + 1, temperatures[w]);
int sorted[DAYS];
for (int i = 0; i < DAYS; i++) {
sorted[i] = temperatures[i];
}
sort_array(sorted, DAYS);
printf("Sorted: ");
print_array(sorted, DAYS);
printf("Median: %d C\n", sorted[DAYS / 2]);
int target = read_int("Which temperature to look for? ");
int pos = find(temperatures, DAYS, target);
if (pos == -1) {
printf("%d C was not on any day.\n", target);
} else {
printf("%d C was first on day %d.\n", target, pos + 1);
}
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