Skip to the task
Layout
Back to classic view

You are looking at lesson 8. Your place is still lesson 7, task 1: Modules.

CHECKTask 22 of 25 · step 9: Extra: how does the CPU see it?

What does swap get?

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

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.

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