;********************************************************************************
; Purpose:   To compute a certain number of fibonacci numbers and then print
;            a specified few of the computed fibonacci numbers. The fibonacci
;            numbers are 1, 1, 2, 3, 5, where the nth fibonacci number is
;            found by adding the previous two fibonacci numbers.
;
; Inputs:    None
;
; Outputs:   The 10th, 25th, and 35th fibonacci numbers.
;
; Date:      March 14, 2005
;
; Author:    Doug Ryan
;
; Algorithm: 1) Initialize an array fib with all 1's
;            2) Repeat
;            3)   Compute the nth fibonacci number by computing
;                 fib[n] = fib[n-1] + fib[n-2]
;            4) Until last fibonacci number is computed
;            5) Print the 10th, 25th, and 35th fibonacci numbers
;********************************************************************************

;********************************************************************************
;                           INCLUDE FILES
;********************************************************************************
INCLUDE Irvine16.inc

;********************************************************************************
;                           STACK SEGMENT
;********************************************************************************
.STACK 100h

;********************************************************************************
;                           DATA SEGMENT
;********************************************************************************

.CONST
MAX_FIBS          =  35
FIBS_COMPUTED     =   2
FIRST_FIB         = TYPE aFib *  9
SECOND_FIB        = TYPE aFib * 24
THIRD_FIB         = TYPE aFib * 34

szFirstFib        BYTE  "  The 10th Fibonacci Number Is: ",0
szSecondFib       BYTE  "  The 25th Fibonacci Number Is: ",0
szThirdFib        BYTE  "  The 35th Fibonacci Number Is: ",0
szProgramHeading  BYTE  "PROGRAM TO OUTPUT A FEW FIBONACCI NUMBERS",0dh,0ah
                  BYTE  "_________________________________________",0dh,0ah
                  BYTE  0dh,0ah,0dh,0ah,0

.DATA
aFib              DWORD       MAX_FIBS dup (1)

;********************************************************************************
;                           CODE SEGMENT
;********************************************************************************
.CODE
main      PROC

          mov ax, @data                      ; set ds and es to point to the
          mov ds, ax                         ; data segment since di and si 
          mov es, ax                         ; are used as pointers

          call Clrscr
          mov edx, OFFSET szProgramHeading   ; output the program title
          call WriteString

          mov si, OFFSET aFib                ; si = fib[n-2]
          mov di, OFFSET aFib + TYPE aFib    ; di = fib[n-1]
          mov cx, MAX_FIBS - FIBS_COMPUTED   ; subtract the two fibonacci 
                                             ; numbers already computed and 
                                             ; place the count in cx

L1:       mov eax, DWORD PTR [si]            ; temp = fib[n-2] + fib[n-1]
            add eax, DWORD PTR [di]
            mov si, di                       ; move si and di to point one 
            add di, TYPE aFib                ; element ahead respectively
            mov DWORD PTR [di], eax          ; save the new fibonacci number
          loop L1                            ; loop until all of the fibonacci 
                                             ; numbers are computed

          mov edx, OFFSET szFirstFib         ; output the first fibonacci number
          call WriteString
          mov eax, aFib[FIRST_FIB]              
          call WriteDec
          call Crlf

          mov edx, OFFSET szSecondFib        ; output the second fibonacci number
          call WriteString
          mov eax, aFib[SECOND_FIB]             
          call WriteDec
          call Crlf

          mov edx, OFFSET szThirdFib         ; output the third fibonacci number
          call WriteString
          mov eax, aFib[THIRD_FIB]            
          call WriteDec
          call Crlf

          exit
main      ENDP
END       main


