HOME | DD

ApertureEngineer — _fibonacci PROC
Published: 2009-12-06 19:58:20 +0000 UTC; Views: 192; Favourites: 1; Downloads: 2
Redirect to original
Description .686
.model tiny
public _fibonacci

.code

;Author: ApertureEngineer
_fibonacci PROC
        push ebp
        mov ebp,esp     ;Building stack frame
        push esi
        push edi        ;saving ESI and EDI
        

        mov ebx,[ebp+8] ;retrievieng n from stack
        cmp ebx,0       ;comparing n with 0
        jg cont_1       ; if n>0 then goto con_1
        mov eax,0       ; if n<=0 return 0
        jmp retv
        
        
cont_1: cmp ebx,2       ;comparing n with 2
        jg cont_2       ;if n>2 then procedure will continue on cont_2
        mov eax,1       ;if not then procedure will return 1
        jmp retv
        
        
cont_2: dec ebx         ;  EBX = n-1
        push ebx        ; pushing n-1 as argument for fibonacci call
        call _fibonacci ; calling fibonacci(n-1)
        pop ebx         ; retrievieng ebx (n-1),
                            ;it may change during fibonacci call
        dec ebx         ;  EBX = n-2
        push eax        ; saving value returned by fibonacci(n-1) on stack
        push ebx        ; pushing n-2 as argument for second fibonacci call
        call _fibonacci ; calling fibonacci(n-2)
        add esp,4       ; cleaning stack
        pop ebx         ; retrieving result of fibonacci(n-1) call form stack
                             ;and saving it in EBX
        add eax,ebx     ; adding results of fibonacci(n-2) (in EAX)
                                ;and fibonacci(n-1) (in EBX) calls

retv:   pop edi         ;resuming initial EDI value
        pop esi         ;resuming initial ESI value
        mov esp,ebp     ;removing stack frame
        pop ebp         
        ret             ; returning fibonacci(n-2)+fibonacci(n-1)
                                 ;as fibonacci(n) result
_fibonacci ENDP

END
Related content
Comments: 0