Skip to the task
Layout
Back to classic view

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

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

How does the CPU see it?

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.

A call

  • 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.
  • Arguments travel in registers: 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".
  • Every call starts with 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.

What to notice (-O0)

  • 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.
  • There is no division for 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.

In CLion's debugger

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.

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

-O0 -O1

C source recursion.c

#include <assert.h>
#include <stdio.h>
/* The digit sum; depth is how many calls deep the function already is.
Before: n >= 0, depth >= 0.
After: the sum of the digits of n. */
int digit_sum(int n, int depth) {
assert(n >= 0 && depth >= 0);
printf("%*sdigit_sum(%d)\n", depth * 2, "", n);
if (n < 10) {
return n;
}
return n % 10 + digit_sum(n / 10, depth + 1);
}
/* n! = 1 * 2 * ... * n.
Before: n is from 0 to 20 (21! doesn't fit in a long long).
After: the factorial of n; 0! = 1. */
long long factorial(int n) {
assert(n >= 0 && n <= 20);
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
int main(void) {
int sum = digit_sum(472, 0);
printf("Digit sum: %d\n", sum);
printf("5! = %lld\n", factorial(5));
printf("20! = %lld\n", factorial(20));
return 0;
}

x86-64 assembly -O1

.LC0:
.ascii "practice/05/recursion.c\0"
.LC1:
.ascii "n >= 0 && depth >= 0\0"
.LC2:
.ascii "\0"
.LC3:
.ascii "%*sdigit_sum(%d)\12\0"
digit_sum:
push rsi
push rbx
sub rsp, 40
mov ebx, ecx
mov esi, edx
test ecx, ecx
js .L7
test edx, edx
js .L7
.L4:
lea edx, [rsi+rsi]
mov r9d, ebx
lea r8, .LC2[rip]
lea rcx, .LC3[rip]
call printf
cmp ebx, 9
jg .L9
.L6:
mov eax, ebx
add rsp, 40
pop rbx
pop rsi
ret
.L7:
mov r8d, 8
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
call [QWORD PTR __imp__assert[rip]]
jmp .L4
.L9:
lea edx, 1[rsi]
movsx rsi, ebx
imul rsi, rsi, 1717986919
sar rsi, 34
mov eax, ebx
sar eax, 31
sub esi, eax
mov ecx, esi
call digit_sum
mov edx, eax
lea eax, [rsi+rsi*4]
add eax, eax
sub ebx, eax
add ebx, edx
jmp .L6
.LC4:
.ascii "n >= 0 && n <= 20\0"
factorial:
push rbx
sub rsp, 32
mov ebx, ecx
cmp ecx, 20
ja .L16
mov eax, 1
test ecx, ecx
jne .L12
.L10:
add rsp, 32
pop rbx
ret
.L16:
mov r8d, 20
lea rdx, .LC0[rip]
lea rcx, .LC4[rip]
call [QWORD PTR __imp__assert[rip]]
.L12:
lea ecx, -1[rbx]
call factorial
mov rcx, rax
movsx rax, ebx
imul rax, rcx
jmp .L10
.LC5:
.ascii "Digit sum: %d\12\0"
.LC6:
.ascii "5! = %lld\12\0"
.LC7:
.ascii "20! = %lld\12\0"
main:
sub rsp, 40
call __main
mov edx, 0
mov ecx, 472
call digit_sum
mov edx, eax
lea rcx, .LC5[rip]
call printf
mov ecx, 5
call factorial
mov rdx, rax
lea rcx, .LC6[rip]
call printf
mov ecx, 20
call factorial
mov rdx, rax
lea rcx, .LC7[rip]
call printf
mov eax, 0
add rsp, 40
ret