汇编语言的简单入门--斐波那契数列(非递归)

根据函数

F(0)=0;F(1) = 1;F(n) = F(n-2) + F(n-1);

 

TITLE Save an array and dispaly


INCLUDE Irvine32.inc
.data

array DWORD 12 DUP (?)   ; define a array for saving Fibonacci numbers
step = type array

prompt byte "The first twelve fibonacci numbers are ",0
prompt1 DWORD "  ",0
		
.code
main PROC

mov esi,OFFSET array                ;edi = address of array
                    
mov ecx,lengthof array              ;initialize loop conuter
                 
                      
mov edx,offset prompt               ;place the zero-ended string's offset in EDX
call writestring					;output the prompt

mov edx,offset prompt1				;place the zero-ended string's offset in EDX

mov edi,0                           ;assign 0 to the first element
mov [esi],edi

mov eax,[esi]                       ;mov the first element to eax for outping
call writeint
call writestring

mov edi,1							;assign 1 to the first element              
mov [esi + 4],edi

mov eax,[esi + 4]					;mov the second element to eax for outping
call writeint
call writestring

sub ecx,2                           ;because we have output two element in array so we just need to the remain of element . 

L1:		

	mov edi,0                       ;every time we use this register,we need to clear it.
	add edi,[esi]
	add esi,step                    ;point to next element
			
	add edi,[esi]
	add esi,step                    ;point to next element
	
	mov [esi],edi
	mov eax,[esi]		            ;move an integer
	
	call writeint
	call writestring
	sub esi,step					;point to last element
	loop L1
	
	call waitmsg
exit
main ENDP
END main


 

你可能感兴趣的:(汇编语言)