Assembly 浮点数集合的平均值

Assembly 浮点数集合的平均值,assembly,x86,Assembly,X86,对于如何获得两个不同浮点数组的平均值,现在真的很困惑。到目前为止,我知道如何做一个数组,但对于第二个数组,我没有任何线索。我认为这很简单,但第二个数组要复杂一些 以下是cpp: #include <iostream> using namespace std; extern "C" double Average (int, double []); void main () { double Array1 [10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6

对于如何获得两个不同浮点数组的平均值,现在真的很困惑。到目前为止,我知道如何做一个数组,但对于第二个数组,我没有任何线索。我认为这很简单,但第二个数组要复杂一些

以下是cpp:

#include <iostream>

using namespace std;

extern "C" double Average (int, double []);

void main ()
{
    double Array1 [10] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0};
    double Array2 [11] = {-1.1, -2.2, -3.3, -4.4, -5.5, -6.6, -7.7, -8.8, -9.9, -10.0, -11.0};

    cout << "Average of Array1 is " << Average (10, Array1) << endl;
    cout << "Average of Array2 is " << Average (11, Array2) << endl;
}
这应该起作用:

_Average proc
  finit
  mov ecx, [esp + 4]      ; get the number of elements
  mov ebx, [esp + 8]      ; get the address of the array
  fldz
  jecxz   noelems
nextelem:
  fadd    REAL8 PTR [ebx]
  add     ebx, 8
  loop    nextelem
  fidiv   [esp + 4]
noelems:
  ret
_Average endp

您应该使用ECX作为循环计数器,并在循环内执行fadd。您还需要除以元素数并确保它不是零。为什么不先在C++中实现平均值,使用g++的
-S
开关获取程序集输出,然后根据需要修改它?
_Average proc
  finit
  mov ecx, [esp + 4]      ; get the number of elements
  mov ebx, [esp + 8]      ; get the address of the array
  fldz
  jecxz   noelems
nextelem:
  fadd    REAL8 PTR [ebx]
  add     ebx, 8
  loop    nextelem
  fidiv   [esp + 4]
noelems:
  ret
_Average endp