使用C ++汇编语言

时间:2014-05-25 21:21:17

标签: c++ arrays assembly floating-point

我应该使用C ++和Assembly编写程序。程序必须计算数组的平均值。

CPP文件必须从用户获取数据并显示结果。在数组中必须是实数(带浮点数)。

ASM文件必须计算此数组的平均值。

那是.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;
 } 

这是我的汇编代码:

.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

哪里有错误?谢谢!

1 个答案:

答案 0 :(得分:0)

由于它是一个浮点数组,每个浮点数占4个字节,你应该将索引乘以4.还要注意C调用约定要求调用者释放参数,因此你的“ret 8”是错误的。 / p>

最后的mov eax, esi无关紧要。

相关问题