Adding an array of floating points
Since FPU Stack only has 8 slots, how can I add more elements. I have an array of 10 elements that I need to add. Here's what I have so far
_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] ; get first element of array
fld REAL8 PTR [ebx + 8] ; get second element of array
fld REAL8 PTR [ebx + 16]; this element is now at the top of the stack
fld REAL8 PTR [ebx + 24]
开发者_运维百科 fld REAL8 PTR [ebx + 32]
fld REAL8 PTR [ebx + 40]
fld REAL8 PTR [ebx + 48]
fld REAL8 PTR [ebx + 56]
;fld REAL8 PTR [ebx + 64]
;fld REAL8 PTR [ebx + 72]
fadd
fadd
fadd
fadd
fadd
fadd
fadd
;fadd
;fadd
fwait ; if necessary wait for the co-processor to finish
ret
_Average endp
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;
}
Keep a running total instead of loading them all and then adding them all.
There's no need to load first then add, you can just do this:
fld real8 ptr [ebx]
fadd real8 ptr [ebx + 8]
fadd real8 ptr [ebx + 16]
fadd real8 ptr [ebx + 24]
and so on...
精彩评论