平均浮动数汇编

时间:2013-11-08 00:43:20

标签: 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.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;
}

我的代码:

 .386

 .model flat

  public    _Average

 .data

 .code

 _Average proc

    finit

    mov ecx, [esp + 4]      ; get the number of elements
    mov ebx, [esp + 8]      ; get the address of the array

    fld     REAL8 PTR [ebx]
    fadd    REAL8 PTR [ebx + 8]
    fadd    REAL8 PTR [ebx + 16]
    fadd    REAL8 PTR [ebx + 24]
    fadd    REAL8 PTR [ebx + 32]
    fadd    REAL8 PTR [ebx + 40]
    fadd    REAL8 PTR [ebx + 48]
    fadd    REAL8 PTR [ebx + 56]
    fadd    REAL8 PTR [ebx + 64]
    fadd    REAL8 PTR [ebx + 72]

    fdiv    REAL8 PTR [ebx + 72]
    ret
    _Average endp

    end

1 个答案:

答案 0 :(得分:0)

这应该有效:

_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