TITLE   Fibonacci numbers (register version)    PROCFIB1.ASM
COMMENT |
        Objective: To compute Fibonacci number using registers
                   for local variables.
            Input: Requests a positive integer from the user.
           Output: Outputs the largest Fibonacci number that
|                  is less than or equal to the input number.

.MODEL SMALL
.STACK 100H
.DATA
prompt_msg   DB  'Please input a positive number (>1): ',0
output_msg1  DB  'The largest Fibonacci number less than '
             DB  'or equal to ',0
output_msg2  DB  ' is ',0

.CODE
INCLUDE io.mac

main  PROC
      .STARTUP
      PutStr  prompt_msg     ; request input number
      GetInt  DX             ; DX := input number
      nwln
      call    fibonacci
      PutStr  output_msg1    ; display Fibonacci number
      PutInt  DX
      PutStr  output_msg2
      PutInt  AX
      nwln
done:
      .EXIT
main  ENDP

;-----------------------------------------------------------
;Procedure fibonacci receives an integer in DX and computes
; the largest Fibonacci number that is less than or equal to
; the input number. The Fibonacci number is returned in AX.
;-----------------------------------------------------------
fibonacci  PROC
      push    BX
      ; AX maintains the smaller of the last two Fibonacci
      ;  numbers computed; BX maintains the larger one.
      mov     AX,1           ; initialize AX and BX to
      mov     BX,AX          ;  first two Fibonacci numbers
fib_loop:
      add     AX,BX          ; compute next Fibonacci number
      xchg    AX,BX          ; maintain the required order
      cmp     BX,DX          ; compare with input number in DX
      jle     fib_loop       ; if not greater, find next number
      ; AX contains the required Fibonacci number
      pop     BX
      ret
fibonacci  ENDP
      END     main
