Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Write and test a MASM program to perform the following tasks: 1. Di

ID: 3787123 • Letter: D

Question

Description:
Write and test a MASM program to perform the following tasks:
1. Display the program title and programmer’s name.
2. Get the user’s name, and greet the user.
3. Display instructions for the user.
4. Repeatedly prompt the user to enter a number. Validate the user input to be in [-100, -1] (inclusive). Count and accumulate the valid user numbers until a non-negative number is entered. (The non-negative number is discarded.)
5. Calculate the (rounded integer) average of the negative numbers.
6. Display:
i. the number of negative numbers entered (Note: if no negative numbers were entered, display a special message and skip to iv.)
ii. the sum of negative numbers entered
iii. the average, rounded to the nearest integer (e.g. -20.5 rounds to -20)
iv. a parting message (with the user’s name)
Requirements:
1. The main procedure must be modularized into commented logical sections (procedures are not required this time)
2. The program must be fully documented. This includes a complete header block for identification, description, etc., and a comment outline to explain each section of code.
3. The lower limit should be defined as a constant.
4. The usual requirements regarding documentation, readability, user-friendliness, etc., apply.
5. Submit your text code file (.asm) to Canvas by the due date.
Notes:
1. There are no new concepts in this programming assignment. It is given for extra practice, to keep MASM fresh in your mind while we study internal/external data representation.
2. This is an integer program. Even though it would make more sense to use floating-point computations, you are required to do this one with integers.

Example (user input in italics):
Welcome to the Integer Accumulator by Austin Miller
What is your name? Caleb
Hello, Caleb
Please enter numbers in [-100, -1].
Enter a non-negative number when you are finished to see results.
Enter number: -15
Enter number: -100
Enter number: -36
Enter number: -10
Enter number: 0
You entered 4 valid numbers.
The sum of your valid numbers is -161
The rounded average is -40
Thank you for playing Integer Accumulator! It's been a pleasure to meet you, Caleb.
Extra-credit options (original definition must be fulfilled):
1. Number the lines during user input.
2. Calculate and display the average as a floating-point number, rounded to the nearest .001.
3. Do something astoundingly creative.
To ensure you receive credit for any extra credit options you did, you must add one print statement to your program output PER EXTRA CREDIT which describes the extra credit you chose to work on. You will not receive extra credit points unless you do this. The statement must be formatted as follows...

Explanation / Answer

TITLE Programming Accumulator


; Description:
;1. Display the program title and programmer’s name.
;2. Get the user’s name, and greet the user.
;3. Display instructions for the user.
;4. Repeatedly prompt the user to enter a number. Validate the user input to be in [-100, -1] (inclusive).
;       Count and accumulate the valid user numbers until a non-negative number is entered. (The non-
;       negative number is discarded.)
;5. Calculate the (rounded integer) average of the negative numbers. 6. Display:
;   i. the number of negative numbers entered (Note: if no negative numbers were entered, display a special message and skip to iv.)
;   ii. the sum of negative numbers entered
;   iii. the average, rounded to the nearest integer (e.g. -20.5 rounds to -20)
;   iv. a parting message (with the user’s name)

INCLUDE Irvine32.inc

.data

welcome                           BYTE   "Welcome to the amazing Integer Accumulator", 0
instructions_1               BYTE   "Please enter numbers between [-100, -1].", 0
instructions_2               BYTE   "Then, enter a non-negative number to see the amazing accumulator in action!", 0
instructions_3               BYTE   " Enter a number: ", 0
userNameInstructions   BYTE   "What's your name, friend?", 0
greeting                       BYTE   "Hi, ", 0
goodbye                           BYTE   "Ta ta for now, ", 0
number                           DWORD ?
userName                       BYTE   21 DUP(0)
userNameByteCount           DWORD   ?
count                             DWORD   1
accumulator                     DWORD   0
totalIs                           BYTE   "The total is:                  ", 0
quantNumbersEntered    BYTE   "Amount of numbers accumulated: ", 0
roundedAve_prompt           BYTE   "The Rounded Average is:        ", 0
roundedAve                     DWORD 0
remainder                       DWORD   ?
floating_point_point   BYTE   ".",0
floating_point_prompt   BYTE   "As a floating point number:    ", 0
neg1k                             DWORD -1000
onek                             DWORD   1000
subtractor                     DWORD   ?
floating_point               DWORD   ?


;ec promp
ec_prompt_1                     BYTE   "EC: Display as floating point value.", 0
ec_prompt_2                     BYTE   "EC: Lines are numbered during user input.", 0

;constants
LOWERLIMIT       =       -100
UPPERLIMIT       =       -1

;change text color, because white text is a little boring after a while
val1 DWORD 11
val2 DWORD 16


.code
main PROC
   ; Set text color to teal
       mov eax, val2
       imul eax, 16
       add eax, val1
       call setTextColor

   ; Programmer name and title of assignment
   call   CrLf
   mov       edx, OFFSET welcome
   call   WriteString
   call   CrLf

   ;ec prompts
   mov       edx, OFFSET ec_prompt_1
   call   WriteString
   call   CrLf
   mov       edx, OFFSET ec_prompt_2
   call   WriteString
   call   CrLf

   ; get user name
   mov       edx, OFFSET userNameInstructions
   call   WriteString
   call   CrLf
   mov       edx, OFFSET userName
   mov       ecx, SIZEOF userName
   call   ReadString
   mov       userNameByteCount, eax

   ;test username
   mov       edx, OFFSET greeting
   call   WriteString
   mov       edx, OFFSET userName
   call   WriteString
   call   CrLF

   ; assignment instructions
   mov       edx, OFFSET instructions_1
   call   WriteString
   call   CrLf
   mov       edx, OFFSET instructions_2
   call   WriteString
   call   CrLf
   mov       ecx, 0


   ; loop to allow user to continue entering negative numbers
   userNumbers:   ;read user number
           mov       eax, count
           call   WriteDec
           add       eax, 1
           mov       count, eax
           mov      edx, OFFSET instructions_3
           call   WriteString
           call ReadInt
           mov   number, eax
           cmp       eax,LOWERLIMIT
           jb       accumulate;
           cmp       eax, UPPERLIMIT
           jg       accumulate
           add       eax, accumulator
           mov       accumulator, eax
           loop   userNumbers


   ; do the accumulation
   accumulate:
           ; test if they entered any valid numbers, if they didnt, jump to the sayGoodbye
           mov       eax, count
           sub       eax, 2
           jz       sayGoodbye
           mov       eax, accumulator
           call   CrLF

           ; accumulated total
           mov       edx, OFFSET totalIs
           call   WriteString
           mov       eax, accumulator
           call   WriteInt
           call   CrLF

           ; total numbers accumulated
           mov       edx, OFFSET quantNumbersEntered
           call   WriteString
           mov       eax, count
           sub       eax, 2
           call   WriteDec
           call   CrLf

           ; integer rounded average
           mov       edx, OFFSET roundedAve_prompt
           call   WriteString
           mov       eax, 0
           mov       eax, accumulator
           cdq
           mov       ebx, count
           sub       ebx, 2
           idiv   ebx
           mov       roundedAve, eax
           call   WriteInt
           call   CrLf

           ; integer average for accumulator
           mov       remainder, edx
           mov       edx, OFFSET floating_point_prompt
           call   WriteString
           call   WriteInt
           mov       edx, OFFSET floating_point_point
           call   WriteString


           ; fancy stuff for floating point creation
           mov       eax, remainder
           mul       neg1k
           mov       remainder, eax ; eax now holds remainder * -1000
           mov       eax, count
           sub       eax, 2           ; ebx now holds something?
           mul       onek
           mov       subtractor, eax

           ; fancy stack stuff for floating point creation
           fld       remainder
           fdiv   subtractor
           fimul   onek
           frndint
           fist   floating_point
           mov       eax, floating_point
           call   WriteDec
           call   CrLf


   ; say goodbye
   sayGoodbye:
           call   CrLf
           mov       edx, OFFSET goodbye
           call   WriteString
           mov       edx, OFFSET userName
           call   WriteString
           mov       edx, OFFSET floating_point_point
           call   WriteString
           call   CrLf
           call   CrLf

exit   ; exit to operating system
main ENDP

END main