assembly versus copmiler{gcc}

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

assembly versus copmiler{gcc}

 
0
  #1
Nov 3rd, 2007
Hi,

if we had the following program::

  1. int main()
  2. {
  3. int a, d;
  4.  
  5. int b=12;
  6. int c=13;
  7.  
  8. a=b+c;
  9. d=b+c;
  10.  
  11. return 0;
  12. }

what would gcc produce as a result?

would it be::
  1. fetch b
  2. fetch c
  3. add
  4. store to a
  5. fetch b
  6. fetch c
  7. add
  8. store to b

Or

something more efficient like
  1. fetch b
  2. fetch c
  3. add
  4. store to a
  5. store to b


How can i see the assembly produced by the compiler{if it is possible}?

Where does the compiler makes a better job than the programmer?

From your experience where do you need to employ assembly while programming a real life project...

thanks in advance,
nicolas

PS:: sorry if i am asking a lot of things, but i am really interested in the optimization part of programming,
so plz contribute in every way you can...
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,572
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: assembly versus copmiler{gcc}

 
0
  #2
Nov 3rd, 2007
>How can i see the assembly produced by the compiler{if it is possible}?
You can add a switch to most compilers that tell them to produce assembly output.

>Where does the compiler makes a better job than the programmer?
Unless you're a good assembly programmer (and in some cases a fantastic assembly programmer), probably everywhere.

>From your experience where do you need to employ
>assembly while programming a real life project...
It depends on the project, but I'd start with nowhere and use assembly as little as possible and only when absolutely necessary.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: assembly versus copmiler{gcc}

 
0
  #3
Nov 3rd, 2007
> what would gcc produce as a result?
The compiler might infer that both operands are known at compile time, and simply store 25 in a and d.

The compiler might also infer that none of the calculations are used, and remove them completely, thus reducing the program to return 0;
> Where does the compiler makes a better job than the programmer?
By consistently applying all optimisation tricks known to the vast team of programmers which developed the compiler, as opposed to the few techniques you've learnt so far, and can think to use in the given circumstance.
You may eventually beat the compiler on any given sample of code, but the effort on your part would be measured in days or weeks.

> From your experience where do you need to employ assembly while programming a real life project.
I've only ever used assembler in the early bootstrap of a processor before there is a viable environment to support writing C code.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: assembly versus copmiler{gcc}

 
0
  #4
Nov 3rd, 2007
I am an avid and would even venture to say an excellent assembly programmer. In the early 80's my assembly overlays to basic where an essential component to greater throughput. Example sorting 1,000 elements in basic would take 11 mins and with the overlay 30 sec with the kind of hardware available then. BIG difference. Can any compiler code as effectively as a good assembly programmer, never. Today though I even question why compilers even have the ability for inline assembly. Most operating systems and especially the two most popular ones don't allow you to address hardware directly and the amount of code you can crank out in an afternoon is monumental with a "C" compiler versus assembly.

So unless you plan for some reason becoming an extremely good assembly coder, I wouldn't even bother with it. I know if my experience didn't come from a time where it was needed I wouldn't bother for the few milliseconds you might eek of of an application over what most compilers can do.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: assembly versus copmiler{gcc}

 
1
  #5
Nov 3rd, 2007
> How can i see the assembly produced by the compiler{if it is possible}?
gcc: to generate assembly pseudo code, use the -S switch. eg.
g++ -O2 -S -c whatever.cc
to see the C code together with the assembly it was converted to:
g++ -c -g -O2 -Wa,-a,-ad whatever.cc > whatever.asm
microsoft: use one of /FA, /FAc, /FAs, /FAu compiler switches

> > From your experience where do you need to employ assembly while programming a real life project.
a. to implement something like the macros in <cstdarg> ( va_start, va_arg, va_end ) assembly may be required on some platforms.
b. defining things like sig_atomic_t, atomic_add etc. on some platforms.
c. to implement thunks and trampolines on some platforms.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: assembly versus copmiler{gcc}

 
0
  #6
Nov 3rd, 2007
thank you all for your answers:

with the example i stated above gcc didn't do a better job {if i understand correctly the output}
  1. .file "testGCC.c"
  2. .text
  3. .globl main
  4. .type main, @function
  5. main:
  6. leal 4(%esp), %ecx
  7. andl $-16, %esp
  8. pushl -4(%ecx)
  9. pushl %ebp
  10. movl %esp, %ebp
  11. pushl %ecx
  12. subl $16, %esp
  13. movl $12, -12(%ebp)
  14. movl $13, -8(%ebp)
  15. movl -8(%ebp), %eax
  16. addl -12(%ebp), %eax
  17. movl %eax, -20(%ebp)
  18. movl -8(%ebp), %eax
  19. addl -12(%ebp), %eax
  20. movl %eax, -16(%ebp)
  21. movl $0, %eax
  22. addl $16, %esp
  23. popl %ecx
  24. popl %ebp
  25. leal -4(%ecx), %esp
  26. ret
  27. .size main, .-main
  28. .ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
  29. .section .note.GNU-stack,"",@progbits
and with o2 i get::
  1.  
  2. .file "testGCC.c"
  3. .text
  4. .align 2
  5. .p2align 4,,15
  6. .globl main
  7. .type main, @function
  8. main:
  9. .LFB2:
  10. leal 4(%esp), %ecx
  11. .LCFI0:
  12. andl $-16, %esp
  13. pushl -4(%ecx)
  14. .LCFI1:
  15. xorl %eax, %eax
  16. pushl %ebp
  17. .LCFI2:
  18. movl %esp, %ebp
  19. .LCFI3:
  20. pushl %ecx
  21. .LCFI4:
  22. popl %ecx
  23. popl %ebp
  24. leal -4(%ecx), %esp
  25. ret
  26. .LFE2:
  27. .size main, .-main
  28. .globl __gxx_personality_v0
  29. .ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
  30. .section .note.GNU-stack,"",@progbits

Originally Posted by salem
By consistently applying all optimisation tricks known to the vast team of programmers which developed the compiler, as opposed to the few techniques you've learnt so far, and can think to use in the given circumstance.
yes i {also} share this opinion but is there any sound example??

the debate started when i argued with a hardware oriented classmate that has the opinion that you can always write better code with assembly as long as you stick with functions and the complexity stays low... of course i couldn't agree with him{i've been reading this forum for some months now!} but i didn't have any solid example...


another question:: i've learned {a year ago} some pretty basic assembly stuff{doing loops,conditionals, simple algorithms, manipulating the stack}... is there anything else that someone should know?

Originally Posted by vijayan121 View Post
> > From your experience where do you need to employ assembly while programming a real life project.
a. to implement something like the macros in <cstdarg> ( va_start, va_arg, va_end ) assembly may be required on some platforms.
b. defining things like sig_atomic_t, atomic_add etc. on some platforms.
c. to implement thunks and trampolines on some platforms.
this is interesting....when you say trampoline you mean this, right?
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: assembly versus copmiler{gcc}

 
1
  #7
Nov 3rd, 2007
When testing what assembler will be produced, don't use main() as the example function. Being the special function where the program starts, it tends to accumulate baggage which isn't present in any other function.

Eg.
  1. void foo ( ) {
  2. int a, d;
  3.  
  4. int b=12;
  5. int c=13;
  6.  
  7. a=b+c;
  8. d=b+c;
  9. }
  10.  
  11. int main(void) {
  12. foo();
  13. return 0;
  14. }
  15.  
  16.  
  17. $ gcc -S foo.c && cat foo.s
  18. _foo:
  19. pushl %ebp
  20. movl %esp, %ebp
  21. subl $16, %esp
  22. movl $12, -12(%ebp)
  23. movl $13, -16(%ebp)
  24. movl -16(%ebp), %eax
  25. addl -12(%ebp), %eax
  26. movl %eax, -4(%ebp)
  27. movl -16(%ebp), %eax
  28. addl -12(%ebp), %eax
  29. movl %eax, -8(%ebp)
  30. leave
  31. ret
  32. # In other words, it does exactly what the code says
  33.  
  34. $ gcc -S -O2 foo.c && cat foo.s
  35. _foo:
  36. pushl %ebp
  37. movl %esp, %ebp
  38. popl %ebp
  39. ret
  40. # The optimiser realises the results are never used, and the code is gone.
If foo() is declared 'static', then there isn't even that much. There is no function at all, and main() doesn't call it.

> has the opinion that you can always write better code with assembly as long
> as you stick with functions and the complexity stays low
It's certainly always "possible", and nobody is going to disagree that given enough time and experience, you can produce the equivalent translation which is better than the compiler.
But when you can write code in a high level language which achieves 95% of the performance with only 5% of the coding effort, you really need a damn good reason to resort to assembler.

Sure, if you've got nothing else to do, and no set time limit in which to do it, you can sit down and craft your assembler code. But some of us have deadlines to meet and changing requirements to cope with.

If you're writing user-land programs for a desktop machine, then there really isn't any need.

Here is another example of where you might choose assembler.
You're writing PIC code and have very tight memory and/or timing constraints. Every byte and every clock tick matters, and you need to make sure you're getting the best of everything.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: assembly versus copmiler{gcc}

 
0
  #8
Nov 3rd, 2007
thank you salem! your answer was very helpful.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: assembly versus copmiler{gcc}

 
1
  #9
Nov 4th, 2007
> when you say trampoline you mean this, right?
yes, except for the java and ojective-c usage of the term.

perhaps the use of trampolines that you would be most familiar with is that in a thread function. the compiler would compile the code for a thread function like any other function; you *can* safely call it like a normal function if you want. when a return is executed from a thread function (other than the one which executes main), an implicit call needs to be made to the system call (pthread_exit, ExitThread) which ends a thread with proper cleanup. this cleanup code would call cleanup/cancellation handlers, destroy any thread specific data, release the stack(s) etc. this is achieved by placing a trampoline on the thread stack (where the return address is expected).

another common example of trampoline use is in implementing tail recursion (eg. in languages of the lisp family). almost all the implementations of compilers/interpreters are in C. generating code for tail recursion in C (without growing the stack) is achieved by a number of trampoline bounces.

trampolines are also used to implement closures in C/C++. for example, in the (non-standard) implementation of nested functions in gcc. http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

posted by salem:
  1. $ gcc -S -O2 foo.c && cat foo.s
  2. _foo:
  3. pushl %ebp
  4. movl %esp, %ebp
  5. popl %ebp
  6. ret
  7. # The optimiser realises the results are never used, and the code is gone.
if we use the -fomit-frame-pointer switch, no frame pointer is created and the code reduces to
  1. _foo:
  2. xorl %eax, %eax
  3. ret
this may cause problems while debugging code on systems with non-reentrant kernels (eg. linux/gdb) which is probably why gcc does not omit frame pointers by default. linux signal trampolines don't have meaningful frames; when a signal trampoline is invoked from a frameless function, there are two frameless functions in a row. gdb tries to solve this by looking for patterns (of frame pointer set up) in the stack. this usually works, except when a signal occurs just as a function is entered, but before the frame has been set up.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: assembly versus copmiler{gcc}

 
0
  #10
Nov 4th, 2007
Originally Posted by vijayan121 View Post
...perhaps the use of trampolines that you would be most familiar with is that in a thread function...
truth be told, i have never heard about trampolines before your post{my fault}... still everything you wrote is very interesting.

thanks vijayan!

PS::
for other newbies{like me} :: here is a link...that explains some basics of optimization
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC