This program prompts the user to input the number of integers (N) to read and the size of each integer in bytes. It then reads N integers as strings, converts them to the specified size in BCD format, calculates the sum and average, and prints the results. Validation is included to ensure decimal numbers of the correct size are input.
can you rewrite this code so that the teacher won't guess it is a copy (1).pdf
1. can you rewrite this code so that the teacher won't guess it is a copy ?
.MODEL SMALL
.STACK 100h
.DATA
N DW 0 ; number of integers to read
SIZE DB 0 ; size of each integer in bytes
SUM DD 0 ; sum of integers
AVG DD 0 ; average of integers
INPUT DB 20 ; input buffer for reading numbers as strings
.CODE
MAIN PROC
; Prompt the user to input N and the size of the integers
MOV AH, 9
LEA DX, PROMPT_N
INT 21h
CALL READ_DEC
MOV N, AX
MOV AH, 9
LEA DX, PROMPT_SIZE
INT 21h
CALL READ_DEC
MOV SIZE, AL
; Allocate memory for the integer array
2. MOV AH, 48h
MOV BX, N
MUL SIZE
MOV AH, 48h
INT 21h
MOV DI, AX
MOV BX, DI
; Loop to read N integers
MOV CX, N
MOV SI, 0
READ_LOOP:
; Prompt the user to input the next integer
MOV AH, 9
LEA DX, PROMPT_INT
INT 21h
; Read the input as a string
MOV AH, 0Ah
LEA DX, INPUT
INT 21h
; Convert the string to an integer in BCD format with SIZE bytes
CALL STR_TO_INT
MOV WORD PTR [DI], AX
ADD DI, SIZE
3. LOOP READ_LOOP
; Calculate the sum and average of the integers
MOV AX, 0
MOV DX, 0
MOV CX, N
MOV SI, BX
ADD_LOOP:
ADD SI, SIZE
MOV BX, WORD PTR [SI]
ADD AX, BX
ADC DX, 0
LOOP ADD_LOOP
MOV SUM, DX
MOV AVG, DX
SHR DX, 1
DIV CX
; Print the sum and average of the integers
MOV AH, 9
LEA DX, RESULT_SUM
INT 21h
CALL WRITE_DEC
MOV AH, 9
LEA DX, RESULT_AVG
4. INT 21h
CALL WRITE_DEC
; Exit the program
MOV AH, 4Ch
INT 21h
MAIN ENDP
; Read a decimal number from the keyboard and return it in AX
READ_DEC PROC
XOR AX, AX
MOV CX, 10
READ_DEC_LOOP:
; Read a character from the keyboard
MOV AH, 1
INT 21h
; Check if it's a decimal digit
CMP AL, '0'
JB READ_DEC_DONE
CMP AL, '9'
JA READ_DEC_DONE
; Multiply AX by 10 and add the new digit
SUB AL, '0'
MUL CX
ADD AX, AX
5. ADD AX, AX
ADD AX, AL
JMP READ_DEC_LOOP
READ_DEC_DONE:
RET
READ_DEC ENDP
; Convert the string in INPUT to an integer in BCD format with SIZE bytes and return it in AX
STR_TO_INT PROC
XOR AX, AX
XOR CX, CX
MOV SI, OFFSET INPUT+2
MOV DI, SI
ADD SI, SIZE-1
STR
; Check if the string is a decimal number
MOV CL, SIZE
DEC CL
CMP BYTE PTR [DI], '-'
JNE STR_SKIP_SIGN
NEG CL
DEC SI
STR_SKIP_SIGN:
MOV CH, 10
6. STR_CHECK_DIGIT_LOOP:
MOV AL, BYTE PTR [SI]
CMP AL, '0'
JB STR_NOT_DECIMAL
CMP AL, '9'
JA STR_NOT_DECIMAL
DEC SI
LOOP STR_CHECK_DIGIT_LOOP
; Convert the string to an integer in BCD format with SIZE bytes
MOV SI, OFFSET INPUT+2
MOV DI, AX
ADD DI, SIZE-1
STR_CONVERT_LOOP:
MOV AL, BYTE PTR [SI]
SUB AL, '0'
MOV BL, AL
MOV AL, CH
MUL BL
ADD AX, CX
MOV BL, AH
MOV AH, 0
MOV CL, 4
SHR BL, CL
7. ADD DI, 1
MOV BYTE PTR [DI], BL
MOV AL, AH
MOV BL, CH
MUL BL
ADD AX, CX
MOV BL, AL
MOV AH, 0
MOV CL, 4
SHR BL, CL
ADD DI, 1
MOV BYTE PTR [DI], BL
ADD SI, 1
CMP SI, OFFSET INPUT+2+20
JBE STR_CONVERT_LOOP
RET
STR_TO_INT ENDP
; Write a decimal number in AX to the console
WRITE_DEC PROC
; Convert the number to a string
XOR CX, CX
MOV BX, 10
WRITE_DEC_LOOP:
8. XOR DX, DX
DIV BX
PUSH DX
INC CX
OR AX, AX
JNZ WRITE_DEC_LOOP
; Write the string to the console
WRITE_DEC_LOOP2:
POP DX
ADD DL, '0'
MOV AH, 2
INT 21h
LOOP WRITE_DEC_LOOP2
RET
WRITE_DEC ENDP
; Data and messages
PROMPT_N DB 'Enter the number of integers to read: $'
PROMPT_SIZE DB 'Enter the size of each integer in bytes: $'
PROMPT_INT DB 'Enter the next integer: $'
RESULT_SUM DB 0Dh, 0Ah, 'The sum is: $'
RESULT_AVG DB 0Dh, 0Ah, 'The average is: $'
END MAIN
this is what the code should do
9. Write an Intel 8086 Assembly program that reads N numbers as Strings, convert them into
variable sized Integer numbers, and then print the summation and average of the numbers. The
program should allow the user to decide the size of the input number itself (assume integers in
format but with variable size). Detailed Description: - Have the program prompt the user to input
N and the size of the number then request inputting the first number, then the second and so on
until N numbers are input. - Your code should allow users to select the size of the numbers, for
example you can have integers with size of 1 Byte each, or you can make them 10 Bytes large. -
Validation: Your code should make sure user inputs Decimal numbers only, and with
predetermined size only. When a user inputs a wrong value, your code should print an error
message that explains it, and then gives the user another chance to input it correctly. - When the
user presses Enter, your code should read the input values as if they are Strings, then convert
them into numbers in BCD format with predefined size, each two digits can be stored in one
Byte. - After reading all the numbers, calculate their summation and average then print the Sum
and Avg