Starting with the following C++ program: #include <iostream> using namespace std
ID: 3734085 • Letter: S
Question
Starting with the following C++ program:
#include <iostream>
using namespace std;
extern “C” long Average (long, long [ ]);
void main ()
{
long Array1 [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
long Array2 [11] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
cout << "Average of Array1 is " << Average (10, Array1) << endl;
cout << "Average of Array2 is " << Average (11, Array2) << endl;
}
Write in assembly language (x86) (in a separate file named Lab8.asm) the function Average.The first parameter is the number of elements in an array, the second is
the address of the array.The function will determine the average of the values in the array and return the average rounded to the nearest whole number (if the
fractional part of the result is equal to or greater than .5, the result is rounded to the next higher number.
Explanation / Answer
AVERAGE:
push ebp ; save ebp
mov ebp, esp ; point ebp to stack pointer
sub esp, 4 ; allocate space for local variable
mov eax, [ebp+4] ; load number of elements into eax
mov ebx, [ebp+12] ; load base address of array into ebx, each takes 8 bytes since long variables
mov edx, 0 ; to store sum
LOOP:
cmp ebx, 0
je EXIT
add edx, ebx ; add current element to edx
add ebx, 8 ; increment offset by 8 bytes in each iteration
jmp LOOP
EXIT:
div edx, eax ; now edx will have average
srp edx, 6, 5 ; round off around 5
mov esp, ebp
pop ebp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.