---
created_at: '2013-02-23T20:02:46.000Z'
title: Why Learning Assembly Language Is Still a Good Idea (2004)
url: http://onlamp.com/pub/a/onlamp/2004/05/06/writegreatcode.html
author: jacquesm
points: 47
story_text: ''
comment_text: 
num_comments: 49
story_id: 
story_title: 
story_url: 
parent_id: 
created_at_i: 1361649766
_tags:
- story
- author_jacquesm
- story_5271225
objectID: '5271225'

---
[Source](http://www.onlamp.com/pub/a/onlamp/2004/05/06/writegreatcode.html "Permalink to Why Learning Assembly Language Is Still a Good Idea - O'Reilly Media")

# Why Learning Assembly Language Is Still a Good Idea - O'Reilly Media



| ----- |
| 

|  ![ONLamp.com][1]  | 

 | 

| ----- |
| 

![oreilly.com][2]![Safari Books Online.][3]![Conferences.][4]

 |  
| 

* * * * * * * * * *  |  
|  ![][5]   
![advertisement][6]  | 

| ----- |
| 

|  ![ ][7] |  
|  ![][7] | 

Essentials

[All Articles][8]

[What is LAMP?][9]

[Linux Commands][10]

  

ONLamp Subjects

[Linux][11]

[Apache][12]

[MySQL][13]

[Perl][14]

[PHP][15]

[Python][16]

[BSD][17]

  

ONLamp Topics

[All Articles][8]

[App Development][18]

[Database][19]

[Programming][20]

[Sys Admin][21]

 | 

 |  ![ ][7] |  ![][7]  

![][22]

  

[Print][23] [Subscribe to ONLamp][24] [Subscribe to Newsletters][25]

| ----- |
|  ![No Starch Press][26]  | 

##  Why Learning Assembly Language Is Still a Good Idea

by [Randall Hyde][27], author of [Write Great Code (No Starch)][28]  
05/06/2004  | 

The world is full of case studies outlining software engineering disasters. Almost every programmer has had to work on a project involving "less than stellar" source code that was difficult to read and maintain. On rare occasion, some programmers get the opportunity to work on a well-designed system, an awe-inspiring piece of craftsmanship that usually produces the exclamation, "This is truly great code!" 

![][29]

Clearly, professional software engineers should strive to achieve this level of greatness in all their code. But the real question is, "What makes code great?" Simply "meeting specifications" is not how one writes great code. True, in today's software environment, some might actually believe that simply meeting the specifications sets an application apart, as many development projects fail to meet their basic design goals. 

However, in other areas greatness is rarely defined by doing the expected and succeeding; greatness is defined by going above and beyond what is expected. Software engineers should expect no less from great software--it should go above and beyond the standard conventions for software development. 

### Efficiency Is the Key

Because greatness is a multifaceted attribute, a short article such as this one cannot begin to describe all the possible components of a great piece of software. Instead, this article will describe one component of writing great code that has been neglected in recent years as computer systems have increased in capacity and power: efficiency.

Anyone who has been around the computer industry for a decade or more is well aware of this phenomenon: machines are getting exponentially more powerful per unit cost, yet users do not perceive this improvement in the applications that they purchase. For example, while word processors are clearly faster today than they were 21 years ago, they aren't 16,384 times faster as Moore's Law [1] would suggest. Part of the problem, of course, is that some of the additional processing power has been employed to support new features (such as a bitmapped display), but a large part of the reason software users aren't seeing an increase in speed is because many of today's programmers don't take the time to write efficient software, or they simply don't know how to write fast software.

Outrageous software development schedules that don't give programmers enough time to develop efficient code is certainly a problem, but many of today's programmers have grown up with fast CPUs, whose speed has made up for poor coding habits and, as such, many of these programmers have never had to learn how to write fast code. 

| ----- |
| 

Related Reading

[ ![][30] ][31]

[Write Great Code][32]**  
Volume I: Understanding the Machine  
By [Randall Hyde][33]**

 | 

Unfortunately, when software performance is less than optimal, these programmers generally don't know how to correct the problems with their software. They'll often spout things like "The 90-10 rule," or "I'll just use a profiler to correct the performance problems," but the truth is they don't really know how to improve the performance of their underperforming applications. It's all well and good to say, "I'll just find a better algorithm!" However, finding and deploying that algorithm, if one actually exists, is another matter.

Most of the time you can achieve very good performance boosts by simply improving the implementation of an existing algorithm. A computer scientist may argue that a constant improvement in performance isn't as good as, say, going from an algorithm with O(n^2) performance to one with O(n lg n) performance, but the truth is that most of the time a constant factor of two or three times improvement, applied throughout a piece of software, can make the difference between a practical application and one that is simply too slow to comfortably use. And it is exactly this type of optimization with which most modern programmers have little experience.

Unfortunately, writing efficient software is a skill, one that must be practiced to learn and one that must be practiced to maintain. Programmers who never practice this skill will never be able to apply it the day they discover that their software is running too slow. Even if a programmer has mastered the skill of writing efficient software, the programmer must practice them on a regular basis. So, there are two reasons why some programmers don't write efficient (and great) software today: they never learned how to write efficient code in the first place, or they've allowed their programming skills to atrophy to the point that they no longer write efficient code as a matter of course.

### Practice Your Skills

For programmers who have simply allowed their skills to falter from lack of use, the solution is obvious--practice writing efficient code, even when the project doesn't absolutely require it. This doesn't mean, of course, that a practicing engineer should sacrifice project schedules, readable and maintainable code, or other important software attributes for the sake of efficiency.

What it does mean is that the software engineer should keep efficiency in mind while designing and implementing the software. The programmer should make a conscious decision to choose a less efficient implementation over a more efficient implementation based on economic or engineering concerns, rather than simply utilizing the first implementation that comes to mind. Just as often as not, this simple consideration of different (and possibly more efficient) implementations is all that is necessary to produce great code. After all, sometimes the more efficient implementation is no more difficult to create than an inefficient one. All an experienced engineer may need are multiple options from which to choose.

Unfortunately, unrealistic software development schedules have led many professional engineers to shortcut the careful consideration of software development and implementation. The end result is that many professional programmers have gotten out of the habit of writing great code. Fortunately, this process is easy to reverse by practicing good software development methodologies, such as considering multiple algorithms and their implementations, as often as possible.

### Learn Assembly Language

What about the programmer who has never learned to write efficient code in the first place? How does one learn how to efficiently implement an application? Unfortunately, colleges and universities today largely take the attitude that if you choose a good algorithm, you don't have to worry about the implementation of that algorithm. Far too many students come out of their data structures and algorithms courses with the attitude that if you can only achieve a constant (that is, O(1)) performance improvement, you've really achieved nothing at all, and that attempts at improvement are a waste of time.

Advances in computer architecture have exacerbated this problem--for example, you might hear a programmer say, "If this program needs to be a little faster, just wait a year or so and CPUs will be twice as fast; there's no need to worry about it." And this attitude, probably more than any other, is why software performance doesn't keep pace with CPU performance.

With every new application, the programmer writes the software slower than it ought to run, on whatever current CPU they're using, believing that future CPU performance boosts will solve their problems. Of course, by the time the CPUs are fast enough to execute their software, the programmer has "enhanced" the software, and is now depending on yet another future version of the CPU. The cycle repeats almost endlessly, with CPU performance never really catching up with the demands of the software, until finally, the software's life comes to an end and the programmer begins the cycle anew with a different application.

The truth is, it is possible to write software that executes efficiently on contemporary processors. Programmers were doing great things with software back in the days when their applications were running on eight-bit 5MHz 8088 PCs; the same techniques they used to squeeze every last bit of performance out of those low-end CPUs provides the key to high-performance applications today. So, how did they achieve reasonable performance on such low-end processors? The answer is not a secret--they understood how the underlying hardware operated and they wrote their code accordingly. That same knowledge, of the underlying hardware, is the key to writing efficient software today. 

Often, you'll hear old-time programmers make the comment that truly efficient software is written in assembly language. However, the reason such software is efficient isn't because the implementation language imparts some magical efficiency properties to that software -- it's perfectly possible to write inefficient software in assembly language. No, the real reason assembly language programs tend to be more efficient than programs written in other languages is because assembly language forces the programmer to consider how the underlying hardware operates with each machine instruction they write. And this is the key to learning how to write efficient code -- keeping one's eye on the low-level capabilities of the machine.

Those same old-time programmers who claim that truly efficient software is written in assembly language also offer another common piece of advice -- if you want to learn how to write great high-level language code, learn how to program in assembly language.

This is very good advice. After all, high-level compilers translate their high-level source statements into low-level machine code. So if you know assembly language for your particular machine, you'll be able to correlate high-level language constructs with the machine language sequences that a compiler generates. And with this understanding, you'll be able to choose better high-level language statements based on your understanding of how compilers translate those statements into machine code.

All too often, high-level language programmers pick certain high-level language sequences without any knowledge of the execution costs of those statements. Learning assembly language forces the programmer to learn the costs associated with various high-level constructs. So even if the programmer never actually writes applications in assembly language, the knowledge makes the programmer aware of the problems with certain inefficient sequences so they can avoid them in their high-level code.

Learning assembly language, like learning any new programming language, requires considerable effort. The problem is that assembly language itself is deceptively simple. You can learn the 20 or 30 machine instructions found in common assembly applications in just a few days. You can even learn how to put those machine instructions together to solve problems the same way you'd solve those same problems in a high-level language in just a few short weeks.

Unfortunately, this isn't the kind of knowledge that a high-level language programmer will find useful when attempting to write efficient high-level code. To reap the benefits of knowing assembly language, a programmer has to learn to think in assembly language. Then, such a programmer can write very efficient high-level language code while thinking in assembly and writing high-level language statements. Though code written in this manner is truly great, there is one slight problem with this approach -- it takes considerable effort to achieve this level. That's one of the reasons such code is great -- because so few practitioners are capable of producing it.

| ----- |
| 

Pages: **1**, [2][34]

 | 

[ Next Page![arrow][35]][34]

 | 

![ ][7]  
  
  

 |  ![ ][7] |  ![ ][7] |  ![ ][7] | 

| ----- |
| 

### Recommended for You

 | 

  
 |  ![ ][7] | 

Sponsored by:

![][36]

  
 | 

| ----- |
|  ![ ][7] |  
| 

![O'reilly][37]

© 2018, O’Reilly Media, Inc.

(707) 827-7019 (800) 889-8969

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.

### About O'Reilly

* [Sign In][38]
* [Academic Solutions][39]
* [Jobs][40]
* [Contacts][41]
* [Corporate Information][42]
* [Press Room][43]
* [Privacy Policy][44]
* [Terms of Service][45]
* [Writing for O'Reilly][46]

### Community

* [Authors][47]
* [Community & Featured Users][48]
* [Forums][49]
* [Membership][50]
* [Newsletters][51]
* [O'Reilly Answers][52]
* [RSS Feeds][53]
* [User Groups][54]

### Partner Sites

* [makezine.com][55]
* [makerfaire.com][56]
* [craftzine.com][57]
* [igniteshow.com][58]
* [PayPal Developer Zone][59]
* [O'Reilly Insights on Forbes.com][60]

### Shop O'Reilly

* [Customer Service][61]
* [Contact Us][61]
* [Shipping Information][62]
* [Ordering & Payment][63]
* [The O'Reilly Guarantee][64]

 | 

[1]: http://www.onlamp.com/images/onlamp/onlamp-logo.gif
[2]: http://www.onlamp.com/images/tabs/ora_gray.gif
[3]: http://www.onlamp.com/images/oreilly/safaribooksonline_gray.gif
[4]: http://www.onlamp.com/images/tabs/conf_gray.gif
[5]: http://adserver.adtechus.com/adserv/3.0/5159/425849/0/225/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]
[6]: http://www.onlamp.com/images/ads/advertisement.gif
[7]: http://www.onlamp.com/images/trans.gif
[8]: http://www.onlamp.com/pub/q/all_onlamp_articles
[9]: http://www.onlamp.com/pub/a/onlamp/2001/01/25/lamp.html
[10]: http://www.linuxdevcenter.com/linux/cmd/
[11]: http://www.linuxdevcenter.com/
[12]: http://www.onlamp.com/apache/
[13]: http://www.onlamp.com/general/mysql.html
[14]: http://www.perl.com
[15]: http://www.onlamp.com/php/
[16]: http://www.onlamp.com/python/
[17]: http://www.onlamp.com/bsd/
[18]: http://www.onlamp.com/general/app_dev.csp
[19]: http://www.onlamp.com/general/database.csp
[20]: http://www.onlamp.com/general/programming.csp
[21]: http://www.onlamp.com/general/sys_admin.csp
[22]: http://adserver.adtechus.com/adserv/3.0/5159/425846/0/16/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]
[23]: http://www.onlamp.com/lpt/a/4804 "Go to a print-friendly version of this page."
[24]: http://www.oreillynet.com/pub/feed/8 "Subscribe"
[25]: http://www.oreillynet.com/cs/nl/home
[26]: http://www.onlamp.com/images/icons/1593270038_train.gif
[27]: http://www.onlamp.com/pub/au/1331
[28]: http://www.oreilly.com/catalog/1593270038/
[29]: http://adserver.adtechus.com/adserv/3.0/5159/425847/0/170/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]
[30]: http://covers.oreilly.com/images/9781593270032/cat.gif
[31]: http://shop.oreilly.com/product/9781593270032.do?CMP=ILL-4GV796923290
[32]: http://shop.oreilly.com/product/9781593270032.do
[33]: http://www.oreillynet.com/pub/au/1331
[34]: /pub/a/onlamp/2004/05/06/writegreatcode.html?page=2
[35]: http://www.onlamp.com/images/arrows/arrows-66.gif
[36]: http://adserver.adtechus.com/adserv/3.0/5159/425850/0/154/ADTECH;loc=300;key=key1+key2+key3+key4;grp=[group]
[37]: http://cdn.oreillystatic.com/images/sitewide-headers/logo02.gif
[38]: http://www.onlamp.com#
[39]: http://oreilly.com/academic/
[40]: http://oreilly.com/jobs/
[41]: http://oreilly.com/contact.html
[42]: http://oreilly.com/about/
[43]: http://press.oreilly.com/index.html
[44]: http://oreilly.com/oreilly/privacy.csp
[45]: http://oreilly.com/terms/
[46]: http://oreilly.com/oreilly/author/intro.csp
[47]: http://oreilly.com/authors/
[48]: http://oreilly.com/community/
[49]: http://forums.oreilly.com/
[50]: https://members.oreilly.com/
[51]: http://elists.oreilly.com/
[52]: http://answers.oreilly.com/
[53]: http://oreilly.com/feeds/
[54]: http://ug.oreilly.com/
[55]: http://makezine.com/
[56]: http://makerfaire.com/
[57]: http://craftzine.com/
[58]: http://igniteshow.com/
[59]: https://www.x.com/community/ppx/devzone
[60]: http://blogs.forbes.com/oreillymedia/
[61]: http://shop.oreilly.com/category/customer-service.do
[62]: http://shop.oreilly.com/category/customer-service/shipping-information.do
[63]: http://shop.oreilly.com/category/customer-service/ordering-payment.do
[64]: http://shop.oreilly.com/category/customer-service/oreilly-guarantee.do

