Back

;Matt Kaliszewski
;October 1, 2002
;Lab 03 - Problem 7

;////////////////////////////////////////////////////////////////////////////
;Code:

title Fibonacci Sequence

; this program generates a the first 24 numbers of
; the Fibonacci number sequence

.model small
.stack 100h
.data
       prev1 dw 0000h
       prev2 dw 0000h
       currNum dw 0000h

.code

extrn Writeint:proc, Crlf:proc

main proc
       mov ax,@data                            ; copy the address of the data segment to ax
       mov ds,ax                                   ; move the address of the data segment into the ds register

       ; initialize and print out the first number in the sequence
       mov prev1,0001h                            ; set the last value to 1

       mov ax,prev1                            ; put the number to be displayed into ax
       mov bx,10                                   ; base 10
       call Writeint                            ; display the number
       call Crlf                                   ; go to the next line

       mov cx,23d;                                   ; initialize the counter to 23

fibonacci:
       mov bx,prev1                            ; move the last value into bx
       add bx,prev2                            ; add the second to last value with the last value
       mov currNum,bx                            ; move the result into currNum
       
       mov bx,prev1                            ; move the last value into bx
       mov prev2,bx                            ; set the second to last value to the last value
       mov bx,currNum                            ; move the current value into bx
       mov prev1,bx                            ; set the last value to the current value

       mov ax,currNum                            ; put the number to be displayed into ax
       mov bx,10                                   ; base 10
       call Writeint                            ; display the number
       call Crlf                                   ; go to the next line

       loop fibonacci                            ; decrement cx and loop if cx is greater than 0

       mov ax,4C00h                            ; get ready to exit DOS
       int 21h                                          ; send the command to DOS

main endp
end main

; output follows

1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368

Back