# 8086 program to generate Fibonacci Sequence

**Problem – ** Write a 8086 assembly level program to generate the Fibonacci Sequence. The length of the sequence is stored in the data segment with the offset value of 0. We will store the generated sequence in the data segment, from the offset value of 500.

* Note:*The generated numbers and memory locations are represented in their Hexa-Decimal format.

**Example:**

**Input:**

**Output:**

**Algorithm:**

The Fibonacci sequence is generated by adding the (i)th element and the (i-1)th element, and storing it into the (i+1)th position. This holds good given that the 1st and 2nd positions are initialized with 0 and 1 respectively. The following steps need to be followed to execute the process using the Assembly Level instructions.

- Move the value stored at offset 00H into CX(this will act as the counter), and decrement it by 2 (because we need to explicitly load the first 2 elements of the sequence)
- Move 00H into AL
- Move 500 into SI
- Move AL into [SI]
- Increment both AL and SI by 1, and store AL’s value in [SI] (with this, we have loaded the first 2 elements of the sequence into the memory)
- Move [SI-1]th value into AL
- Move [SI]th value into AH
- Move 00H into BH
- Add BH and AH (result stored in BH)
- Add BH again with AL
- Increment SI by 1
- Store BH into [SI]
- Loop back to Step 6 till counter becomes 0
- Stop

**Program:**

Memory Address | Instruction/Mnemonic | Explanation/Comment |
---|---|---|

2000 | MOV AL, 00H | AL <- 00 |

2002 | MOV SI, 500H | SI <- 500 |

2005 | MOV [SI], AL | [SI] <- 00 |

2007 | ADD SI, 01H | SI <- SI + 01 |

200A | ADD AL, 01H | AL <- AL + 01 |

200C | MOV [SI], AL | SI <- AL |

200E | MOV CX, [0000H] | CX <- 0000 |

2012 | SUB CX, 0002H | CX <- CX – 02 |

~2015 | L1: | LABEL |

2015 | MOV AL, [SI-1] | AL <- [SI – 01] |

2018 | ADD AL, [SI] | AL <- AL + [SI] |

201A | ADD SI, 01H | SI <- SI + 01 |

201D | MOV [SI], AL | [SI] <- AL |

201F | LOOP L1 | LOOP L1 CX TIMES |

2021 | HLT | END |

**Explanation:**

**MOV AL, 00H:**AL now has the 1st number from the sequence**MOV SI, 500H:**Making the SI point to the output location**MOV [SI], AL:**Moving 0 into the first position**ADD SI, 1:**Increment SI to point to the next memory location**ADD AL, 1:**Now, AL has the 2nd element of the sequence**MOV [SI], AL:**Moving 01H into the 2nd position**MOV CX, [0000H]:**Moving the value stored at offset 0 into CX(counter)**SUB CX, 02H:**Since we have initialised the first 2 elements of the sequence, we need to decrement the counter by 2**L1:**This defines the start of the loop (A label is created)**MOV AL, [SI-1]:**Moves the element in the (i-1)th position into AL**ADD AL, [SI]:**Moves the (i)th element with the (i-1)th element already present in AL**ADD SI, 1:**Increment SI to point to the next position**MOV [SI], AL:**Store the sum in the new position**LOOP L1:**The instructions between label L1 and this LOOP instruction are executed “CX” times**HLT:**Ends the program

## Actual Output:

The circled memory location (0000) contains the length of the sequence. For this program, it is 8.

The highlighted values are the elements of the Fibonacci Sequence (in their Hexa-Decimal representation. Hence, 13 is represented as 0D)

Refer for: 8085 program to generate Fibonacci series

Enjoy coding!

## Recommended Posts:

- 8086 program to generate G.P. series of n numbers
- 8086 program to generate AP series of n numbers
- 8085 program to generate Fibonacci series
- 8086 program to add two 8 bit BCD numbers
- 8086 program to subtract two 16 bit BCD numbers
- 8086 program to print a String
- 8086 program to add two 16 bit BCD numbers with carry
- 8086 program to multiply two 16-bit numbers
- 8086 program to add two 16-bit numbers with or without carry
- 8086 program to multiply two 8 bit numbers
- 8086 program to find the min value in a given array
- 8086 program for selection sort
- 8086 program to reverse a string
- 8086 program to subtract two 8 bit BCD numbers
- 8086 program to Print a 16 bit Decimal number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.