mov eax,[ebp-4]
You use memory in [ebp-4] without investing anything useful in it! You must reserve this space in your prolog function:
_fibonacci: push ebp mov ebp, esp sub esp, 4
Returning from the first recursive call, you put the result from EAX into this memory slot.
Returning from the second recursive call, you add the contents of this memory dictionary to EAX .
Thus, the EDX register will no longer be closed.
Why are you using the EBX register at all? If you use it, you must save it, as explained in Al Kepp's answer .
If you start by placing the argument in EAX , you know that for both values โโbelow 2 (so 0 and 1), the result will be equal to the argument. Plain.
mov eax, [ebp+8] ;; param n cmp eax, 2 jb _endFIbofunc
If you do not balance the stack immediately after the first recursive call, you can simply reduce the colon that already exists and make a second recursive call.
dec eax ; n-1 push eax ;(*) call _fibonacci mov [ebp-4], eax dec dword ptr [esp] ; n-2 call _fibonacci add esp,4 ;(*) add eax, [ebp-4]
The whole process:
_fibonacci: push ebp mov ebp, esp sub esp, 4 ;(*) mov eax, [ebp+8] ;; param n cmp eax, 2 jb _endFIbofunc dec eax ; n-1 push eax ;(*) call _fibonacci mov [ebp-4], eax dec dword ptr [esp] ;(*) n-2 call _fibonacci add esp,4 ;(*) add eax, [ebp-4] _endFIbofunc: mov esp, ebp pop ebp ret