Description: Write a MASM program to calculate Fibonacci numbers: 1. Display the program title and your name. Then get the user’s name and greet the user. 2. Prompt the user to enter the number of Fibonacci terms to be displayed. Advice the user to enter an integer in the range [1 – 46]. 3. Get and validate the user input. 4. Calculate and display all of the Fibonacci numbers up to and including the nth term. The results should be displayed 5 terms per line with at least 5 spaces between terms. 5. Display a goodbye message that includes the user’s name and terminates the program.

Respuesta :

Answer:

.data

programmerName               BYTE   "PROGRAMMER NAME", 0

programTitle       BYTE   "PRINT FIBONACCI NUMBERS WITHIN RANGE", 0

prompt_1           BYTE   "WHAT IS YOUR NAME? ", 0

prompt_2           BYTE   "Enter the number of Fibonacci terms you want to display between [1 - 46]", 0

num_fibo               DWORD   ?

previous1               DWORD   ?

previous2               DWORD   ?

spaces               BYTE   " ",0

goodbye               BYTE   "Goodbye, ", 0

first2           BYTE   "1 1 ", 0

first1           BYTE   "1", 0

temp               DWORD   ?

moduloFive           DWORD   5

UPPERLIMIT = 46

LOWERLIMIT = 1

;user's name

buffer               BYTE 21 DUP(0)

byteCount           DWORD   ?

;greet the user

hi                   BYTE   "Hi, ",0

;validate

highError           BYTE   "The number you entered is too high! Please enter number below 46", 0

lowError           BYTE   "The number you entered is too low! Please eneter number above 1", 0

;EC -> Setting Background Color and Text Color

val1 DWORD 11

val2 DWORD 16

.code

main PROC

  ; setting text color to teal

      mov eax, val2

      imul eax, 16

      add eax, val1

      call setTextColor

  ; introduction

      mov       edx, OFFSET programTitle

      call   WriteString

      mov       edx, OFFSET programmerName

      call   WriteString

      call   CrLf

      ; EC Prompt

      mov       edx, OFFSET ec_prompt

      call   WriteString

      call   CrLf

      mov       edx, OFFSET prompt_1

      call   WriteString

      call   CrLf

      ; get user's name

      mov       edx, OFFSET buffer   ;point to the buffer

      mov       ecx, SIZEOF   buffer   ; specify max characters

      call   ReadString

      mov       byteCount, eax

      ; greet the user

      mov       edx, OFFSET hi

      call   WriteString

      mov       edx, OFFSET buffer

      call   WriteString

      call   CrLf

      ;userInstructions

topPrompt:

          mov       edx, OFFSET prompt_2

          call   WriteString

          call   CrLf

  ;getUserData

      call   ReadInt

      mov       num_fibo, eax

  ; Validate user data

      cmp       eax, UPPERLIMIT

      jg       TooHigh

      cmp       eax, LOWERLIMIT

      jl       TooLow

      je       JustOne

      cmp       eax, 2

      je       JustTwo

  ; displayFibs

      mov       ecx, num_fibo

      sub       ecx, 3           ; we start at iteration 3, the first two are taken care of by JustOne and JustTwo

      mov       eax, 1

      call   WriteDec

      mov       edx, OFFSET spaces

      call   WriteString

      call   WriteDec

      mov       edx, OFFSET spaces

      call   WriteString

      mov       previous2, eax

      mov       eax, 2

      call   WriteDec

      mov       edx, OFFSET spaces

      call   WriteString

      mov       previous1, eax

      fib:

          add       eax, previous2

          call   WriteDec

          mov       edx, OFFSET spaces

          call   WriteString

          mov       temp, eax

          mov       eax, previous1

          mov       previous2, eax

          mov       eax, temp

          mov       previous1, eax

          mov       edx, ecx

          cdq

          div       moduloFive

          cmp       edx, 0

          jne       skip

          call   CrLf

      skip:

              mov       eax, temp

              loop   fib

              jmp       TheEnd

TooHigh:

          mov       edx, OFFSET highError

          call   WriteString

          jmp       TopPrompt

TooLow:

          mov       edx, OFFSET lowError

          call   WriteString

          jmp       TopPrompt

JustOne:

          mov       edx, OFFSET first1

          call   WriteString

          jmp       TheEnd

JustTwo:

          mov       edx, OFFSET first2

          call   WriteString

          jmp       TheEnd

;farewell

TheEnd:

          call   CrLf

          mov       edx, OFFSET goodbye

          call   WriteString

          mov       edx, OFFSET buffer

          call   WriteString

          call   CrLf

  exit   ; exit to operating system

main ENDP

END main