Menu
CoddyTech

Two Recursive Calls

Part of the Functions, Stack and Strings section of Coddy's Assembly journey. Lesson 19 of 33.

The Fibonacci numbers start 0, 1, 1, 2, 3, 5, 8: each one is the sum of the two before it. As a function: fib(n) = fib(n - 1) + fib(n - 2), with fib(0) = 0 and fib(1) = 1 as base cases. Each call makes two calls.

The first call leaves its result in rax, and the second call overwrites rax. Keep the first result in a callee-saved register such as rbx: every function that uses rbx restores it, so it survives the call. Save your caller's rbx first.

The whole function, with n in rdi and fib(n) in rax:

fib:
    cmp rdi, 2
    jl .base
    push rbx
    push rdi
    dec rdi
    call fib
    mov rbx, rax
    pop rdi
    sub rdi, 2
    call fib
    add rax, rbx
    pop rbx
    ret
.base:
    mov rax, rdi
    ret

The pushes and pops pair up in reverse order: push rbx and push rdi at the start, pop rdi before the second call, pop rbx before ret.

Two calls per level add up: fib(25) makes about 240,000 calls. The stack stays small, because each call returns before the next one starts: the deepest chain is 25 calls.

challenge icon

Challenge

Easy

Write a recursive function fib: n in rdi (0 to 25), the Fibonacci number fib(n) in rax, with fib(0) = 0 and fib(1) = 1. solve receives n in rdi, calls fib and returns its result in rax.

Example: for the input 10 the program prints 55.

Write your code in solution.asm. The locked main.asm reads the input, calls solve and prints the number in rax, or the name of a callee-saved register that solve changed.

Try it yourself

bits 64
section .data
    changed_rbx db 'solve changed rbx', 10
    changed_rbp db 'solve changed rbp', 10
    changed_r12 db 'solve changed r12', 10
    changed_r13 db 'solve changed r13', 10
    changed_r14 db 'solve changed r14', 10
    changed_r15 db 'solve changed r15', 10
section .bss
    input_buffer resb 256
    output_buffer resb 32
    values resq 4
section .text
    global _start
    extern solve
_start:
    xor eax, eax
    xor edi, edi
    mov rsi, input_buffer
    mov edx, 255
    syscall
    test rax, rax
    jle .loaded
    mov rsi, input_buffer
    lea rdi, [input_buffer + rax]
    xor ecx, ecx
.scan:
    cmp rsi, rdi
    jae .loaded
    cmp ecx, 4
    jae .loaded
    movzx eax, byte [rsi]
    cmp al, 32
    jbe .space
    mov r10, 1
    cmp al, '-'
    jne .number
    mov r10, -1
    inc rsi
.number:
    xor eax, eax
.digit:
    cmp rsi, rdi
    jae .store
    movzx edx, byte [rsi]
    cmp dl, '0'
    jb .store
    cmp dl, '9'
    ja .store
    imul rax, rax, 10
    sub edx, '0'
    add rax, rdx
    inc rsi
    jmp .digit
.store:
    imul rax, r10
    mov [values + rcx * 8], rax
    inc ecx
.space:
    inc rsi
    jmp .scan
.loaded:
    mov rbx, 0x1111111111111111
    mov rbp, 0x2222222222222222
    mov r12, 0x3333333333333333
    mov r13, 0x4444444444444444
    mov r14, 0x5555555555555555
    mov r15, 0x6666666666666666
    mov rdi, [values]
    mov rsi, [values + 8]
    mov rdx, [values + 16]
    mov rcx, [values + 24]
    xor eax, eax
    call solve
    mov rdx, 0x1111111111111111
    lea rsi, [changed_rbx]
    cmp rbx, rdx
    jne .changed
    mov rdx, 0x2222222222222222
    lea rsi, [changed_rbp]
    cmp rbp, rdx
    jne .changed
    mov rdx, 0x3333333333333333
    lea rsi, [changed_r12]
    cmp r12, rdx
    jne .changed
    mov rdx, 0x4444444444444444
    lea rsi, [changed_r13]
    cmp r13, rdx
    jne .changed
    mov rdx, 0x5555555555555555
    lea rsi, [changed_r14]
    cmp r14, rdx
    jne .changed
    mov rdx, 0x6666666666666666
    lea rsi, [changed_r15]
    cmp r15, rdx
    jne .changed
    lea rsi, [output_buffer + 31]
    mov byte [rsi], 10
    mov ecx, 1
    xor r8d, r8d
    test rax, rax
    jns .positive
    mov r8d, 1
    neg rax
.positive:
    mov r9d, 10
.convert:
    xor edx, edx
    div r9
    add dl, '0'
    dec rsi
    mov [rsi], dl
    inc ecx
    test rax, rax
    jnz .convert
    test r8d, r8d
    jz .write
    dec rsi
    mov byte [rsi], '-'
    inc ecx
.write:
    mov edx, ecx
    mov eax, 1
    mov edi, 1
    syscall
.exit:
    mov eax, 60
    xor edi, edi
    syscall
.changed:
    mov eax, 1
    mov edi, 1
    mov edx, 18
    syscall
    jmp .exit
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Functions, Stack and Strings

Practice on your own: Assembly playground