global _start

section .data

section .text

_start:
	;put something for fib to calculate here
	call _fib
	je		exit

_fib:
	push ebp
    mov  ebp, esp
    sub  esp, 16    ; ...AA-A-A-ND we've built the stack frame
    mov  eax, [ebp+8]
    cmp  eax, 2
	jae  .recur
	xor  edx, edx
	jmp  .done
	
.recur:
	sub  eax, 2
    push eax            ; compute fib(n-2)
    call _fib
    mov  [ebp-8], eax   ; save returned value in 8-byte local variable...
    mov  [ebp-4], edx   ; ...in Little-Endian byte order.
    mov  eax, [ebp+8]   ; get argument again
    dec  eax
    push eax            ; compute fib(n-1)
    call _fib
    mov  [ebp-16], eax  ; save returned value in 8-byte local variable...
    mov  [ebp-12], edx  ; ...in Little-Endian byte order.
	; the next steps are not as efficient as they could be...
    mov  eax, [ebp-8]
    mov  edx, [ebp-4]   ; retrieve 1st computed value
    add  eax, [ebp-16]
    adc  edx, [ebp-12]  ; add 2nd computed value

.done:
    mov  esp, ebp
    pop  ebp
    ret

exit:
	mov		eax, 01h		; exit()
	xor		ebx, ebx		; errno
	int		80h
