I have written a test program in asm(masm) and in c++(ms visual c++ 6.0), as following:
in c++ ??

int i, j, k, l

l = 0;
for(i = 0; i < 1000; i++ ) {
for(j = 0; j < 1000; j++ ) {
for( k = 0; k < 1000; k++ ) {
l++;
}
}
}

in asm:
LOCAL i
LOCAL j
LOCAL k
LOCAL l

mov l, 0
mov i, 0
.while i < 1000
mov j, 0
.while j < 1000
mov k, 0
.while k < 1000
inc l
inc k
.endw
inc j
.endw
inc i
.endw

then i compile and run them. I find that the c++ code is fast than the asm code!!!

so why we use asm?
Posted on 2003-09-22 01:03:57 by lovelypp
try put variables values to register, and xor reg,reg inside the cycles. Should really a little faster.

B7
Posted on 2003-09-22 01:28:19 by Bit7
Simple loops are not enough to compare two languages. You must also be aware of that asm itself isn't even faster than Visual Basic, it totally depends on how skilled the person is who writes the code.
Posted on 2003-09-22 01:28:22 by Delight
in some cases.. you will not gain much more speed in ASM then C++, but C++ being faster then ASM is based on you skill in ASM..
Posted on 2003-09-22 01:29:55 by devilsclaw
Firstly I think the quality of the asm code that you coded is poor. :/ If you are making use of asm, better make use of registers. What is the point of have 8 general purpose registers but not using them? And perhaps you can try replacing the inc with add (According to intel P4 manual, it would be faster). ;)
Posted on 2003-09-22 01:39:36 by roticv
thanks roticv

i havent gotten to that book yet but it was one of my questions.
Posted on 2003-09-22 01:44:38 by devilsclaw
If you use the C++ code like that, it will produce no code at all since you don't use l (so there's no point in calculating it). If you want it to generate code you need to do something with l after the loop, like returning it from main()..
For example:



int foo()
{
int l = 0;
for(int i = 0; i < 1000; i++ )
for(int j = 0; j < 1000; j++ )
for(int k = 0; k < 1000; k++ )
l++;
return l;
}

int main()
{
return foo();
}


This is what VC 7.1 outputs as main()


mov eax, 3B9ACA00h
ret

:grin:

Thomas
Posted on 2003-09-22 01:47:06 by Thomas
Thomas, I belive you write it with "compiling by mind" :)

This is good example of optimization

Sure, we can write in asm
mov  eax,1000000000

ret

lovelypp
Really, compiler can save your time.
Because compiler is written by guys who underatand many low-level things.
At the other side, you can save some "runtime", but you need to learn some tricks.
Posted on 2003-09-22 02:12:00 by S.T.A.S.
Isn't 3B9ACA00h = 1000^3? I think it is one of the coolest optimisation I have seen. :stupid:

devilsclaw,
The intel manual states that it is better to replace inc and dec with add and sub as inc and dec only affects a subset of the EFLAG, eg it does not affect carry flag. "This creates a dependence on all previous writes of the flag register." But of course the good thing about inc/dec is that it is one byte short.
Posted on 2003-09-22 02:36:02 by roticv
nothing is faster than ASM
because every programming languages are translated into asm.
If your program written in asm is slower than in HighLevelLanguage,
it means you do something wrong or you do another operation which is needn't.
Posted on 2003-09-22 03:48:57 by etn
There's also another little reason - compilation time :)
I got used to compiling large proggies in asm , for just 1 to 3 seconds.
A week ago I had to install VC++6.0 again, for a job . So, there was a 1000-line code that I had to compile to see how the plugin works, and :eek: 50 seconds. Of course, I can later make a "precompiled header" and incremental compiling, but the first shock stays. Good that it isn't as slow as VB - a 500-line code had to compile for 15 minutes :eek: :eek:
I love testing my code incrementally - this way there are no bugs left, and even a 3-second difference in compilation time is critical for such an operation. (I guess :grin: )

this discussion has been taking place over and over :( , don't you think?
Use the search, Luke! :)
Posted on 2003-09-22 04:43:17 by Ultrano
C programs grow exponentially with size of executeable code. None high level assembler nuemonics produce pure machine code. For instance a C program with 5k of executable code my actually be 10K in file size and a 15k of execution would be more like 55k file size. C language has heaps of translation code in it to point to the actual machine code that does something. Assembler is pure machine code. Try writing a much larger program next time and see which is faster. C will get slower with size and takes up too much space on disk.
Posted on 2003-09-22 06:51:22 by mrgone
The 'pure' code generated by a compiler won't really be that much larger than similar code in assembly (with the same amount and type of optimizations applied). Sure, you have some hits because of the runtime libraries included, but how bad is 75k if your application is 500k large? Besides, you should really use the runtime functions, so that there will be very little dead code.

And if you want to do "to the bones" API programming without the runtime library, well, just about every C/C++ compiler (and a lot of other HLLs) will let you not link in the runtimes, and then your executable size can compete with assembly.

If you just apply a little care and consideration while designing and programming, you can get nice results... whether you use a highlevel language or not. Iirc, farbrausch's FR-08 64k winning intro at The Party 2000 was coded in C++.
Posted on 2003-09-22 09:05:19 by f0dder
a 500-line code had to compile for 15 minutes


Ultrano, are you using a 100-MHz Pentium with 16 MB Ram? Even on a 333MHz with 32MB Ram (Win98) I've compiled a 200,000 line VB Program in less than 30 seconds (VB6) -- worst case with a forced recompile from scratch still only took less than 2 minutes (totally acceptable). Now, on a 1.7GHz with 512MB Ram, I can compile a combined 6 projects in a solution of about 80,000 lines of code in less than 12 seconds. I can't imagine 500 lines of VB taking 15 minutes.

<sarcasm>Perhaps if the MSN Messenger, AIM, ICQ, and Yahoo Messenger apps were closed before compiling, McAffee always interferes so I always disable if I'm developing (I use Norton these days), close your favorite file sharing application, then you may get better performance</sarcasm>


Thanks,
Shawn
Posted on 2003-09-22 11:14:21 by _Shawn

Isn't 3B9ACA00h = 1000^3? I think it is one of the coolest optimisation I have seen.


Actually, I once saw GCC (with -O3) replace a function call (passed a constant parameter), that called another function, that computed some value based on the original parameter, with a single MOV instruction. Now *that* was quite impressive. Of course, it took forever to compile and the "-O3" setting isn't safe for all programs, but it was still *very* impressive.
Cheers,
Randy Hyde
Posted on 2003-09-22 11:55:06 by rhyde
Visual studio .net, especially the 2003 edition, can do some pretty cute stuff when "global optimizations" are enabled. Object files are compiled to a bytecode format, and optimizations aren't done until link time. Of course this takes _forever_ when you're doing large applications, and should only be done for few test builds and release versions... but it's pretty cute nevertheless.
Posted on 2003-09-22 11:59:11 by f0dder
_Shawn, in fact, yes. I had to use a 233MHz - 32ram Pc, that was burdened as a server for other 10 PCs , and maybe the code was more than 500 (maybe 5000), but I remember how much I was sweating, waiting for this compilation to complete, in front of my employer. I had to fix some program , and create my own installation, that would install the files on even the worst PC one could find these days. I managed to complete the job, "ULIS" is the installation package I posted here. Then I was asked to make a VB database program, whose development the employer and my colleagues dragged down, and finally didn't pay me anything for either of the tasks. :stupid:
My PC at home isn't good for computation either. But my GPU is cool - GeForce2, so I can still play some games.
but at least I can play mp3 :), and my own musical software :)
Posted on 2003-09-22 13:42:02 by Ultrano
by the way, not all c++ compilers are clever as M$VC...
At job, i've re-wrote two routines from c++ to asm.. since compiler was bcb, i 've beat the c++ compiler, regular my asm sketch routines (no optimization) are 35 % faster :)
Thanks to Mr. Borland, now at job i seems a genius of assembly ... hahaha
Posted on 2003-09-22 15:05:01 by Bit7
In a related item, see my new post in this thread:

http://www.asmcommunity.net/board/index.php?topic=15286

:grin:
Posted on 2003-09-22 20:07:51 by S/390

Visual studio .net, especially the 2003 edition, can do some pretty cute stuff when "global optimizations" are enabled.
Intel's compiler does the same thing and certainly better if your targeting P4.
Posted on 2003-09-22 21:20:23 by bitRAKE