Sign In
Forgot Password?
Sign In | | Create Account

Assembly language is always smallest/fastest - not!

Colin Walls

Colin Walls

Posted May 27, 2009
5 Comments

It would seem intuitive that writing in assembly language is the best possible option if you want the most optimal code in terms of size and/or speed. After all, assuming the programmer is smart and competent in the assembler for a particular CPU and understands its architecture well enough, and he has been fully appraised of the functional requirements of the code, the only possible result is code that uses the processor’s capabilities in an ideal way. A compiler just cannot compete, as it has no information on the precise requirements of the code.

Well, that would just seem common sense, but it does not take into account one key factor: human nature.

Let me illustrate this issue with a simple example: a C language switch statement. There are three patterns which I can envisage for the case values: contiguous values; almost contiguous values, with a few values missing; completely non-contiguous values. For contiguous case values, a good compiler will probably generate code with a simple list of addresses, which is indexed by the case values. The same thing would result for almost contiguous values, except the table would have a few dummy entries. For completely non-contiguous values, the likely code is a look-up table of values and addresses. In short, a compiler will use a strategy which is appropriate to the pattern of case values.

A smart human programmer would take a different approach and almost always code a look-up table. Why? Because this code is maintainable. If the code was written for contiguous values and then a change of requirements made the case values non-contiguous, a re-write would be necessitated and a smart programmer would want to avoid that possibility. A compiler re-writes the code every time you run it, so it does not care.

So, for switch statements anyway, a compiler will, on average, produce better code than a human programmer. The result is that C will, thus, yield a more efficient result than assembly language.

C++

More Blog Posts

About Colin Walls Follow on Twitter

Colin WallsI have over twenty-five years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles and two books on embedded software, I am a member of the marketing team of the Mentor Graphics Embedded Systems Division, and am based in the UK. Away from work, I have a wide range of interests including photography and trying to point my two daughters in the right direction in life. Learn more about Colin, including his go-to karaoke song and the best parts of being British: http://go.mentor.com/3_acv Visit The Colin Walls Blog

More Posts by Colin Walls

Comments 5

Post a Comment
Interesting article and good subject. Assembly rarely gets mention these days. Anyways I just wanted to add another viewpoint in the sense that, most programmers that code in assembly (the ones I mostly know are in the security software and OS development field) think differently. So the human nature argument doesn't really come into play when a *good* assembly programmer knows exactly how many clock cycles every instruction will take etc. It's true that with the optimization available on most current C compilers, sure, its hard to compete with that. But with that being said, my main response is as follows: "Assembly language is always the smallest/fastest - not!" Is not true. Assembly always is the smallest/fastest and always will be because it is what every other programming language will be translated to in order to run. Therefore the only merit your article has is based on nothing to do with assembly, but with humans. Or at least the assembly programmers that you know. I only bother to post this because it's easy to read your article and think "Okay so I will just use C and never assembly because of my own human nature." No. If you are smart enough and understand each instruction well then assembly will always be the fastest/smallest and if it is not then it is lack of education and not a lack on part of anything to do with "assembly." -Ryan

Ryan
2:57 AM Jan 29, 2010

All good and valid points Ryan. Maybe the title should be "Assembly language programmers don't always produce the fastest/smallest code".

Colin Walls
9:48 AM Jan 29, 2010

I agree with Ryan's point and I think the suggested title by Colin fits well with that point. Good article overall.

Ajay Bhargav
4:27 AM Mar 22, 2013

Ryan is right. A good example is in DOS, where the interrupt table is broken down by a series of compare statements, and this is quite often the best way to do a switch in assembly. Yet, when a certain type of processor is used, a switch table can be used and one set of call/return sequence will end up at the right location. Or with some processors, the vector can be preloaded and a call, pop old return, push new return, and then the return instruction will create the branch. The deal is how does the assembly programmer choose to automate the branch vs how the compiler generate the code and what kinds of optimization take place. Ryan is right, the programmer who knows his code will do better optimizations and create better assembly than the compiler/optimizer. Most of the good programmers I know will write code in high level languages, then hand optimize at both the C and assembly languages to get the best results. Using C gets the overall construct and working code faster on large projects, optimizing at assembly gives the best run time performance. For embedded products, hand coded assembly is often the only effective choice for power, size, speed, and cycle count factors.

Howard Howell
4:35 PM Mar 22, 2013

Even if I agree with Ryan and Colin, this is always a neverending story. Yes, assmebler will always be fastest and smallest as soon as programmer well knows the core's assembler and features. Nowadays, most of time portability and also more readable/maintainable code is needed. For that reason, C is the best choice. But it is also important to say that some cases need from developper to use assembly language to have completet mastery of execution time and actions. In these cases, C will not produce accurate code whatever the compiler is. From my experience, I saw many RTOS schedulers written in assembly where the services where written in C language. I personnally think this is the way most applications are developped today. What we can add is that portability forces the uses of high level languages but also the use of more powerfull processor because of "encapsulation" for all features (you can also call that drivers, BSP,...) and so assembly language is not really used by most of developpers.

TARRIS
8:53 AM Mar 25, 2013

Add Your Comment

Please complete the following information to comment or sign in.

(Your email will not be published)

Archives

 
Online Chat