Thanks for you reply. You said [ "pop rdi" will gets the return address!] , but I found [pop rdi] actually works well.
I am new to NASM or assembly, please make sure you didn't miss something.
after some googling, I figured out what's wrong.
first, I did not save rcx, so its changed during recurse.
second, I forget ret after add f(n-1) and f(n-2).
now it works.
`asm
global fib
section .text
fib:
cmp rdi, 1 ; if less than 1
jle done ; return
; calculate f(n-1)
dec rdi ; n-1
push rdi ; push n-1
call fib ; calculate f(n-1)
mov rcx, rax ; get f(n-1) results and save to rcx
pop rdi ; restore n-1
; calculate f(n-2)
push rcx ; save rcx
dec rdi ; n-2
push rdi ; push n-2
call fib ; calculate f(n-2)
pop rdi ; restore n-2
pop rcx ; restore rcx
add rax, rcx ; calulate f(n-1) + f(n-2)
ret
done:
mov rax, rdi
ret
`