{"id":148,"date":"2009-05-27T08:43:19","date_gmt":"2009-05-27T08:43:19","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=148"},"modified":"2026-03-26T16:30:18","modified_gmt":"2026-03-26T20:30:18","slug":"assembly-language-is-always-smallestfastest-not","status":"publish","type":"post","link":"https:\/\/blogs.sw.siemens.com\/embedded-software\/2009\/05\/27\/assembly-language-is-always-smallestfastest-not\/","title":{"rendered":"Assembly language is always smallest\/fastest &#8211; not!"},"content":{"rendered":"<p>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&#8217;s capabilities in an ideal way. A compiler just cannot compete, as it has no information on the precise requirements of the code.<\/p>\n<p><!--more-->Well, that would just seem common sense, but it does not take into account one key factor: human nature.<\/p>\n<p>Let me illustrate this issue with a simple example: a C language <strong>switch<\/strong> statement. There are three patterns which I can envisage for the <strong>case<\/strong> values: contiguous values; almost contiguous values, with a few values missing; completely non-contiguous values. For contiguous <strong>case<\/strong> values, a good compiler will probably generate code with a simple list of addresses, which is indexed by the <strong>case<\/strong> 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 <strong>case<\/strong> values.<\/p>\n<p>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 <strong>case<\/strong> 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.<\/p>\n<p>So, for <strong>switch<\/strong> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It would seem intuitive that writing in assembly language is the best possible option if you want the most optimal&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[312,313,300],"industry":[],"product":[],"coauthors":[],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-news","tag-assembly","tag-c","tag-embedded-software"],"_links":{"self":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":1,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":9756,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/148\/revisions\/9756"}],"wp:attachment":[{"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=148"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=148"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=148"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}