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?

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.

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].

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

.LC0:
.ascii "practice/08/pointers.c\0"
.LC1:
.ascii "a != NULL && b != NULL\0"
swap:
push rsi
push rbx
sub rsp, 40
mov rbx, rcx
mov rsi, rdx
test rcx, rcx
je .L6
test rdx, rdx
je .L6
.L4:
mov eax, DWORD PTR [rbx]
Note 1: *a is one instruction; a is kept in rbx.
mov edx, DWORD PTR [rsi]
mov DWORD PTR [rbx], edx
mov DWORD PTR [rsi], eax
add rsp, 40
pop rbx
pop rsi
ret
.L6:
mov r8d, 9
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L4
.LC2:
.ascii "n >= 1 && t != NULL && min != NULL && max != NULL\0"
min_max:
push rbp
push rdi
push rsi
push rbx
sub rsp, 40
mov rdi, rcx
mov ebp, edx
mov rbx, r8
mov rsi, r9
test edx, edx
jle .L9
test rcx, rcx
je .L9
test r8, r8
je .L9
test r9, r9
jne .L10
.L9:
mov r8d, 19
lea rdx, .LC0[rip]
lea rcx, .LC2[rip]
call [QWORD PTR __imp__assert[rip]]
.L10:
mov eax, DWORD PTR [rdi]
mov DWORD PTR [rbx], eax
mov DWORD PTR [rsi], eax
cmp ebp, 1
jle .L8
lea rax, 4[rdi]
lea edx, -2[rbp]
lea r8, 8[rdi+rdx*4]
jmp .L15
.L14:
add rax, 4
cmp rax, r8
je .L8
.L15:
mov rcx, rax
mov edx, DWORD PTR [rax]
cmp edx, DWORD PTR [rbx]
jge .L13
mov DWORD PTR [rbx], edx
.L13:
mov edx, DWORD PTR [rcx]
cmp edx, DWORD PTR [rsi]
jle .L14
mov DWORD PTR [rsi], edx
jmp .L14
.L8:
add rsp, 40
pop rbx
pop rsi
pop rdi
pop rbp
ret
.LC3:
.ascii "n >= 0 && t != NULL\0"
sum:
push rsi
push rbx
sub rsp, 40
mov rbx, rcx
mov esi, edx
test edx, edx
js .L24
test rcx, rcx
je .L24
.L19:
movsx rsi, esi
lea rdx, [rbx+rsi*4]
cmp rbx, rdx
jnb .L23
mov eax, 0
.L22:
add eax, DWORD PTR [rbx]
add rbx, 4
cmp rbx, rdx
jb .L22
.L18:
add rsp, 40
pop rbx
pop rsi
ret
.L24:
mov r8d, 35
lea rdx, .LC0[rip]
lea rcx, .LC3[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L19
.L23:
mov eax, 0
jmp .L18
.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 rbx
sub rsp, 80
call __main
mov DWORD PTR 76[rsp], 3
mov DWORD PTR 72[rsp], 8
mov r8d, 8
mov edx, 3
lea rcx, .LC4[rip]
call printf
lea rdx, 72[rsp]
lea rcx, 76[rsp]
call swap
mov r8d, DWORD PTR 72[rsp]
mov edx, DWORD PTR 76[rsp]
lea rcx, .LC5[rip]
call printf
mov DWORD PTR 48[rsp], 4
mov DWORD PTR 52[rsp], -2
mov DWORD PTR 56[rsp], 9
mov DWORD PTR 60[rsp], 0
mov DWORD PTR 64[rsp], 7
lea rbx, 48[rsp]
lea r9, 40[rsp]
lea r8, 44[rsp]
mov edx, 5
mov rcx, rbx
call min_max
mov r8d, DWORD PTR 40[rsp]
mov edx, DWORD PTR 44[rsp]
lea rcx, .LC6[rip]
call printf
mov edx, 5
mov rcx, rbx
call sum
mov edx, eax
lea rcx, .LC7[rip]
call printf
mov eax, 0
add rsp, 80
pop rbx
ret