Skip to the task
Layout
Back to classic view

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

CHECKTask 17 of 20 · step 7: Extra: how does the CPU see it?Why 8 and 12?
Why 8 and 12? in practice/09/

array_init writes to [rax], 8[rax] and 12[rax]. Which field is where, and why does count start at 8, not at 4?

Numbered notes explain the assembly lines.

Details & hint

Stuck? data at 0, count at 8, capacity at 12. The first field, data, is a pointer, and a pointer takes 8 bytes, so count starts after it.

practice 09 numbers.c Code Diff
UNCHANGED This step does not change this file.
#include <stdio.h>
#include <string.h>
#include "safe_input.h"
int main(void) {
char t[20];
int sum = 0;
printf("Whole numbers one per line, q to stop.\n");
for (;;) {
printf("> ");
fflush(stdout);
int r = get_line(t, (int)sizeof t);
if (r == -1 || strcmp(t, "q") == 0) {
break;
}
if (r == 0) {
printf("Line too long: at most %zu characters.\n", sizeof t - 2);
continue;
}
int x;
if (to_int(t, &x)) {
sum += x;
printf("OK: %d, sum %d\n", x, sum);
} else {
printf("\"%s\" is not a whole number.\n", t);
}
}
printf("Sum: %d\n", sum);
return 0;
}
opened for this task

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

-O0 -O1

C source memory.c

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* A resizable array of integers. */
typedef struct {
int *data; /* the elements in dynamic memory, or NULL */
int count; /* how many elements are stored */
int capacity; /* how many fit before it has to grow */
} IntArray;
/* An empty array: no memory yet. Before: m != NULL. */
void array_init(IntArray *m) {
assert(m != NULL);
m->data = NULL;
m->count = 0;
m->capacity = 0;
}
/* Adds x at the end; when there is no room, doubles the capacity.
Before: m != NULL. After: 1 - added, 0 - out of memory (m unchanged). */
int array_add(IntArray *m, int x) {
assert(m != NULL);
if (m->count == m->capacity) {
int new_capacity = m->capacity == 0 ? 4 : m->capacity * 2;
int *new_data = realloc(m->data, (size_t)new_capacity * sizeof *new_data);
if (new_data == NULL) {
return 0;
}
printf("Capacity: %d -> %d\n", m->capacity, new_capacity);
m->data = new_data;
m->capacity = new_capacity;
}
m->data[m->count] = x;
m->count++;
return 1;
}
/* Gives the memory back and leaves an empty array. Before: m != NULL. */
void array_free(IntArray *m) {
assert(m != NULL);
free(m->data);
array_init(m);
}
/* Returns a new copy of s in dynamic memory, or NULL if there isn't enough.
Before: s != NULL. After: the caller frees the copy with free. */
char *copy_string(const char *s) {
size_t size = strlen(s) + 1;
char *copy = malloc(size);
if (copy == NULL) {
return NULL;
}
memcpy(copy, s, size);
return copy;
}
int main(void) {
IntArray squares;
array_init(&squares);
for (int i = 1; i * i < 1000; i++) {
if (!array_add(&squares, i * i)) {
printf("Out of memory.\n");
array_free(&squares);
return EXIT_FAILURE;
}
}
printf("Squares below 1000: %d, capacity %d\n", squares.count, squares.capacity);
printf("Last: %d\n", squares.data[squares.count - 1]);
array_free(&squares);
char *name = copy_string("Ona Petraite");
if (name == NULL) {
printf("Out of memory.\n");
return EXIT_FAILURE;
}
printf("Copy: %s (%zu bytes)\n", name, strlen(name) + 1);
free(name);
return EXIT_SUCCESS;
}

x86-64 assembly -O0

.LC0:
.ascii "practice/12/memory.c\0"
.LC1:
.ascii "m != NULL\0"
array_init:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR 16[rbp], rcx
cmp QWORD PTR 16[rbp], 0
jne .L6
mov r8d, 15
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L6:
mov rax, QWORD PTR 16[rbp]
mov QWORD PTR [rax], 0
Note 1: m->data: offset 0.
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 8[rax], 0
Note 2: m->count: offset 8, after the 8-byte pointer.
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 12[rax], 0
Note 3: m->capacity: offset 12.
nop
leave
ret
.LC2:
.ascii "Capacity: %d -> %d\12\0"
array_add:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov DWORD PTR 24[rbp], edx
cmp QWORD PTR 16[rbp], 0
jne .L10
mov r8d, 24
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L10:
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR 8[rax]
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
cmp edx, eax
jne .L11
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
test eax, eax
je .L12
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
add eax, eax
jmp .L13
.L12:
mov eax, 4
.L13:
mov DWORD PTR -4[rbp], eax
mov eax, DWORD PTR -4[rbp]
cdqe
lea rdx, 0[0+rax*4]
mov rax, QWORD PTR 16[rbp]
mov rax, QWORD PTR [rax]
mov rcx, rax
call realloc
Note 4: rcx: the old address, rdx: bytes; the new address comes back in rax.
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
Note 5: new_data == NULL is a comparison with 0.
jne .L14
mov eax, 0
jmp .L15
.L14:
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 12[rax]
mov edx, DWORD PTR -4[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC2[rip]
call printf
mov rax, QWORD PTR 16[rbp]
mov rdx, QWORD PTR -16[rbp]
mov QWORD PTR [rax], rdx
mov rax, QWORD PTR 16[rbp]
mov edx, DWORD PTR -4[rbp]
mov DWORD PTR 12[rax], edx
.L11:
mov rax, QWORD PTR 16[rbp]
mov rdx, QWORD PTR [rax]
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 8[rax]
cdqe
sal rax, 2
add rdx, rax
mov eax, DWORD PTR 24[rbp]
mov DWORD PTR [rdx], eax
mov rax, QWORD PTR 16[rbp]
mov eax, DWORD PTR 8[rax]
lea edx, 1[rax]
mov rax, QWORD PTR 16[rbp]
mov DWORD PTR 8[rax], edx
mov eax, 1
.L15:
leave
ret
array_free:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR 16[rbp], rcx
cmp QWORD PTR 16[rbp], 0
jne .L19
mov r8d, 42
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
.L19:
mov rax, QWORD PTR 16[rbp]
mov rax, QWORD PTR [rax]
mov rcx, rax
call free
mov rcx, QWORD PTR 16[rbp]
call array_init
nop
leave
ret
copy_string:
push rbp
mov rbp, rsp
sub rsp, 48
mov QWORD PTR 16[rbp], rcx
mov rcx, QWORD PTR 16[rbp]
call strlen
add rax, 1
mov QWORD PTR -8[rbp], rax
mov rax, QWORD PTR -8[rbp]
mov rcx, rax
call malloc
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
jne .L21
mov eax, 0
jmp .L22
.L21:
mov rdx, QWORD PTR -8[rbp]
mov rax, QWORD PTR -16[rbp]
mov r8, rdx
mov rdx, QWORD PTR 16[rbp]
mov rcx, rax
call memcpy
mov rax, QWORD PTR -16[rbp]
.L22:
leave
ret
.LC3:
.ascii "Out of memory.\12\0"
.LC4:
.ascii "Squares below 1000: %d, capacity %d\12\0"
.LC5:
.ascii "Last: %d\12\0"
.LC6:
.ascii "Ona Petraite\0"
.LC7:
.ascii "Copy: %s (%zu bytes)\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 64
call __main
lea rax, -32[rbp]
mov rcx, rax
call array_init
mov DWORD PTR -4[rbp], 1
jmp .L24
.L27:
mov eax, DWORD PTR -4[rbp]
imul eax, eax
mov edx, eax
lea rax, -32[rbp]
mov rcx, rax
call array_add
test eax, eax
jne .L25
lea rcx, .LC3[rip]
call printf
lea rax, -32[rbp]
mov rcx, rax
call array_free
mov eax, 1
jmp .L29
.L25:
add DWORD PTR -4[rbp], 1
.L24:
mov eax, DWORD PTR -4[rbp]
imul eax, eax
cmp eax, 999
jle .L27
mov edx, DWORD PTR -20[rbp]
mov eax, DWORD PTR -24[rbp]
mov r8d, edx
mov edx, eax
lea rcx, .LC4[rip]
call printf
mov rdx, QWORD PTR -32[rbp]
mov eax, DWORD PTR -24[rbp]
cdqe
sal rax, 2
sub rax, 4
add rax, rdx
mov eax, DWORD PTR [rax]
mov edx, eax
lea rcx, .LC5[rip]
call printf
lea rax, -32[rbp]
mov rcx, rax
call array_free
lea rcx, .LC6[rip]
call copy_string
mov QWORD PTR -16[rbp], rax
cmp QWORD PTR -16[rbp], 0
jne .L28
lea rcx, .LC3[rip]
call printf
mov eax, 1
jmp .L29
.L28:
mov rax, QWORD PTR -16[rbp]
mov rcx, rax
call strlen
lea rdx, 1[rax]
mov rax, QWORD PTR -16[rbp]
mov r8, rdx
mov rdx, rax
lea rcx, .LC7[rip]
call printf
mov rax, QWORD PTR -16[rbp]
mov rcx, rax
call free
mov eax, 0
.L29:
leave
ret