0

I have an assignment for my school I will paste the instructions down below, I am just wondering what I would have to do for the average assembly function, all 3 other functions are completed.

Write a C/C++ program that does the following:

in C++ program declare an array of integers (size is your choice)--your program should work with any array size call an assembly function to compute the average of all elements in the array (pass the array and the size of the array) call an assembly function to compute the largest (max) of all elements in the array (pass the array and the size of the array) call an assembly function to compute the smallest (min) of all elements in the array (pass the array and the size of the array) display the average, min, and max in C++ program There should be three assembly files for the three functions (average-40 points, largest-30points, smallest-30 points)

Below I will post my C++ code as well as the largest and smallest function

C++ code is below:

#include <iostream>


using namespace std;
int main()
{
    int sz;
    cout << "How many numbers do you want the array to generate?:";
    cin >> sz;
    int randArray[sz];
    for (int i = 0; i < sz; i++)
        randArray[i] = rand() % 1000; 

    cout << "\nnumbers in array:" << endl;

    for (int i = 0; i < sz; i++)

        cout << "number " << i + 1 << "::" << randArray[i] << endl;

    return 0;
}

Assembly function for the largest is below:

.686
.model flat

.code

_max PROC; named _max because C automatically prepends an underscode, it is needed to interoperate

push ebp
mov ebp, esp; stack pointer to ebp

mov ebx, [ebp + 8]; address of first array element
mov ecx, [ebp + 12]
mov ebp, 0
mov edx, 0
mov eax, [ebx]

loopMe:
    cmp ebp, ecx
    je allDone

    cmp eax, [ebx + edx]
    jg continue

    mov eax, [ebx + edx]

continue:
    add edx, 4
    add ebp, 1
    jmp loopMe

allDone :

    pop ebp
    ret
_max ENDP

END

Assembly function for the smallest is below:


.model flat

.code

_smallest PROC

    push ebp
    mov ebp,esp ;stack pointer to ebp
    
    mov ebx,[ebp+8] ; address of first array element
    mov ecx,[ebp+12]
    mov ebp,0
    mov edx,0
    mov eax,[ebx]

loopMe:
    cmp ebp,ecx
    je allDone
    
    cmp eax,[ebx+edx]
    jl continue

    mov eax,[ebx+edx]

continue:
    add edx,4
    add ebp,1
    jmp loopMe

allDone:
    
    pop ebp
    ret
_smallest ENDP

END

The only difference between the smallest and largest function is that the smallest function has "jl"=jump less under loopMe and the largest function has "jg"=jump greater under loopMe. What would I have to change in this function to make it solve for average? I was told by the professor that I only needed to change two things for that function specifically. If anyone is able to point out what I am missing I would greatly appreciate it!

Botje
  • 26,269
  • 3
  • 31
  • 41
chrisward
  • 23
  • 5
  • 3
    Instead of comparing the values, you would add them all up and then divide by the array size. That's the average. Don't focus on the "two things", just do the computations. – BoP Nov 21 '22 at 07:17
  • 1
    Your functions modify EBX without saving/restoring it. You're doing that for EBP, but EBX is also call-preserved. Only EAX, ECX, and EDX can be used as scratch registers in the standard 32-bit calling conventions. [What are callee and caller saved registers?](https://stackoverflow.com/a/56178078). Also, you only need 4 registers for min or max: a pointer, an end-pointer (ptr+length), a max-seen, and a temporary to load into. Except you're using a memory compare and loading twice, so you could use only 3 regs if you used a `do{stuff; p++;}while(p != endp)` loop structure. – Peter Cordes Nov 21 '22 at 13:05

0 Answers0