Averaging floating value in assembly
I have to get average. In C++ since I don't pass it the length of the array I don't know how to divide it by that.
Here's how I'm passing my values in C++
extern "C" double Average (long [6]);
_Average proc
finit
mov ecx, 6 ; number of elements
mov ebx, [esp + 4] ; address of the array
fldz
L1:
fld REAL4 PTR [ebx] ; get elements of array
fadd
add ebx, 4
loop L1
fdiv DWORD PTR [esp + 4]
fwait
开发者_Go百科 ret
_Average endp
Then pass the length. Or, since it's a fixed-sized array of length 6, just assume it's 6.
You already know that it's six elements long, since you're using that as your loop count (in ecx
) and it is specified in the prototype. The only alternative is to pass another parameter with the length (as you would have to do in pure C++ anyway).
You also need to use fild
instead of fld
to load a long
, since it is not a floating point variable, as well as changing REAL4 PTR
to DWORD PTR
. Alternatively, change long
to float
. (Another alternative is changing long
to double
, though this changes the size of the data type which will require more modifications.)
精彩评论