Assmbly Language The objectives of this program is to understand dynamic arrays,
ID: 3801338 • Letter: A
Question
Assmbly Language
The objectives of this program is to understand dynamic arrays, stacks and subprogram calling another subprogram (function calls that are more than one level deep).
create_array will have no arguments IN and two arguments OUT:
$sp+0 - array base address (OUT)
$sp+4 - array length (OUT)
create_array will call subprogram allocate_array to dynamically allocate array. Then it will call read_array to fill the array. Lastly, create_array will return base address and array size back to the main.
allocate_array will have no arguments IN and two arguments OUT:
$sp+0 - array base address (OUT)
$sp+4 - array length (OUT)
allocate_array will ask the user for the size of the array and will validate that it is between 5 (exclusive) and 10 (inclusive) elements or (5, 10]. If an invalid length is entered, allocate_array will print an error message and reprompt the user for the length. When a valid length is entered, allocate_array will dynamically allocate exactly enough memory to hold the array, and then return base address and array size back to create_array subprogram.
read_array will have two arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
read_array will read values from the user and store them in the array until the array is full. A prompt must be printed before each entry is read (e.g. “Enter an integer: “).
print_array will have two arguments IN and no arguments OUT:
$sp+0 - array length (IN)
$sp+4 - array base address (IN)
print_array will print all values from the array with each value separated by a space.
print_every_nth will have three arguments IN and no arguments OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
$sp+8 - element 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 must print an error and return without printing anything.
sum_odd_values will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
$sp+8 - sum of odd values (OUT)
sum_odd_values will calculate the sum of all odd values in the array and return that sum. For example: given the array [8 4 3 6 5 5 2 8 7 2], the sum would be 30. sum_odd_values MAY NOT print anything (main must print the sum).
reverse_array will have two arguments IN and one argument OUT:
$sp+0 - array base address (IN)
$sp+4 - array length (IN)
$sp+8 - array base address of reversed array (OUT)
reverse_array will dynamically allocate an array with the same size as original array. Then copies values from original array into reversed array. Lastly, it will return array base address of reversed array back to main. Remember the size of reverse array is the same as size of original array so there is no need to return it back to main.
pseudocode:
revered array [0] = original array [size -1]
revered array [1] = original array [size -2]
…
revered array [size - 1] = original array [0]
Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Reversed array: [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
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.