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?
How does the CPU see it? in practice/05/

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.

Numbered notes explain the assembly lines.

Details & hint

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.

practice 05 recursion.c Code Diff
UNCHANGED This step does not change this file.
#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;
}
opened for this task

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

.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 rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov DWORD PTR 16[rbp], ecx
Note 1: n arrives in ecx and is copied into memory: a parameter is a copy.
mov DWORD PTR 24[rbp], edx
cmp DWORD PTR 16[rbp], 0
js .L4
cmp DWORD PTR 24[rbp], 0
jns .L10
.L4:
mov r8d, 8
Note 2: Line number 8 is a constant for assert's message.
lea rdx, .LC0[rip]
lea rcx, .LC1[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
jmp .L7
.L10:
nop
.L7:
mov eax, DWORD PTR 24[rbp]
add eax, eax
mov r9d, DWORD PTR 16[rbp]
lea r8, .LC2[rip]
mov edx, eax
lea rcx, .LC3[rip]
call printf
cmp DWORD PTR 16[rbp], 9
jg .L8
mov eax, DWORD PTR 16[rbp]
jmp .L9
.L8:
mov edx, DWORD PTR 16[rbp]
movsx rax, edx
imul rax, rax, 1717986919
Note 3: n / 10 without dividing: a multiply by a "magic" number and shifts.
shr rax, 32
sar eax, 2
mov ecx, edx
sar ecx, 31
sub eax, ecx
mov ebx, eax
mov eax, ebx
sal eax, 2
add eax, ebx
add eax, eax
mov ebx, edx
sub ebx, eax
mov eax, DWORD PTR 24[rbp]
lea edx, 1[rax]
mov eax, DWORD PTR 16[rbp]
movsx rcx, eax
imul rcx, rcx, 1717986919
shr rcx, 32
sar ecx, 2
sar eax, 31
mov r8d, eax
mov eax, ecx
sub eax, r8d
mov ecx, eax
call digit_sum
Note 4: Recursion: the same call into itself; then add eax, ebx adds n % 10.
add eax, ebx
.L9:
mov rbx, QWORD PTR -8[rbp]
leave
ret
.LC4:
.ascii "n >= 0 && n <= 20\0"
factorial:
push rbp
mov rbp, rsp
push rbx
sub rsp, 40
mov DWORD PTR 16[rbp], ecx
cmp DWORD PTR 16[rbp], 0
js .L12
cmp DWORD PTR 16[rbp], 20
jle .L18
.L12:
mov r8d, 20
lea rdx, .LC0[rip]
lea rcx, .LC4[rip]
mov rax, QWORD PTR __imp__assert[rip]
call rax
jmp .L15
.L18:
nop
.L15:
cmp DWORD PTR 16[rbp], 0
jne .L16
mov eax, 1
jmp .L17
.L16:
mov eax, DWORD PTR 16[rbp]
movsx rbx, eax
mov eax, DWORD PTR 16[rbp]
sub eax, 1
mov ecx, eax
call factorial
imul rax, rbx
.L17:
mov rbx, QWORD PTR -8[rbp]
leave
ret
.LC5:
.ascii "Digit sum: %d\12\0"
.LC6:
.ascii "5! = %lld\12\0"
.LC7:
.ascii "20! = %lld\12\0"
main:
push rbp
mov rbp, rsp
sub rsp, 48
call __main
mov edx, 0
mov ecx, 472
Note 5: 472 is the first argument, so ecx; 0 is the second, edx.
call digit_sum
mov DWORD PTR -4[rbp], eax
mov eax, DWORD PTR -4[rbp]
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
leave
ret