include irvine32.inc
.data
str1 byte "请输入所要的斐波那序列的位置:",0;
str2 byte "斐波那序列的结果为:",0;
str3 byte "斐波那序列的最大值为N=",0;
i dword 2;
var1 dword 3 dup(1,0,0);
var2 dword 3 dup(1,0,0);
.code
main proc
mov ecx,0;
mov eax,0;
L1:
mov var1,1;
mov dword ptr var1+4,0;
mov dword ptr var1+8,0;
mov var2,1;
mov dword ptr var2+4,0;
mov dword ptr var2+8,0;
mov edx, offset str1;
call writestring ;
call readint;输入所要的斐波那序列的位置;
CMP eax,0;如果要结束就输入小于等于0
jng next;
cmp eax,2;判断小于2的情况输出1;
jng L3;
;call crlf;
sub eax,2;
mov ecx,eax;将循环次数赋给ECX=EAX-2;
;大于2的情况求解过程;
mov eax,0;
mov ebx,0;
mov edx,0;
mov i,2;N=i;记住每次求值时必须回归初态即:i=2
;edx为和的结果,ebx 为f(n-1)的值,eax为f(n-2)的值
L2:
add edx,var1;//f(n)=f(n-1)+f(n-2)
add edx,var2;
mov eax,dword ptr var1+4;
adc eax,dword ptr var2+4;
mov ebx,dword ptr var2+4;
mov dword ptr var1+4,ebx;
mov dword ptr var2+4,eax;
mov eax,dword ptr var1+8;
adc eax,dword ptr var2+8;
mov ebx,dword ptr var2+8;
mov dword ptr var1+8,ebx;
mov dword ptr var2+8,eax;
cmp eax,ebx;
jb next1;//判断参数的合法性,如果f(n)小于f(n-1)则可以判断发生了溢出,此时 N=i;
mov eax,var2;
mov var1,eax;
mov var2,edx;
mov edx,0;
inc i;//记录N的值;
LOOP L2;实现循环
mov edx, offset str2;
call writestring ;
mov eax,dword ptr var2+8;
call writehex;
mov al,',';
call writechar;
mov eax,dword ptr var2+4;
call writehex;
mov al,',';
call writechar;
mov eax,dword ptr var2;
call writehex;
call crlf;
mov eax,1;
cmp eax,1
je L4;//跳过小于2的情况求解过程;
;小于2的情况求解过程;
L3:
mov edx, offset str2;
call writestring ;
mov eax,1;
call writeint;
call crlf;
L4:
jmp L1;//实现多次求值的情况;直到求的值越界或者是输入的值小于1(不合法)
next:
jmp quit;
;call writeint;
next1:
mov edx, offset str3;
call writestring ;
mov eax,0;
mov eax,i;
call writeint;
quit:
exit;
main endp
end main