Framework in C
#include <stdio.h>
#include <inttypes.h>
#define VALUE 10
extern uint64_t fib_rec_asm (uint64_t value);
uint64_t fib_rec_c (uint64_t value);
int main(int argc, char const * argv[])
{
uint64_t value1 = fib_rec_asm(VALUE);
uint64_t value2 = fib_rec_c(VALUE);
printf("ASM: %"PRId64"\n", value1);
printf("C : %"PRId64"\n", value2);
return 0;
}
uint64_t fib_rec_c (uint64_t value)
{
if (value < 3)
{
return 1;
}
return (fib_rec_c(value-1) + fib_rec_c(value-2));
}
Fibonacci in NASM
global fib_rec_asm
section .data
v dq 0x0000000000000000 ; store value
section .text
fib_rec_asm:
push rdi ; rdi in use after call -> save it
;ANCHOR
xor rax, rax ; start with empty rax register
cmp rdi, 2
jg rec
mov rax, 1
jmp return
rec:
;RECURSION
dec rdi
call fib_rec_asm
add [v], rax
dec rdi
call fib_rec_asm
add [v], rax
mov rax, [v]
xor rdx, rdx
mov [v], rdx
return:
pop rdi ; restore saved register
ret