Assembly Language in Linux

WHY?

I suppose that is the most common question I am asked when anyone learns that I program in assembly language. There are two reasons. Both of them settle in the same category of “optimization.” For one thing, no matter how efficient the compiler, no compiler can come close to assembly when it comes to size (compactness). Any compiler uses a certain amount of overhead for certain operations that are not visible in programming with higher-level languages. For a similar reason, assembly language is much faster that compiled languages. Without having so much overhead to deal with, the computer can spend it's time actually processing data instead of processing overhead. Most programs typically need to depend on either speed or size to operate according to user specifications.

In many cases, most home computers carry enough internal memory for the average user to run whatever programs they will use. However, in recent years, there has been a massive movement towards small, portable systems. These are called “embedded systems.” These embedded systems are usually very small, compact devices such as cell phones and personal data assistants (PDAs). Because these devices are so small, and always getting smaller, having “plenty” of memory is no longer the typical programming environment.

In many applications, speed is not important. Most applications used by the “average” users are in an environment where data is entered, and then click on an “OK” button, and after a short pause (typically ½ second) and their results are displayed. In many cases, even a 1-second delay is acceptable, if not expected. However, probably any serious game player would be first to require (and brag about) the latest and greatest (meaning fastest) CPU they could buy, regardless of how far it put them in debt. Next, however, are the few people working in scientific communities, whose mathematical models continue to grow at exponential rates. Recently, NASA unveiled it's newest supercomputer, and although they never mentioned the details about processing speed or throughput, it was still fascinating to look at the rows and rows of racks of computers. (By the way, anyone suffering from the delusion that such a machine would run Microsoft Windows needs to go back and re-read their system requirements.) I have to wonder, after looking through various magazines at similar supercomputers' processing capabilities, would that much processing power be needed if assembly language was a more prominent programming language?

So, if assembly language is so powerful, why isn't it used more?

Despite these two HUGE benefits of assembly language, it comes at a high cost.

Assembly language is not complicated to learn, but it is complicated to use. What may be done in a single line of a high-level language may require a page of assembly code. Also, high-level languages use algebra-like statements to perform calculations, while assembly language shows every little detail of moving date from point to point (i.e. from memory into internal registers) before finally executing the command to add, followed by moving more data (i.e. moving the sum back into memory).

Assembly languages is not portable. There are many different microprocessors in use now, most notably the Pentium family (including the many many clones), Motorola's 68K family, the StrongARM family (very prominent in the PDA arena), and many others. All of these microprocessors have a different language. A program written for a Pentium will not work on a computer using any other processor. Likewise, even if one program is written for a specific microprocessor, that will not guarantee that the same program will run on all computers using the same microprocessor. For example, the Apple II, Atari2600, and Commodore VIC20 all used the same microprocessor, the 6502, but although the code would run, the program would seem to fail because the peripherals and I/O ROM values would all be different in the different machines. Generally speaking, any program written in assembly language will only work on that type of computer. Fortunately, the IBM PC has grown to a point where regardless of who manufactures it, the overall functionality remains the same from one machine to another, even as the Pentium I gave way to the Pentium II and II and now the Pentium IV, these families have stayed backwards-compatible, so a program written for a Pentium I will still work on a machine using a Pentium IV. How long will this remain true? Who knows?

Now, all that being said, is the benefit worth the effort? In my opinion, yes, usually. There are going to be times where it seems like the effort is far more than the benefits, but there will also be times that although the benefit may not bee seen directly, a project may not work at all without using at least some assembly language. As an example, my WAVES project is one that would never have worked without assembly language, even though there were other components of the projreect that have been writen in other languages (mostly 'C'/C++). "What about these newer machines that are so much faster and have so much more memory?" In many ways, programs have not really gone through many changes over the last few years. Even the most advanced games are really little more than remakes of older games, some even use the same game engines, and benefit from larger, faster computers only by their ability to do more calculations (usually used to add more graphics) in the game. What has really happened is that because the computer is so much faster, now, than say, 5 years ago, programmers get away with writing sloppy and inefficeint code and it doesn't matter, anymore, because the processor, being so much faster, still manages to complete it's calculations on time (whatever that timeframe happens to be. In my pages on Linux Assembly language, I will use a number of examples of programs written in assembly lanuguage and compare them against other higher-level languages, and examine the speed and gbenefits of using assembly language.

Now, before I get too far, a short disclaimer.... I am familiar with many different microprosessors, and I am quite aware that linux (and unix in general) can be run on many different processors as well. I have chosen to target the Intel family, specifically the Pentium family of processors only because these computers are the most readily available computers available. Realizing that that probably alienates all thos Mac users, let me also state that although the Macintosh uses a different microprocessor, but a lot of the thought processes will still be very similar. Also, I don't have a Mac at my disposal... if someone wants to simply donate one, then I will consider spending some time writing pages specifically for the Mac.


That being said, it's time to get started....

Part 1 - An introduction to assembly language in linux.
Part 2 - Assembly language functions as part of a 'C' program
Part 3 - Assembly language program using 'C' standard libraries
Part 4 - Assembly language without 'C'

Wenton's email (wenton@ieee.org)

home