You are looking at lesson 5. Your place is still Practice test 1, task 1: About.
Extra: a function call is call and ret, arguments travel in registers, and recursion is the same call into the same function. Look at practice/05/recursion.c.
Extra. Not graded. What happens when a function is called, and when it calls itself?
Open the tool window's Assembly tab. On the left is your practice/05/recursion.c.
call digit_sum goes to the start of the function and remembers where to come back; ret at the end of the function goes back.n in ecx, depth in edx. The result comes back in eax. At -O0, both arguments are copied into memory at the start of the function (mov DWORD PTR 16[rbp], ecx): that is topic 5's "a parameter is a copy".push rbp, mov rbp, rsp, sub rsp, 40: it makes its own room on the stack. A recursive call is the same call, only into the same function, so every depth gets its own room. Without a base case, the stack would run out of room in the end.return n % 10 + digit_sum(n / 10, depth + 1); first computes n % 10 and keeps it in ebx, then calls, and adds it once the call returns: add eax, ebx.n / 10: instead, an imul by the odd number 1717986919 and shifts. Multiplying is faster than dividing, and for integers the result is exactly the same.assert is a plain if: a comparison and, when the condition is not met, a call to _assert (in the assembly __imp__assert: the function sits in a Windows DLL) with the file name and the line number (mov r8d, 8). That is where "Assertion failed … line 8" comes from.Switch to -O1: factorial keeps n in the register ebx, which a call leaves unchanged, and multiplies only once the recursive call has returned.
CLion shows the same machine code. Put a breakpoint on the return line of digit_sum and start Debug. In the Debug window open the GDB tab (the debugger's console) and type, once per debug session:
set disassembly-flavor intel
Then right-click the top line of the Frames list and choose Disassemble: Intel syntax, as here. Your CLion compiles with GCC 11.2, not with the version that made this view, so some instructions may differ slightly.
Optional. The repository's .gdbinit gives the same command at every start, but GDB reads it only if you allow it (README.md says how). The command in the GDB tab always works.
C source recursion.c
x86-64 assembly -O0
x86-64 assembly -O1
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.