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?
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.
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.
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.
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.
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
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 :)