Electronic Design

To Compile Or !(To Compile) Assembly Is The Question

C is the language of choice for embedded development, but assembler still ranks high. This is especially true for 8-bit MCUs. But is assembler really the best choice for optimal code size and execution efficiency?

For 32- and 64-bit environments, the answer is a resounding no. Assembler use is so rare that most developers don't even know how to run the assembler, let alone create a macro. Things get a little fuzzier when 16-bit processors are considered. Many 16-bit architectures lack the large register set preferred by C and C++ compiler writers. The 8-bit architectures are even harder for compilers to contend with. But even here, compilers can usually outdo the most experienced programmers.

Of course, your mileage will vary depending on the compiler used. For example, Peter Dibble, distinguished engineer at TimeSys, notes that the typical C compiler, using minimal optimization, compiles a simple signed number function down to nine instructions for the Motorola 68000 family. Clever programmers can cut it to six, but they tend to leave in the conditional jumps that match the algorithm. The best is yet to come, though.

Using a technique called superoptimization, a compiler was able to chop the size of the program down to four instructions with no jump instructions. This not only slashed the size, but also improved execution on pipeline machines. Superoptimization takes into account the entire instruction set and the side effects of each instruction. One of the first papers on the subject was by Henry Massalin, entitled "Superoptimizer—A Look at the Smallest Program," in the conference on Architectural Support for Programming Languages and Systems, 1987.

Programmers are more productive when implementing a high-level language. They also make fewer errors and the code is easier to maintain. I know that I can accomplish more with C++ or Java than with assembler, even on small projects. A good compiler is important, so check out how well your compiler optimizes.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.