2016-11-21 36 views
0

任何人都可以幫助我嗎?我必須在C++和ASM中總結n個元素,一切都適用於C++,但不適用於ASM,有人知道如何解決這個問題嗎?它計算C++的總和並顯示時間和總和,但在ASM中顯示爲0.但是,有時它顯示爲0,對於C++,有沒有人知道最新的問題? 我用TURBOC++,這裏是我的代碼:C++和asm錯誤

#include <iostream.h> 
#include <conio.h> 
#include <stdlib.h> 
#include <dos.h> 
#include <time.h> 




void main() 
{ 
clrscr(); 
int n = 30000; 
double s=0; 
int a[30000]; 
cout << "Array has " << n << " elements 3 times summed"; 
for (int i=0; i<n; i++) 
{ 
    a[i]=rand() % 10 + 1; 
} 
clock_t begin = clock(); 
for(i=0; i<n; i++) 
{ 
    s+=a[i]; 
} 
for(i=0; i<n; i++) 
{ 
    s+=a[i]; 
} 
for(i=0; i<n; i++) 
{ 
    s+=a[i]; 
} 
clock_t end = clock(); 
cout << "\nExecution time for the sum in C++ is: " << ((double)(end-begin)/CLOCKS_PER_SEC); 
int tmp; 
clock_t start = clock(); 
for (int j=0;j<3;j++){ 
for (i=0;i<n;i++) 
    asm { 
    mov ax,13 
    add ax,2 
} 
} 
clock_t stop = clock(); 

cout << "\nExecution time for the sum in ASM is: " << ((double)(stop-start)/CLOCKS_PER_SEC); 
cout<<"\nSum: "<< s; 
getch(); 
} 
+0

我打算假設您使用的是Visual Studio,因爲彙編代碼與GCC不匹配(https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html)。你會得到什麼錯誤信息? –

+1

我不明白,你希望你的總和在'asm'部分計算出來,並出現在's'中?你只需在那裏計算13 + 2的「ax」。 –

+0

如果它顯示的**時間爲零,很可能是因爲你的編譯器發現這只是一個冗長的說「完全不做」的方式,並且完全刪除了你的裝配部分。分析編譯的結果,看看它是否還包含你的'mov ax,13;在某處添加斧頭2。 –

回答

0

你正在改變的ax價值,但你不能確定其局部變量在你的C++代碼是由ax,表示如果有的話。

喜歡的東西:

mov ax,13 
add ax,2 
add <localvar>, ax 

會在這種情況下適當的。

相關問題