0% found this document useful (0 votes)
14 views4 pages

MP 11

This document contains an assembly language program that generates Fibonacci numbers based on user input. It prompts the user to enter the number of Fibonacci numbers to display, calculates them, and prints the results. The program uses DOS interrupts for input and output operations.

Uploaded by

tanmay.aj2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

MP 11

This document contains an assembly language program that generates Fibonacci numbers based on user input. It prompts the user to enter the number of Fibonacci numbers to display, calculates them, and prints the results. The program uses DOS interrupts for input and output operations.

Uploaded by

tanmay.aj2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment No: 11

Code:
.model small
.stack 100h

.data
prompt db "Enter the value of n: $"
newline db 0Ah, 0Dh, "$"
result db "Fibonacci Numbers are: $"
n db 0 ; Variable to store the number of Fibonacci numbers to generate
fib1 db 0 ; First Fibonacci number (0)
fib2 db 1 ; Second Fibonacci number (1)
fib3 db 0 ; To store the next Fibonacci number
buffer db 3, 0 ; Buffer to store a number for input

.code
main:
mov ax, @data
mov ds, ax ; Initialize the data segment

; Display prompt message


lea dx, prompt
mov ah, 09h
int 21h

; Read input for 'n' (number of Fibonacci numbers)


lea dx, buffer
mov ah, 0Ah
int 21h
lea si, buffer + 1 ; SI points to the first digit of the input

; Convert ASCII to integer


mov al, [si]
sub al, '0'
mov bl, al ; Store the value of 'n' in BL (number of Fibonacci numbers to
generate)

; Display "Fibonacci Numbers are: "


lea dx, result
mov ah, 09h
int 21h

; Display the first Fibonacci number (fib1 = 0)


mov al, fib1
call PrintNumber

; Display the second Fibonacci number (fib2 = 1)


mov al, fib2
call PrintNumber

; Loop to generate the next Fibonacci numbers, starting from the 3rd
Fibonacci number
mov cl, bl ; Store the count of Fibonacci numbers to print in CL
sub cl, 2 ; Since the first two numbers are already printed, we start
with n-2
fibonacci_loop:
; Calculate the next Fibonacci number
mov al, fib1
add al, fib2 ; fib3 = fib1 + fib2
mov fib3, al
; Display the next Fibonacci number
mov al, fib3
call PrintNumber
; Update fib1 and fib2 for next iteration
mov al, fib2
mov fib1, al ; Store fib2 in fib1
mov al, fib3
mov fib2, al ; Store fib3 in fib2
dec cl ; Decrement the count
jnz fibonacci_loop ; If cl > 0, repeat the loop
; Display a new line
lea dx, newline
mov ah, 09h
int 21h
; Exit program
mov ah, 4Ch
int 21h
;----------------------------------------------
PrintNumber proc
; Input: AL = number to print
; Output: prints the number in AL as ASCII
add al, '0' ; Convert number to ASCII
mov dl, al
mov ah, 02h
int 21h
ret
PrintNumber endp
end main

Output:

You might also like