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

Objectives (using MASM/Assembly Language): 1) Designing, implementing, and calli

ID: 3808251 • Letter: O

Question

Objectives (using MASM/Assembly Language):
1) Designing, implementing, and calling low-level I/O procedures
2) Implementing and using a macro


Problem Definition:
• Implement and test your own ReadVal and WriteVal procedures for unsigned integers.
• Implement macros getString and displayString. The macros may use Irvine’s ReadString to get input from the user, and WriteString to display output.
o getString should display a prompt, then get the user’s keyboard input into a memory location
o displayString should the string stored in a specified memory location.
o readVal should invoke the getString macro to get the user’s string of digits. It should then convert the digit string to numeric, while validating the user’s input.
o writeVal should convert a numeric value to a string of digits, and invoke the displayString macro to produce the output.
• Write a small test program that gets 10 valid integers from the user and stores the numeric values in an array. The program then displays the integers, their sum, and their average.

Requirements:
1) User’s numeric input must be validated the hard way: Read the user's input as a string, and convert the string to numeric form. If the user enters non-digits or the number is too large for 32-bit registers, an error message should be displayed and the number should be discarded.
2) Conversion routines must appropriately use the lodsb and/or stosb operators.
3) All procedure parameters must be passed on the system stack.
4) Addresses of prompts, identifying strings, and other memory locations should be passed by address to the macros.
5) Used registers must be saved and restored by the called procedures and macros.
6) The stack must be “cleaned up” by the called procedure.
7) The usual requirements regarding documentation, readability, user-friendliness, etc., apply.
8) Submit your text code file (.asm) to Canvas by the due date.

Notes:
1) For this assignment you are allowed to assume that the total sum of the numbers will fit inside a 32 bit register.
2) When displaying the average, you may round down to the nearest integer. For example if the sum of the 10 numbers is 3568 you may display the average as 356.

Example
PROGRAMMING ASSIGNMENT 6: Designing low-level I/O procedures
Written by: Sheperd Cooper
Please provide 10 unsigned decimal integers.
Each number needs to be small enough to fit inside a 32 bit register.
After you have finished inputting the raw numbers I will display a list of the integers, their sum, and their average value.
Please enter an unsigned number: 156
Please enter an unsigned number: 51d6fd
ERROR: You did not enter an unsigned number or your number was too big.
Please try again: 34
Please enter an unsigned number: 186
Please enter an unsigned number: 15616148561615630
ERROR: You did not enter an unsigned number or your number was too big.
Please try again: -145
ERROR: You did not enter an unsigned number or your number was too big.
Please try again: 345
Please enter an unsigned number: 5
Please enter an unsigned number: 23
Please enter an unsigned number: 51
Please enter an unsigned number: 0
Please enter an unsigned number: 56
Please enter an unsigned number: 11
You entered the following numbers:
156, 34, 186, 345, 5, 23, 51, 0, 56, 11
The sum of these numbers is: 867
The average is: 86
Thanks for playing!

Extra Credit:
1) 1 point: number each line of user input and display a running subtotal of the user’s numbers.
2) 2 points: Handle signed integers.
3) 3 points: make your ReadVal and WriteVal procedures recursive.
4) 4 points: implement procedures ReadVal and WriteVal for floating point values, using the FPU.
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...
--Program Intro--
**EC: DESCRIPTION
--Program prompts, etc—

Explanation / Answer

TITLE Sorting Arrays    (sandetra_6A.asm)


; Description: Implements and tests ReadVal and WriteVal procedures
;   for unsigned integers using getString and displayString macros.
;
; EXTRA CREDIT: Numbers each line of user input / displays running subtotal of users numbers (+25)
;       ReadVal and WriteVal procedures are recursive (+50)

INCLUDE Irvine32.inc

NO_LOOPS = 15   ;numer of inputs to loop
STRSIZE = 20   ;max size of input strings

;-----------------------------------------------------
getString MACRO buffer, prompt_string
;
; Displays a variable, using its known attributes.
; Receives: 1: A string to store the string in.
;   2: Address of a prompt
;-----------------------------------------------------
   push   edx
   push   ecx
   displayString OFFSET prompt_string
   mov       edx, buffer
   mov       ecx, STRSIZE - 1
   call   ReadString
   pop       ecx
   pop       edx
ENDM

;-----------------------------------------------------
displayString MACRO buffer
;
; Displays a string.
; Receives: address of a string.
;-----------------------------------------------------
   push   edx
   mov       edx, buffer
   call   WriteString
   pop       edx
ENDM

.data
dwordArray   DWORD   NO_LOOPS DUP(?)
loopcount   DWORD   0               ;loop counter
runningtot   DWORD   0               ;running total accumulator
intro1       BYTE   "PROGRAMMING ASSIGNMENT 6: Designing low-level I/O procedures", 0
intro2       BYTE   "Written by:chegg ", 0
intro3       BYTE   "Please provide 15 unsigned decimal integers.",0
intro4       BYTE   "After you have finished inputting the raw numbers I will display",0
intro5       BYTE   "a list of the integers, their sum, and their average value. ",0
info1       BYTE   "The largest possible unsigned 32 bit int is: ", 0
info2       BYTE   "So each int and the sum of all 15 must be less than that.", 0
prompt1       BYTE   "Please enter an unsigned int: ", 0
prompt2       BYTE   "Decimal value accepted as: ", 0
prompt3       BYTE   "String value read as: ", 0
valuest       BYTE   " values entered. Running total: ", 0
error1       BYTE   "< ERROR! : Invalid character >", 0
error2       BYTE   "< ERROR! : INT32 OVERFLOW! PLS USE SMALLER NUMBERS >", 0
error3       BYTE   "< ERROR! : TOTAL OVERFLOWED. PLS USE SMALLER NUMBERS >", 0
results1   BYTE   "You entered the following numbers: ", 0
results2   BYTE   "The sum of these numbers is: ", 0
results3   BYTE   "The average is: ", 0
goodBye       BYTE   "Good-bye! Thanks for a very substantial class!", 0
spacer       BYTE   ", ",0

.code
;-----------------------------------------------------
main PROC
;-----------------------------------------------------
   displayString OFFSET intro1
   call   crlf
   displayString OFFSET intro2
   call   crlf
   call   crlf
   displayString OFFSET intro3
   call   crlf
   displayString OFFSET intro4
   call   crlf
   displayString OFFSET intro5
   call   crlf
   call   crlf
   displayString OFFSET info1
   push   0FFFFFFFFh               ;largest possible 32bit int (also for testing)
   call   WriteVal
   call   crlf
   displayString OFFSET info2
   call   crlf
   call   crlf
   mov       ecx, NO_LOOPS           ;global variable
   mov       edi, OFFSET dwordArray   ;array[0]
mainloop:
   mov       eax, loopcount
   call   WriteDec
   displayString OFFSET valuest
   mov       eax, runningtot
   call   WriteDec
   call   crlf
   push   edi                       ;push the array
   call   ReadVal
   call   crlf
   inc       loopcount
   mov       eax, [edi]
   add       edi, 4                   ;incriment array
   mov       ebx, runningtot
   add       eax, ebx
   jc       overflow
   mov       runningtot, eax
   loop   mainloop
   jmp       skipend
overflow:
   displayString OFFSET error3
   call   crlf
skipend:
   push   NO_LOOPS               ;size of array
   push   OFFSET dwordArray
   call   Results
   call   crlf
   displayString OFFSET goodBye
   call   crlf
   exit   ; exit to operating system
main ENDP

;-----------------------------------------------------
ReadVal PROC
; Converts a string of user input to an integer.
; Receives: address of a dword integer
; Returns: user-specified integer in address
;-----------------------------------------------------
   push   ebp
   mov       ebp, esp
   jmp       skip1
errormess:
   displayString OFFSET error1
   call   CrLf
   jmp       skip1
errormess2:
   displayString OFFSET error2
   pop       eax
   call   CrLf
skip1:
   mov       esi, [ebp+8]
   getString esi, prompt1
   mov       eax, 0
   push   eax
reads:
   mov       eax, 0
   LODSB
   cmp       al, 0
   je       endread       ;NULL terminate
   pop       ebx           ;restore calculated value
   push   eax           ;save ascii char
   mov       eax, ebx
   mov       ebx, 10
   mul       ebx           ;multiply result by 10
   jc       errormess2
   mov       edx, eax   ;store result in edx
   pop       eax           ;reload ascii char
   cmp       al, 48       ;cmp to ascii 0
   jl       errormess
   cmp       al, 57       ;cmp to ascii 9
   jg       errormess
   mov       ah, 48
   sub       al, ah       ;convert ascii to dec
   mov       ah, 0
   add       eax, edx
   jc       errormess2
   push   eax           ;save calculated value
   jmp       reads
endread:
   pop       eax
   mov       esi, [ebp+8]
   mov       [esi], eax
   ;displayString OFFSET prompt2   ;debug
   ;call   WriteDec
   ;call   CrLf
   pop       ebp
   ret 4
ReadVal ENDP

;-----------------------------------------------------
WriteVal PROC
; Converts a numeric value to a string of digits and displays output.
; Receives: address of a dword unsigned integer
; Returns: console output
;-----------------------------------------------------
   push   ebp
   mov       ebp, esp
   pushad
   sub       esp, 2           ;make space for the character string
;-----header------
   mov       eax, [ebp+8]
   lea       edi, [ebp-2]   ;LEA to access the local address
   mov       ebx, 10
   mov       edx, 0
   div       ebx               ;divide input by 10
   cmp       eax, 0
   jle       endwrite       ;end recursion when eax = 0
   push   eax
   call   WriteVal
endwrite:
   mov       eax, edx
   add       eax, 48           ;convert to ascii
   stosb                   ;store in edi
   mov       eax, 0           ;null terminator
   stosb
   sub       edi, 2           ;reset edi
   displayString edi
;-----footer------
   add       esp, 2
   popad
   pop       ebp
   ret       4
WriteVal ENDP

;-----------------------------------------------------
Results PROC
; Calculates the sum and mean of ints in an array.
; Receives: address of an array, size of the array
; Returns: console output of the sum and mean
;-----------------------------------------------------
   push   ebp
   mov       ebp, esp
   mov       esi, [ebp+8]
   mov       ecx, [ebp+12]
   sub       esp, 4
   mov       edx, 0           ;use eax to calculate the sum
   displayString OFFSET results1
   call   crlf
   jmp       s1
resloop:
   displayString OFFSET spacer
s1:
   push   [esi]
   call   WriteVal
   mov       ebx, [esi]
   add       edx, ebx
   add       esi, 4
   loop   resloop
   call   crlf
   displayString OFFSET results2
   push   edx               ;display the sum
   call   WriteVal
   call   crlf
   displayString OFFSET results3
   mov       eax, edx
   mov       edx, 0
   mov       ebx, [ebp+12]
   div       ebx
   push   eax
   call   WriteVal
   call   crlf
   add       esp, 4
   pop       ebp
   ret       8
Results ENDP

END main