0

can anyone help me please? I have to sum n elements in C++ and ASM, everything is working for C++ but not for ASM, does anybody know how to fix the problem? It calculates the sum for C++ and shows me the time and sum, but in ASM shows 0. But, sometimes it shows 0 for C++, does anybody know whats the problem? I use TurboC++, here it is my code:

#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();
}
  • I'm going to assume you're using Visual Studio because the assembly code doesn't match GCC ( https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html ). What error message do you get? – Max Lybbert Nov 21 '16 at 16:35
  • 1
    I don't get it, how do you want your sum to be calculated in the `asm` part and appear in `s`? You just keep calculating 13+2 in `ax` in there. – The Vee Nov 21 '16 at 16:40
  • If the **time** it shows is zero, it may very well be that your compiler found that this is just a lengthy way of saying "do nothing at all" and removed your assembly part completely. Analyze the result of the compilation to see if it really still contains your `mov ax, 13; add ax, 2` somewhere. – The Vee Nov 21 '16 at 16:42
  • so what do I have to do? :( ...what to change? – Tutunaru Dan Marin Nov 21 '16 at 16:47
  • `clock()` has very low resolution, so it is quite possible that the computations take less than one clock tick. It's similar to a real wall clock where the big hand moves once per minute. If you do your task fast, the hind might not have moved during your work. – Bo Persson Nov 21 '16 at 16:47
  • 1. Please indent your code properly. 2. Turbo C++ is **too** old, almost a decade older than the first C++ standard (C++98), therefore [it shouldn't be used](http://stackoverflow.com/q/1961828/995714), unless when one wants to write code for DOS, because the codes written in it aren't actually C++ standard conformant. – phuclv Nov 21 '16 at 16:53
  • If you want a small test case before taking on the summation of `a[]` in the assembler then make it do at least *something* observable. Perhaps add 1 to `s` in every iteration. I don't know how a variable is introduced to the `asm` part in Turbo C++, that's for your homework. – The Vee Nov 21 '16 at 16:53
  • for example it doesn't have STL and `std` namespace, and use non-standard headers ([`iostream.h` instead of `iostream`](http://stackoverflow.com/q/2976477/995714)). And a serious problem is that you're using `rand` without seeding it with `srand` first – phuclv Nov 21 '16 at 16:55
  • @TheVee Turbo C++ is not that smart to optimize the code out because it was so old to have modern optimization tricks – phuclv Nov 21 '16 at 16:58
  • @LưuVĩnhPhúc I see. Then it would be what Bo Persson said. – The Vee Nov 21 '16 at 16:59

1 Answers1

0

You are changing the value of ax but you can't be sure which local variable in your C++ code is being represented by ax, if any.

Something like:

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

Would be appropriate in this case.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85