Linux Assembly HOWTO
Prev Chapter 2. Do you need assembly? Next

2.1. Pros and Cons

2.1.1. The advantages of Assembly

Assembly can express very low-level things:

  • you can access machine-dependent registers and P>

  • you can control the exact code behavior in critical sections that might otherwise involve deadlock between multiple software threads or hardware devices

  • you can break the conventions of your usual compiler, which might allow some optimizations (like temporarily breaking rules about memory allocation, threading, calling conventions, etc)

  • you can build interfaces between code fragments using incompatible conventions (e.g. produced by different compilers, or separated by a low-level interface)

  • you can get access to unusual programming modes of your processor (e.g. 16 bit mode to interface startup, firmware, or legacy code on Intel PCs)

  • you can produce reasonably fast code for tight loops to cope with a bad non-optimizing compiler (but then, there are free optimizing compilers available!)

  • you can produce hand-optimized code perfectly tuned for your particular hardware setup, though not to someone else's

  • you can write some code for your new language's optimizing compiler (that is something what very few ones will ever do, and even they not often)

  • i.e. you can be in complete control of your code

2.1.2. The disadvantages of Assembly

Assembly is a very low-level language (the lowest above hand-coding the binary instruction patterns). This means

2.1.3. Assessment

All in all, you might find that though using assembly is sometimes needed, and might even be useful in a few cases where it is not, you'll want to:

  • minimize use of assembly code

  • encapsulate this code in well-defined interfaces

  • have your assembly code automatically generated from patterns expressed in a higher-level language than assembly (e.g. GCC inline assembly macros)

  • have automatic tools translate these programs into assembly code

  • have this code be optimized if possible

  • All of the above, i.e. write (an extension to) an optimizing compiler back-end.

Even when assembly is needed (e.g. OS development), you'll find that not so much of it is required, and that the above principles retain.

See the Linux kernel sources concerning this: as little assembly as needed, resulting in a fast, reliable, portable, maintainable OS. Even a successful game like DOOM was almost massively written in C, with a tiny part only being written in assembly for speed up.


Prev Home Next
Do you need assembly? Up How to NOT use Assembly