Skip to the task
Layout
Back to classic view

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

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.

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

.LC0:
.ascii "Year of birth: \0"
.LC1:
.ascii "Grade 1: \0"
.LC2:
.ascii "Grade 2: \0"
.LC3:
.ascii "Grade 3: \0"
.LC4:
.ascii "Year of study: %d\12\0"
.LC5:
.ascii "Group: PI25%c\12\0"
.LC6:
.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
push rdi
push rsi
push rbx
sub rsp, 40
call __main
lea rcx, .LC0[rip]
call read_int
mov ebp, eax
lea rcx, .LC1[rip]
call read_int
mov ebx, eax
lea rcx, .LC2[rip]
call read_int
mov edi, eax
lea rcx, .LC3[rip]
call read_int
mov esi, eax
mov edx, 1
Note 1: study_year is gone: printf just gets 1.
lea rcx, .LC4[rip]
call printf
mov edx, 65
lea rcx, .LC5[rip]
call printf
mov edx, 2026
sub edx, ebp
lea rcx, .LC6[rip]
call printf
add ebx, edi
add ebx, esi
pxor xmm1, xmm1
cvtsi2sd xmm1, ebx
divsd xmm1, QWORD PTR .LC7[rip]
movq rdx, xmm1
lea rcx, .LC8[rip]
call printf
mov r9d, 1
mov r8d, 8
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 edx, 3153600000
lea rcx, .LC12[rip]
call printf
mov rax, QWORD PTR .LC13[rip]
movq xmm1, rax
movabs rdx, 0x3fd3333333333334
lea rcx, .LC14[rip]
call printf
mov eax, 0
add rsp, 40
pop rbx
pop rsi
pop rdi
pop rbp
ret
.LC7:
.long 0
.long 1074266112
.LC13:
.long 858993460
.long 1070805811