Table Of ContentGuide to Assembly Language
Programming in Linux
Sivarama P. Dandamudi
Guide to Assembly Language
Programming in Linux
^ Sprriin ger
This eBook does not include ancillary media that was packaged with the
printed version of the book.
Sivarama P. Dandamudi
School of Computer Science
Carleton University
Ottawa, ON K1S5B6
Canada
sivarama@scs.carleton.ca
Library of Congress Cataloging-in-Publication Data
A CLP. Catalogue record for this book is available
from the Library of Congress.
ISBN-10: 0-387-25897-3 (SC) ISBN-10: 0-387-26171-0 (e-book)
ISBN-13: 978-0387-25897-3 (SC) ISBN-13: 978-0387-26171-3 (e-book)
Printed on acid-free paper.
© 2005 Springer Science+Business Media, Inc.
All rights reserved. This work may not be translated or copied in whole or in part
without the written permission of the publisher (Springer Science+Business
Media, Inc., 233 Spring Street, New York, NY 10013, USA), except for brief
excerpts in connection with reviews or scholarly analysis. Use in connection with
any form of information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter
developed is forbidden.
The use in this publication of trade names, trademarks, service marks and similar
terms, even if they are not identified as such, is not to be taken as an expression of
opinion as to whether or not they are subject to proprietary rights.
Printed in the United States of America.
9 8 7 6 5 4 3 21 SPIN 11302087
springeronline.com
To
my parents, Subba Rao and Prameela Rani,
my wife, Sobha,
and
my daughter, Veda
Preface
The primary goal of this book is to teach the IA-32 assembly language programming under
the Linux operating system. A secondary objective is to provide a gende introduction to the
Fedora Linux operating system. Linux has evolved substantially since its first appearance in
1991. Over the years, its popularity has grown as well. According to an estimate posted on
http : //counter. li . org/, there are about 18 million Linux users worldwide. Hopefully,
this book encourages even more people to switch to Linux.
The book is self-contained and provides all the necessary background information. Since
assembly language is very closely linked to the underlying processor architecture, a part of the
book is dedicated to giving computer organization details. In addition, the basics of Linux are
introduced in a separate chapter. These details are sufficient to work with the Linux operation
system.
The reader is assumed to have had some experience in a structured, high-level language such
as C. However, the book does not assume extensive knowledge of any high-level language—only
the basics are needed.
Approach and Level of Presentation
The book is targeted for software professionals who would like to move to Linux and get a com
prehensive introduction to the IA-32 assembly language. It provides detailed, step-by-step instruc
tions to install Linux as the second operating system.
No previous knowledge of Linux is required. The reader is introduced to Linux and its com
mands. Four chapters are dedicated to Linux and NASM assembler (installation and usage). The
accompanying DVD-ROMs provide the necessary software to install the Linux operating system
and learn assembly language programming.
The assembly language is presented from the professional viewpoint. Since most professionals
are full-time employees, the book takes their time constraints into consideration in presenting the
material.
viii Preface
Summary of Special Features
Here is a summary of the special features that sets this book apart:
• The book includes the Red Hat Fedora Core 3 Linux distribution (a total of two DVD-ROMs
are included with the book). Detailed step-by-step instructions are given to install Linux on
a Windows machine. A complete chapter is used for this purpose, with several screenshots
to help the reader during the installation process.
• Free NASM assembler is provided so that the readers can get hands-on assembly language
programming experience.
• Special I/O software is provided to simplify assembly language programming. A set of input
and output routines is provided so that the reader can focus on writing assembly language
programs rather than spending time in understanding how the input and output are done
using the basic I/O functions provided by the operating system.
• Three chapters are included on computer organization. These chapters provide the necessary
background to program in the assembly language.
• Presentation of material is suitable for self-study. To facilitate this, extensive programming
examples and figures are used to help the reader grasp the concepts. Each chapter contains
a simple programming example in "Our First Program" section to gently introduce the con
cepts discussed in the chapter. This section is typically followed by "Illustrative Examples"
section, which gives more programming examples.
• This book does not use fragments of code in examples. All examples are complete in
the sense that they can be assembled and run, giving a better feeling as to how these pro
grams work. These programs are on the accompanying DVD-ROM (DVD 2). In addition,
you can also download these programs from the book's Web site at the following URL:
http://www.scs.carleton.ca/~sivarama/linux_book.
• Each chapter begins with an overview and ends with a summary.
Overview of the Book
The book is divided into seven parts. Part I provides introduction to the assembly language and
gives reasons for programming in the assembly language. Assembly language is a low-level lan
guage. To program in the assembly language, you should have some basic knowledge about the
underlying processor and system organization. Part II provides this background on computer orga
nization. Chapter 2 introduces the digital logic circuits. The next chapter gives details on memory
organization. Chapter 4 describes the Intel IA-32 architecture.
Part III covers the topics related to Linux installation and usage. Chapter 5 gives detailed
information on how you can install the Fedora Core Linux provided on the accompanying DVD-
ROMs. It also explains how you can make your system dual bootable so that you can select the
operating system (Windows or Linux) at boot time. Chapter 6 gives a brief introduction to the
Linux operating system. It gives enough details so that you feel comfortable using the Linux
operating system. If you are familiar with Linux, you can skip this chapter.
Part IV also consists of two chapters. It deals with assembling and debugging assembly lan
guage programs. Chapter 7 gives details on the NASM assembler. It also describes the I/O routines
developed by the author to facilitate assembly language programming. The next chapter looks at
the debugging aspect of program development. We describe the GNU debugger (gdb), which
is a command-line debugger. This chapter also gives details on Data Display Debugger (DDD),
Preface ix
which is a nice graphical front-end for gdb. Both debuggers are included on the accompanying
DVD-ROMs.
After covering the setup and usage details of Linux and NASM, we look at the assembly lan
guage in Part V. This part introduces the basic instructions of the assembly language. To facilitate
modular program development, we introduce procedures in the third chapter of this part. The re
maining chapters describe the addressing modes and other instructions that are commonly used in
assembly language programs.
Part VI deals with advanced assembly language topics. It deals with topics such as string
processing, recursion, floating-point operations, and interrupt processing. In addition. Chapter 21
explains how you can interface with high-level languages. By using C, we explain how you can call
assembly language procedures from C and vice versa. This chapter also discusses how assembly
language statements can be embedded into high-level language code. This process is called inline
assembly. Again, by using C, this chapter shows how inline assembly is done under Linux.
The last part consists of five appendices. These appendices give information on number sys
tems and character representation. In addition, Appendix D gives a summary of the IA-32 instruc
tion set. A comprehensive glossary is given in Appendix E.
Acknowledgments
I want to thank Wayne Wheeler, Editor and Ann Kostant, Executive Editor at Springer for suggest
ing the project. I am also grateful to Wayne for seeing the project through.
My wife Sobha and daughter Veda deserve my heartfelt thanks for enduring my preoccupation
with this project! I also thank Sobha for proofreading the manuscript. She did an excellen tjob!
I also express my appreciation to the School of Computer Science at Carleton University for
providing a great atmosphere to complete this book.
Feedback
Works of this nature are never error-free, despite the best efforts of the authors and others involved
in the project. I welcome your comments, suggestions, and corrections by electronic mail.
Ottawa, Canada Sivarama P. Dandamudi
January 2005 sivarama@scs . carleton. ca
http://www.scs.carleton.ca/~sivarama
Contents
Preface vii
PART I Overview 1
1 Assembly Language 3
Introduction 3
What Is Assembly Language? 5
Advantages of High-Level Languages 6
Why Program in Assembly Language? 7
Typical Applications 8
Summary 8
PART II Computer Organization 9
2 Digital Logic Circuits 11
Introduction 11
Simple Logic Gates 13
Logic Functions 15
Deriving Logical Expressions 17
Simplifying Logical Expressions 18
Combinational Circuits 23
Adders 26
Programmable Logic Devices 29
Arithmetic and Logic Units 32
Sequential Circuits 35
Latches 37
Flip-Flops 39
Summary 43
3 Memory Organization 45
Introduction 45
Basic Memory Operations 46
Types of Memory 48
Building a Memory Block 50
xii Contents
Building Larger Memories 52
Mapping Memory 56
Storing Multibyte Data 58
Alignment of Data 59
Summary 60
4 The IA-32 Architecture 61
Introduction 61
Processor Execution Cycle 63
Processor Registers 63
Protected Mode Memory Architecture 67
Real Mode Memory Architecture 72
Mixed-Mode Operation 74
Which Segment Register to Use 75
Input/Output 76
Summary 78
PART III Linux 79
5 Installing Linux 81
Introduction 81
Partitioning Your Hard Disk 82
Installing Fedora Core Linux 92
Installing and Removing Software Packages 107
Mounting Windows File System 110
Summary 112
Getting Help 114
6 Using Linux 115
Introduction 115
Setting User Preferences 117
System Settings 123
Working with the GNOME Desktop 126
Command Terminal 132
Getting Help 134
Some General-Purpose Commands 135
File System 139
Access Permissions 141
Redirection 145
Pipes 146
Editing Files with Vim 147
Summary 149
PART IV NASM 151
7 Installing and Using NASM 153
Introduction 153
Installing NASM 154