Chapter 5
Procedures
Assembly Language Programming Exercise
Problem # 10:
Write a procedure that produces N values in the Fibonacci number series and stores them in an array of doubleword. Input parameters should be a pointer to an array of doubleword, a counter of the number of values to generate. Write a test program that calls your procedure, passing N = 47. The first value in the array will be 1, and the last value will be 2,971,215,073. Use the Visual Studio debugger to open and inspect the array contents.
Solution:
.386
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
array DWORD 1,1
lastVal DWORD 2971215073
count DWORD 47-2
.code
main PROC
mov ESI, OFFSET array
Add ESI, TYPE array
Add ESI, TYPE array
mov ecx, count
L1:
call generateFibonacci
ADD ESI, TYPE array
Loop L1
INVOKE ExitProcess,0
main ENDP
generateFibonacci PROC
MOV EAX,[ESI-4]
MOV EBX,[ESI-8]
ADD EAX,EBX
MOV [ESI],EAX
ret
generateFibonacci ENDP
END main
.model flat,stdcall
.stack 4096
ExitProcess PROTO, dwExitCode:DWORD
.data
array DWORD 1,1
lastVal DWORD 2971215073
count DWORD 47-2
.code
main PROC
mov ESI, OFFSET array
Add ESI, TYPE array
Add ESI, TYPE array
mov ecx, count
L1:
call generateFibonacci
ADD ESI, TYPE array
Loop L1
INVOKE ExitProcess,0
main ENDP
generateFibonacci PROC
MOV EAX,[ESI-4]
MOV EBX,[ESI-8]
ADD EAX,EBX
MOV [ESI],EAX
ret
generateFibonacci ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
Program to Recursive Procedure in Assembly Language using Visual Studio