Fibonnaci Sequence of a given number (Assembly Language)

; Program to compute fibonnaci sequence of a given number

DATA SEGMENT ;INITIALIZE DATA SEGMENT
COUNT DW 0AH ;INITIALIZE COUNT TO 0AH ANY DATA
RES DB 10 DUP(?) ;INITIALIZE RESULT
DATA ENDS ;END OF DATA SEGMENT

CODE SEGMENT ;INTIALIZE CODE SEGMENT
START: ;START THE CODE
ASSUME CS:CODE,DS:DATA ;ASSUMPTION OF CODE AND DATA
MOV AX,DATA ;MOVE DATA INTO THE ACCUMULATOR AX REGISTER
MOV DS,AX ;MOVE AX INTO THE DATA SEGMENT DS REGISTER
MOV CX,COUNT ;MOV COUNT INTO CODE SEGMENT CS REGISTER WHICH WILL ACT AS A COUNT
MOV SI, OFFSET RES ;MOV RESULT INTO THE SOURCE INDEX. SOURCE INDEX IS A KIND OF STACK
MOV AL,00H ;INITIALIZE LOWER 8 BIT ACCUMULATOR AS 00H;
MOV BL,01H ;INITIALIZE UPPER 8 BIT ACCUMULATOR AS 01H;
MOV [SI],AL ;MOV AL INTO SI.
;ASSUME SOURCE INDEX(SI) ADDRESS IS 0001H;
;0001H LOCATION CONTAINS 00H;
INC SI ;INCREMENT SI
MOV [SI],BL ;MOV BL INTO SI
;AFTER INCREMENTING IN THE ABOVE STEP THE NEW LOCATION OF SI IS 0002H
;0002H LOCATION CONTAINS 01H;
INC SI ;INCREMENT SI
UP: MOV AL,[SI-2] ;UP USED AS A LABEL
;MOV THE CONTENTS OF THE SOURCE INDEX INTO AL
;MOVING CONTENTS OF SOURCE INDEX AT DESTINATION [SI-2] =>0002H
MOV BL,[SI-1] ;MOV THE CONTENTS OF THE SOURCE INDEX INTO BL
;MOVING CONTENTS OF SOURCE INDEX AT DESTINATION [SI-1] =>0001H
ADD AL,BL ;ADDING THE CONTENTS OF AL AND BL
MOV [SI],AL ;MOVING THE CONTENTS OF AL INTO THE SOURCE INDEX
;NOW THE LOCATION OF SOURCE INDEX WILL BE 0003H DUE TO INC INSTRUCTION
;IN THE STEP ABOVE UP LABEL
INC SI ;INCREMENT SI WITHIN THE LOOP
;NOW THE CURRENT LOCATION OF SI IS 0004H;
LOOP UP ;END OF LOOP
MOV AH,4CH ;END OF CODE
INT 21H ;END OF CODE
CODE ENDS ;END OF CODE SEGMENT
END START ;END OF START

;INPUT
; NUM1 – 0AH

;OUTPUT – 00 01 01 02 03 05 08 0D 15 22 37

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s