An Assembly Program


Assembly languages are used when speed is essential or when an operation is needed that is not possible in a high-level language. The following Microsoft MASM assembly program generates and prints up to N, entered by a user, Fibonacci numbers, where
Fibonacci.asm
 TITLE Fibonacci Numbers              (Fibonacci.asm)

 INCLUDE Irvine32.inc

 .data
 prompt1  BYTE   "Find the first N values of the Fibonacci numbers.", 0Dh, 0Ah
          BYTE   0Dh, 0Ah, "Enter the value of N (positive integer):  ", 0
 prompt2  BYTE   "  ", 0
 n        DWORD  ?

 .code
 main PROC
     call  Clrscr                ; Clears the screen.
     mov   edx, OFFSET prompt1
     call  WriteString           ; Prints the prompt 1.
     call  ReadInt               ; Reads the N.
     mov   n, eax
     call  Crlf                  ; Prints a newline.

     mov   ecx, n                ; Computes and prints N Fibonacci numbers.
     mov   eax, 1                ; Initializes Fib(1) = 1.
                                 ; eax = 1, 1, 2, 3, 5, 8, 13, ...
     mov   ebx, 0                ; Fib(2) = Fib(1) + 0.
 
 L1: add   eax, ebx              ; Generates the next Fibonacci number.
     call  WriteDec              ; Prints the number. 
     mov   edx, OFFSET prompt2   
     call  WriteString           ; Prints spaces.
            ; Sets up the sequence.
     loop  L1

     call  Crlf
     call  Crlf
     exit
 main ENDP
 END main