Assembly to implement Fibnacci

The 32-bit assembly environment is used here, and the compiler is VS2010


.386
.model flat,stdcall
option casemap:none

includelib msvcrt.lib
printf  PROTO C :dword, :VARARG

.data   ;数据段
    num dword 15
    szOut byte 'FIB(%d)=%d',0ah,0

.code ;代码段开始
factorial proc C n:dword
    cmp n, 2
    jbe exitrecurse
    mov ebx, n          ;EBX=n
    dec ebx             ;EBX=n-1
    invoke factorial, ebx   ;EAX=(n-1)!
    cmp edx,0
    je scan
    add ecx,eax
    scan:
    mov ebx, n
    sub ebx,2
    invoke factorial,ebx;
    add ecx,eax                 ;EAX=EAX*n
    mov edx,0
    ret
exitrecurse:
    mov eax, 1          ;n=1时,n!=1
    ret
factorial endp

start:
    mov ecx,0
   invoke factorial, num
   invoke printf, offset szOut, num, ecx
   ret
end start;  代码段结束

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325046328&siteId=291194637