This blog post will walk you through an 8086 assembly program designed to generate the Fibonacci sequence. While this may sound simple, it’s an excellent example for understanding looping, arithmetic operations, and register management in assembly language.
The Fibonacci Series
The Fibonacci series is a sequence where each number is the sum of the two preceding ones:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
In this program, we’ll generate the first 10 Fibonacci numbers and store them in memory.
Code
; 8086 Program to Generate 10 Fibonacci Numbers
data segment
COUNT equ 10 ; Number of terms to generate
FIB_SERIES db COUNT dup(?) ; Array to store the series
data ends
code segment
assume cs:code, ds:data
start:
; Initialize Data Segment (DS) register
mov ax, data
mov ds, ax
; Use SI as a pointer to the array
mov si, offset FIB_SERIES
; Seed the first two Fibonacci numbers
; Fib(0) = 0
mov byte ptr [si], 00h
; Fib(1) = 1
inc si
mov byte ptr [si], 01h
; Set up loop counter. We already have 2 numbers,
; so we need to generate (COUNT - 2) more.
mov cx, COUNT
sub cx, 2
L1:
; AL = Fib(n-1) (e.g., [si])
mov al, [si]
; BL = Fib(n-2) (e.g., [si-1])
mov bl, [si-1]
; AL = Fib(n-1) + Fib(n-2)
add al, bl
; Move pointer to next position Fib(n)
inc si
; Store the new term: [si] = AL
mov [si], al
; Decrement CX, loop if CX is not zero
loop L1
; Halt execution for debugging
int 3
code ends
end start