Skip to the task
Layout
Back to classic view

You are looking at lesson 2. Your place is still lesson 8, task 1: Memory.

READTask 19 of 23 · step 8: Extra: how does the CPU see it?

How does the CPU see it?

Extra: practice/02/main.c in assembly. Hover over a C line and see where the variables live in memory and what the compiler worked out by itself.

Extra. This step is not graded and the homework doesn't need it. It is a look at what the compiler makes of your program.

The processor doesn't understand C. GCC turns your main.c into machine code: simple commands such as "move a number", "add", "compare", "call a function". The text form of those commands is called assembly.

How to look

Open the tool window's Assembly tab. On the left is practice/02/main.c, on the right what GCC made of it: for an x86-64 processor, Windows, as MinGW does in CLion. Hover over a C line and its instructions light up, and the other way round. Click to keep the selection (Esc clears it).

  • -O0: no optimization. This is how CLion's Debug profile builds.
  • -O1: a little optimization.

Reading an instruction: first the command, then where it writes, then where from it takes. mov eax, 5 means "put 5 into eax". eax, edx, rcx… are registers: the processor's dozen or so "variables" it computes with.

What to notice (-O0)

  • Every variable lives in memory, next to rbp: mov DWORD PTR -8[rbp], 1 is int study_year = 1;. DWORD is 4 bytes, the size of an int.
  • char group = 'A'; becomes BYTE PTR and the number 65: a character is just a number.
  • sizeof computes nothing while the program runs: the 4, 8 and 1 sit ready next to printf.
  • There is no multiplication for 100LL * 365 * 24 * 60 * 60: the compiler did it itself and wrote the result.
  • Arguments travel to a function in registers: the first in rcx, the second in rdx, the third in r8, the fourth in r9 (the Windows rule). The result comes back in eax, which is how read_int's answer gets into the variable.

Switch to -O1: most variables move from memory into registers, and study_year disappears altogether: printf simply gets 1.

CLion

The debugger shows machine code too: stop at a breakpoint, right-click a line in the Debug window's Frames list and choose Disassemble. For Intel syntax, as here, type set disassembly-flavor intel in the Debug window's GDB tab first: topic 5 shows it step by step. CLion's GCC (11.2) is a different version from the one that generated this view, so some instructions may differ slightly.

opened for this task

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

-O0 -O1

C source main.c

#include <limits.h>
#include <stdio.h>
#include "input.h"
int main(void) {
const int current_year = 2026;
int study_year = 1;
char group = 'A';
int birth_year = read_int("Year of birth: ");
int g1 = read_int("Grade 1: ");
int g2 = read_int("Grade 2: ");
int g3 = read_int("Grade 3: ");
double average = (g1 + g2 + g3) / 3.0;
int age = current_year - birth_year;
printf("Year of study: %d\n", study_year);
printf("Group: PI25%c\n", group);
printf("Age: %d years\n", age);
printf("Average: %.2f\n", average);
printf("In memory: int %zu B, double %zu B, char %zu B\n", sizeof study_year, sizeof average,
sizeof group);
printf("\n");
printf("Largest int: %d\n", INT_MAX);
long long century = 100LL * 365 * 24 * 60 * 60;
printf("100 years: %lld s\n", century);
printf("0.1 + 0.2 = %.17f\n", 0.1 + 0.2);
return 0;
}

x86-64 assembly -O0

.LC0:
.ascii "Year of birth: \0"
.LC1:
.ascii "Grade 1: \0"
.LC2:
.ascii "Grade 2: \0"
.LC3:
.ascii "Grade 3: \0"
.LC5:
.ascii "Year of study: %d\12\0"
.LC6:
.ascii "Group: PI25%c\12\0"
.LC7:
.ascii "Age: %d years\12\0"
.LC8:
.ascii "Average: %.2f\12\0"
.LC9:
.ascii "In memory: int %zu B, double %zu B, char %zu B\12\0"
.LC10:
.ascii "\12\0"
.LC11:
.ascii "Largest int: %d\12\0"
.LC12:
.ascii "100 years: %lld s\12\0"
.LC14:
.ascii "0.1 + 0.2 = %.17f\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 96
call __main
mov DWORD PTR -4[rbp], 2026
mov DWORD PTR -8[rbp], 1
mov BYTE PTR -9[rbp], 65
Note 1: A char is 1 byte (BYTE), and 'A' is just the number 65.
lea rcx, .LC0[rip]
call read_int
mov DWORD PTR -16[rbp], eax
Note 2: read_int's result comes back in eax and is stored in the variable.
lea rcx, .LC1[rip]
call read_int
mov DWORD PTR -20[rbp], eax
lea rcx, .LC2[rip]
call read_int
mov DWORD PTR -24[rbp], eax
lea rcx, .LC3[rip]
call read_int
mov DWORD PTR -28[rbp], eax
mov edx, DWORD PTR -20[rbp]
mov eax, DWORD PTR -24[rbp]
add edx, eax
mov eax, DWORD PTR -28[rbp]
add eax, edx
pxor xmm0, xmm0
cvtsi2sd xmm0, eax
movsd xmm1, QWORD PTR .LC4[rip]
divsd xmm0, xmm1
movsd QWORD PTR -40[rbp], xmm0
mov eax, DWORD PTR -4[rbp]
sub eax, DWORD PTR -16[rbp]
mov DWORD PTR -44[rbp], eax
mov eax, DWORD PTR -8[rbp]
mov edx, eax
lea rcx, .LC5[rip]
call printf
movsx eax, BYTE PTR -9[rbp]
mov edx, eax
lea rcx, .LC6[rip]
call printf
mov eax, DWORD PTR -44[rbp]
mov edx, eax
lea rcx, .LC7[rip]
call printf
movsd xmm0, QWORD PTR -40[rbp]
mov rax, QWORD PTR -40[rbp]
movupd xmm1, xmm0
mov rdx, rax
lea rcx, .LC8[rip]
call printf
mov r9d, 1
mov r8d, 8
Note 3: sizeof is already worked out: 4, 8 and 1 straight in the registers.
mov edx, 4
lea rcx, .LC9[rip]
call printf
lea rcx, .LC10[rip]
call printf
mov edx, 2147483647
lea rcx, .LC11[rip]
call printf
mov eax, 3153600000
Note 4: 100LL * 365 * 24 * 60 * 60: the compiler did the multiplying.
mov QWORD PTR -56[rbp], rax
mov rax, QWORD PTR -56[rbp]
mov rdx, rax
lea rcx, .LC12[rip]
call printf
mov rax, QWORD PTR .LC13[rip]
mov rdx, rax
movq xmm0, rdx
movupd xmm1, xmm0
mov rdx, rax
lea rcx, .LC14[rip]
call printf
mov eax, 0
leave
ret
.LC4:
.long 0
.long 1074266112
.LC13:
.long 858993460
.long 1070805811