问题描述
我应该写使用C ++和汇编程序。程序必须统计数组的平均值。
I should write the program using C++ and Assembly. Program must count the average of the array.
CPP文件必须从用户的数据并显示result.In数组必须是实数(浮点)。
The CPP file must take the data from the user and display the result.In the array must be the real numbers (with floating-point).
该ASM文件必须指望这个数组的平均值。
The ASM file must count the average of this array.
这是.cpp文件:
#include <iostream.h>
#define L 4
extern "C" float average(float* tab, int G);
int main()
{
float tab[L]={0};
cout<<"Enter array: \n";
for(int i=0; i<L; i++)
cin >> tab[i];
cout << "Average value of entered array = " << average(tab, L);
cout << "\nThe end of the programm\n";
return 0;
}
这是我的组装code:
And here is my assembly code:
.386
.model SMALL,c
PUBLIC average
.stack 100h
.data
.code
average PROC
push ebp
mov ebp, esp
push esi
mov ecx, [ebp+12]
mov esi, [ebp+8]
finit ;coprocessor
fldz
sum:
fadd dword ptr [esi+ecx-4] ;ST(0)=ST(0)+ST(i)
loop sum ;retry sum while cx!=0
fidiv dword ptr [ebp+12] ;Division
pop esi ;End of the programm
pop ebp
mov eax, esi
ret 8
average ENDP
END
结果始终是2.422547e + 198
The result always is 2.422547e+198
我哪里有错?谢谢!
推荐答案
由于这是float数组,你应该通过4乘以指数每次取4个字节还要注意,C调用约定任务调用者将释放的论点,这样你的 RET 8
是错误的。
Since it's an array of floats each taking 4 bytes you should multiply the index by 4. Also note that C calling convention mandates that the caller will free the arguments, as such your "ret 8
" is wrong.
的 MOV EAX,ESI
结尾是无关紧要的。
这篇关于用C汇编语言++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!