BETA Try the new lesson view
Theory

Extra: how does the CPU see it?

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.