1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
INCLUDE Irvine32.inc
.data
welcome BYTE "Welcome! This program determines what the nth Fibonacci number is!",0ah, 0dh,0
input dword ?
result dword ?
save dword ?
.code
main PROC
mov edx, OFFSET welcome ;Message 1 is displayed
call WriteString
call ReadDec ;input from keyboard
call crlf
mov input, eax ;users input is moved into the input variable
mov ecx, input
cmp ecx, 0
jz L1
cmp ecx, 1
jz L1
mov eax, 0 ;The first Fib number is set to eax
mov ebx, 1 ;The second Fib number is set to ebx
add eax, ebx ; eax and ebx is added together
mov result, eax ;The result is moved from eax into the "result" variable
mov save, ebx ;The integer in ebx is moved to the save variable
dec ecx ;Minus the users input by 1
call FibSum ;Calculate the Fib number
L1: call WriteDec
Call Crlf
exit
main ENDP
|
FibSum PROC
;
; Calculates the users input until the nth number is found
;
; Receives: input from user
; Returns: result in eax
;-----------------------------------------------
cmp ecx, 1 ;Compares the users input to 1 if it is equal to one then jumps to quit
jz L2
mov eax, save
mov ebx, result
add eax, ebx
mov result, eax
mov save, ebx
dec ecx
call FibSum ;calls the FibSum function
L2: ret
FibSum ENDP
END main |