Assembly language is always smallest/fastest - not!

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.

Preparing Recommendations

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.

About Colin Walls

imageI 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. Visit The Colin Walls Blog

More Posts by Colin Walls

More Blog Posts

Preparing Recommendations

Comments (↓ Add Your Own)

2 Comments on this Post

Commented on 2:57 AM, Jan 29, 2010
By Ryan

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

Commented on 9:48 AM, Jan 29, 2010
By Colin Walls

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

Add Your Comment

Please complete the following information to comment or sign in.

(Your email will not be published)