Skip to the task
Layout
Back to classic view

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

CHECKTask 20 of 23 · step 8: Extra: how does the CPU see it?Which register?
Which register? in practice/03/

main calls digit_sum(472, 0). In which register does the function get 472, and in which does it return the result?

Numbered notes explain the assembly lines.

Details & hint

Stuck? 472 in ecx (the first argument), 0 in edx (the second). The result comes back in eax: after the call, main stores it in sum.

practice 03 CMakeLists.txt Code Diff
UNCHANGED This step does not change this file.
add_executable(p03_grades main.c)
target_link_libraries(p03_grades PRIVATE course_input)
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