MP 11
MP 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
; 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: