Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Last week I was wondering, with compilers getting better and better at optimizing, will there be a point when there is no need for hand written assembly? Are there still specialized fields where the compilers aren't smart enough to produce code that rivals hand-written assembly?

share|improve this question

closed as not constructive by Walter, gnat, Jim G., Thomas Owens Oct 21 '12 at 22:33

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
Not technically an answer but someone is going to write compilers and in a way they are going to write assembly (in the sense that they decide what assembly is output from the compilers) –  Ebenezer Sklivvze Apr 23 '11 at 10:55
2  
See @tcrosley's reply to the memory management question on tiny programmable devices. –  rwong Apr 23 '11 at 18:11
1  
Well, someone has to write the compiler! How are you going to do that without writing hand-written assembly? –  user16764 Jun 9 '12 at 19:33

7 Answers 7

up vote 19 down vote accepted

will there be a point when there is no need for hand written assembly?

Never. There is always a need for specialized hardware-specific instructions.

Are there still specialized fields where the compilers aren't smart enough to produce code that rivals hand-written assembly?

The kernel's interrupt management, features of I/O drivers, locking and thread synchronization often must be include some assembler because they make use of instructions that are outside the standard instruction set used by a compiler.

Indeed, as Intel moves forward trying to resolve the memory ordering issues they have created (http://www.mpdiag.com/intel_arch.html) there may be additional or different instructions that may have to be added or adjusted inside the common OS kernels; things that compilers won't normally generate for end-user applications.

share|improve this answer
    
Some programming languages have constructs which allow weaving in short segments of assembly code. Sometimes the compiler provides nice wrappers for each assembly instruction so that it looks like a function call with appropriate return types. See C++ Intrinsics for example: msdn.microsoft.com/en-us/library/hd9bdb82(v=vs.80).aspx (GNU C and Intel C also supports intrinsics) –  rwong Apr 23 '11 at 18:16
    
@rwong: Exactly. This is often a good way to handle the hand-crafted machine-specific code. –  S.Lott Apr 25 '11 at 10:59

For some very special microcontrollers it is not worthwhile to port a compiler.

For example, I once wrote about a thousand lines of assembly for a 24-bit controller in an FPGA, for a special purpose application.

share|improve this answer

First of all, I think that some people write assembly "because it's there", in much the same way as Sir Edmund Hillary talked about people climbing Mount Everest because it's there. Its very existence is a challenge to them, and they'll embrace that challenge whether they have a practical need (or even use) for the result or not.

That said, my own take on things is that while assembly language is used for a shrinking percentage of all code, there's still enough growth in programming overall that in an absolute sense, assembly coding is still growing.

For example, quite a bit of MS-DOS was written in assembly language. Just for the sake of argument, let's assume that it was all hand-written assembly language. For the sake of a round number, let's figure that means hand written assembly language that produced about 100 kilobytes of output code -- probably something like 50 thousand lines of source code.

The last I heard, Microsoft claimed that only about one tenth of one percent of Windows was written in assembly language. Windows is currently somewhere around 150 million lines of code though -- so .1 percent of that is a total of 150,000 lines.

I'd note as well that Windows represents an area that's probably the most amenable to using a compiler -- it's intended primarily for fast machines with lots of memory, and lots of resources in general. Features are much more important than minimizing resource usage.

Small embedded systems (for one obvious example) undoubtedly favor hand written assembly code to a much greater degree. The code for a typical microwave hardly needs huge chunks of code for things like file systems, antialiased font rendering, or managing multitudes of machines over a network.

share|improve this answer

I think this question can be split into 2 parts:

Will there come a time when writing in assembly to increase the speed of execution will die out?

I believe it has already come.

Will there come a time when writing in assembly will die out?

No, you'll always need closer access to the hardware when writing things like hypervisors, kernels, device drivers, etc.

share|improve this answer
3  
As far as I know the first part of your answer is plain wrong. Manually writing assembly for speed optimization has in no way died out. Just have a look into the area of video encoding / decoding / transcoding. You'll see lot's of assembly there. Or just any other field, where performance actually matters. –  NikiC Apr 23 '11 at 17:52
    
Yes, I agree that you can find examples of people who have hand-written assembly in their source code, but in many cases, the assembly that they have written is now slower than what a good optimizing compiler can produce. –  dan_waterworth Apr 24 '11 at 4:56
    
Dan, can you give some examples? –  JBRWilkinson Jun 10 '12 at 10:30
    
@JBRWilkinson, I remember seeing an LLVM presentation that showed at least one case. Unfortunately, I can't seem to find it. I'm sure you can find a case where someone writing a codec dropped into assembly for the inner loop and that improved performance by 3%, but when you consider: that with every compiler release you'll need to re-evaluate it, the amount of extra time and knowledge that it takes to do it in the first place, the drop in maintainability of the codebase and the vanishingly small amount of places where dropping into assembly actually improves performance, it rarely makes sense. –  dan_waterworth Jun 11 '12 at 7:42

There are certain things, that you can only do with assembly, but you will find that those are already conveniently wrapped into functions. So you don't actually need to do them by hand.

I guess if you program a very specific chip, that you know everything about, using assembly might just squeeze the last bit out of it (although it should be possible to put that knowledge into a compiler).

However on normal CPUs with all the different architectures and even instruction sets and things as branch prediction and out-of-order-execution you might actually slow things down with hand-written assembly, except if you build it for a specific chip. This effort is unjustifiable, since a proper C compiler can break down the language to all those architectures leveraging most of their peculiar optimization possibilities, while trying to do all this by hand will make your head explode and take up a budget noone on earth is willing to pay, because it is not even clear that the result will be measurably faster.

share|improve this answer
    
Good point about hand-written could be slower –  JBRWilkinson Jun 10 '12 at 10:45

There will always be a need for programming in assembly by hand as not all generated assembly is most optimal or even the fastest. A great example of this is on: http://www.virtualdub.org/blog/pivot/entry.php?id=307

share|improve this answer

Assembly or shellcode in general, is a most importance thing when you're writing a malware that exploit some security vulnerabilities (it's called worm). You must to write sequence of assembly instructions to bypass anti-virus. Most of anti-virus use signature-base technique to detect malicious code. Because of that, you cannot reuse old shellcode. It's funny to write some line of assembly codes and made it work for some reasons :)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.