Programming from the Ground Up
Jonathan Bartlett
Edited by
Dominick Bruno, Jr.
Programming from the Ground Up
by Jonathan Bartlett
Edited by Dominick Bruno, Jr.
Copyright © 2003 by Jonathan Bartlett
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.1 or any later version published by the Free Software Foundation;
with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the
license is included in Appendix H. In addition, you are granted full rights to use the code examples for
any purpose without even having to credit the authors.
All trademarks are property of their respective owners.
This book can be purchased at http://www.bartlettpublishing.com/
This book is not a reference book, it is an introductory book. It is therefore not suitable by itself to
learn how to professionally program in x86 assembly language, as some details have been left out to
make the learning process smoother. The point of the book is to help the student understand how
assembly language and computer programming works, not to be a reference to the subject. Reference
information about a particular processor can be obtained by contacting the company which makes it.
To receive a copy of this book in electronic form, please visit the website
http://savannah.nongnu.org/projects/pgubook/ This site contains the instructions for downloading a
transparent copy of this book as defined by the GNU Free Documentation License.
Table of Contents
1. Introduction........................................................................................................1
Welcome to Programming ...............................................................................1
Your Tools........................................................................................................3
2. Computer Architecture .....................................................................................7
Structure of Computer Memory.......................................................................7
The CPU...........................................................................................................9
Some Terms ...................................................................................................11
Interpreting Memory......................................................................................13
Data Accessing Methods................................................................................14
Review ...........................................................................................................16
3. Your First Programs........................................................................................19
Entering in the Program.................................................................................19
Outline of an Assembly Language Program..................................................22
Planning the Program.....................................................................................28
Finding a Maximum Value.............................................................................31
Addressing Modes .........................................................................................41
Review ...........................................................................................................45
4. All About Functions.........................................................................................49
Dealing with Complexity...............................................................................49
How Functions Work .....................................................................................50
Assembly-Language Functions using the C Calling Convention ..................52
A Function Example ......................................................................................59
Recursive Functions.......................................................................................64
Review ...........................................................................................................71
5. Dealing with Files.............................................................................................75
The UNIX File Concept.................................................................................75
Buffers and .bss ...........................................................................................76
Standard and Special Files.............................................................................78
Using Files in a Program................................................................................79
iii
Review ...........................................................................................................93
6. Reading and Writing Simple Records ...........................................................95
Writing Records...........................................................................................100
Reading Records..........................................................................................104
Modifying the Records ................................................................................111
Review .........................................................................................................114
7. Developing Robust Programs........................................................................117
Where Does the Time Go?...........................................................................117
Some Tips for Developing Robust Programs...............................................118
Handling Errors Effectively.........................................................................121
Making Our Program More Robust.............................................................123
Review .........................................................................................................126
8. Sharing Functions with Code Libraries.......................................................129
Using a Shared Library................................................................................130
How Shared Libraries Work ........................................................................133
Finding Information about Libraries............................................................134
Useful Functions..........................................................................................140
Building a Shared Library............................................................................141
Review .........................................................................................................143
9. Intermediate Memory Topics........................................................................147
How a Computer Views Memory ................................................................147
The Memory Layout of a Linux Program....................................................149
Every Memory Address is a Lie ..................................................................151
Getting More Memory.................................................................................155
A Simple Memory Manager ........................................................................157
Using our Allocator......................................................................................174
More Information.........................................................................................177
Review .........................................................................................................178
iv
10. Counting Like a Computer .........................................................................181
Counting.......................................................................................................181
Truth, Falsehood, and Binary Numbers.......................................................186
The Program Status Register .......................................................................195
Other Numbering Systems...........................................................................196
Octal and Hexadecimal Numbers ................................................................199
Order of Bytes in a Word.............................................................................201
Converting Numbers for Display.................................................................204
Review .........................................................................................................210
11. High-Level Languages.................................................................................213
Compiled and Interpreted Languages..........................................................213
Your First C Program...................................................................................215
Perl...............................................................................................................218
Python ..........................................................................................................219
Review .........................................................................................................220
12. Optimization.................................................................................................223
When to Optimize........................................................................................223
Where to Optimize.......................................................................................224
Local Optimizations.....................................................................................225
Global Optimization.....................................................................................229
Review .........................................................................................................230
13. Moving On from Here .................................................................................233
From the Bottom Up....................................................................................234
From the Top Down .....................................................................................234
From the Middle Out ...................................................................................235
Specialized Topics .......................................................................................236
Further Resources on Assembly Language..................................................237
v