Skip to the task
Layout
Back to classic view

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

CHECKTask 23 of 25 · step 9: Extra: how does the CPU see it?How far does p move?
How far does p move? in practice/05/

The function sum has p++. How many bytes does its instruction add to the address?

Numbered notes explain the assembly lines.

Details & hint

Stuck? 4: add QWORD PTR ..., 4. p points to an int, so p++ moves to the next element, 4 bytes on, not 1. # ── topic 12 (lesson 50, step 8): practice/12/memory.c ──────────────

practice 05 main.c Code Diff
UNCHANGED This step does not change this file.
#include <assert.h>
#include <stdio.h>
#include "input.h"
/* Whether the grade is valid.
After: returns 1 if grade is from 1 to 10, otherwise 0. */
int is_valid(int grade) {
return grade >= 1 && grade <= 10;
}
/* Asks for a grade until a valid one is entered.
After: the returned grade is from 1 to 10. */
int read_grade(void) {
int grade = read_int("Grade: ");
while (!is_valid(grade)) {
printf("Grade must be between 1 and 10.\n");
grade = read_int("Grade: ");
}
return grade;
}
/* Prints the grade in words.
Before: grade is from 1 to 10. */
void print_in_words(int grade) {
assert(is_valid(grade));
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
case 8:
printf("Good\n");
break;
case 7:
printf("Fair\n");
break;
case 6:
printf("Satisfactory\n");
break;
case 5:
printf("Weak\n");
break;
default:
printf("Unsatisfactory\n");
break;
}
}
/* Rounds to a whole number.
Before: x >= 0.
After: the nearest whole number; x.5 is rounded up. */
int round_to_int(double x) {
assert(x >= 0);
return (int)(x + 0.5);
}
void run_tests(void) {
assert(is_valid(1));
assert(is_valid(10));
assert(!is_valid(0));
assert(!is_valid(11));
assert(round_to_int(0) == 0);
assert(round_to_int(7.49) == 7);
assert(round_to_int(7.5) == 8);
}
int main(void) {
run_tests();
int count = read_int("How many grades? ");
while (count < 1) {
printf("At least one grade is needed.\n");
count = read_int("How many grades? ");
}
int sum = 0;
for (int i = 1; i <= count; i++) {
printf("%d. ", i);
int grade = read_grade();
print_in_words(grade);
sum += grade;
}
double average = (double)sum / count;
printf("Average: %.2f\n", average);
printf("Rounded: %d\n", round_to_int(average));
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