Hi can i get help writing this program in assembly language The purpose of this
ID: 3604114 • Letter: H
Question
Hi can i get help writing this program in assembly language
The purpose of this program is to help you understand dynamic array, system stack and multi-level subprogram calls.
This program will first prompt user to input a valid array size (between 4 inclusive to 11 exclusive) and then dynamically allocate an array in memory based on the size. It will then ask user to input integers one by one to fill out the allocated array until it’s full. After input, the program will print out the original array and then do three operations on the array:
calculate the summation of all even numbers in the array and print out the result;
dynamically allocate a new array with the same size, reversely copy all elements from the original array in to the new array, and then print the new array to the console;
Prompt user for a stride n and print out every nth element in the new reversed array.
To achieve this goal, you will need 1 main program associate with 7 subprograms:
create_array
This subprogram will have no argument IN and two arguments OUT:
$sp+0 - array base address (OUT)
$sp+4 - array size (OUT)
create_array will prompt user for a size of the array and will validate the input (between 4 inclusive to 11 exclusive). If an invalid size is entered, it will print an error message and re-prompt the user for a valid one. When a valid size is entered, create_array will call allocate_array to dynamically allocate an array. And then it will call read_array to fill it out. It will return base address and array size to the calling location at the end of this subprogram.
allocate_array
This subprogram will have one argument IN and one arguments OUT:
$sp+0 - user input size (IN)
$sp+4 - array base address (OUT)
allocate_array will dynamically allocate enough memory to hold the array based on the input size, and then return base address of the array back to the calling location.
read_array
This subprogram will have two arguments IN and no argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
read_array will read integer values from user and store them into the array until the array is full. A prompt must be printed before each entry is read (e.g. “Enter an integer: “).
print_array
This subprogram will have two arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
print_array will print all values from the array with each value separated by a space.
print_every_nth
This subprogram will have three arguments IN and no argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - stride N (IN)
print_every_nth will print every nth value from the array, starting from the first value (index 0). For example, consider the array [1 2 3 4 5 6 7 8 9 0].
For N = 3, the output would be [1 4 7 0].
For N = 4, the output would be [1 5 9].
If N 0, the subprogram will print an error message instead.
sum_even_values
This subprogram will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - sum of even values (OUT)
sum_even_values will calculate the sum of all even values in the array and return that sum to calling location. For example: given the array [8 4 3 6 5 5 2 8 7 2], the sum would be (8+4+6+2+8+2 = 30). sum_even_values will NOT print anything (main should print the sum).
reverse_array
This subprogram will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array size (IN)
$sp+8 - array base address of reversed array (OUT)
reverse_array will call allocate_array to dynamically allocate a new array with the same size as the original one, and then reversely copy allvalues from the original array into the new array. This subprogram will return the base address of reversed array back to the calling location. Remember the size of reverse array is the same as size of original array so there is no need to return it again.
pseudocode:
revered array [0] = original array [size -1]
revered array [1] = original array [size -2]
…
revered array [size - 1] = original array [0]
Original Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Reversed array: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
The structure of the program should as follow:
Main:
Create_array
Allocate_array
Read-array
Print_array
Sum_even_values
Reverse_array
Allocate_array
Print_every_nth
Remember that main should also print the returned sum of even values after sum_even_values subprogram. You should store array base address, reversed array base address, array length into static variables.
Explanation / Answer
.DATA
numArray DWORD ?
numElts DWORD 100
num DWORD ?
exitNum DWORD -9999
prompt BYTE "Enter a number", 0
string BYTE 40 DUP (?)
resultLbl BYTE "Results", 0
sum BYTE 11 DUP(?), " is the sum.", 0dh, 0ah
;numEntered BYTE 11 DUP(?), " numbers were entered."
avg BYTE 11 DUP(?), " is the average."
count BYTE 11 DUP(?), " is the number of entries that are >= the average."
.CODE
_MainProc PROC
mov eax, 0 ; sum := 0
lea ebx, numArray ; get address of nbrArray
LOOP1: input prompt, string, 40 ; read ASCII characters
atod string ; convert to integer
mov num, eax ; store in memory
mov ecx, numElts ; count := nbrElts
cmp exitNum, eax
je QUIT ; quit if -9999
add eax, [ebx] ; add number to sum
add ebx, 4 ; get address of next array elt
add ecx, 1 ; add one for count
loop LOOP1 ; repeat nbrElts times
cdq ; extend sum to quadword
idiv numElts ; calculate average
dtoa avg, ebx ; convert to ASCII characters
dtoa count, ecx
dtoa sum, eax
QUIT:
output resultLbl, sum, avg, count
ret
_MainProc ENDP
END ; end of source code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.