G u i d e to A s s e m b l y L a n g u a g e
P r o g r a m m i n g in L i n u x
Sivarama P. Dandamudi
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.
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 language.
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 organization.
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 DVDROMs.
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 language
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),
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 language
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 remaining
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 systems
and character representation. In addition, Appendix D gives a summary of the IA-32 instruction
set. A comprehensive glossary is given in Appendix E.
Introduction
Assembly Language
The main objective of this chapter is to give you a brief introduction to the assembly language. To achieve this goal, we compare and contrast the assembly language with high-level languages you are familiar with. This comparison enables us to take a look at the pros and cons of the assembly language vis-a-vis high-level languages.
Approach and Level of Presentation
The book is targeted for software professionals who would like to move to Linux and get a comprehensive introduction to the IA-32 assembly language. It provides detailed, step-by-step instructions to install Linux as the second operating system.
No previous knowledge of Linux is required. The reader is introduced to Linux and its commands.
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.
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 concepts
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 programs
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.
Product details
Price
|
|
---|---|
File Size
| 31,398 KB |
Pages
|
539 p |
File Type
|
PDF format |
ISBN-10
ISBN-13 | 0-387-26171-0 978-0387-26171-3 |
Copyright
| 2005 Springer Science +Business Media, Inc |
Table of Contents
Preface vii
PART I Overview
1 Assembly Language
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
2 Digital Logic Circuits
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
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
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
5 Installing Linux
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
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
7 Installing and Using NASM
Introduction 153
Installing NASM 154
Contents xiii
Generating the Executable File 154
Assembly Language Template 155
Input/Output Routines 156
An Example Program 159
Assembling and Linking 160
Summary 166
Web Resources 166
8 Debugging Assembly Language Programs
Strategies to Debug Assembly Language Programs 167
Preparing Your Program 169
GNU Debugger 170
Data Display Debugger 179
Summary 184
PART V Assembly Language
9 A First Look at Assembly Language
Introduction 187
Data Allocation 188
Where Are the Operands 193
Overview of Assembly Language Instructions 196
Our First Program 205
Illustrative Examples 206
Summary 209
10 More on Assembly Language
Introduction 211
Data Exchange and Translate Instructions 212
Shift and Rotate Instructions 213
Defining Constants 217
Macros 218
Our First Program 221
Illustrative Examples 223
When to Use the XLAT Instruction 227
Summary 229
11 Writing Procedures
Introduction 231
What Is a Stack? 233
Implementation of the Stack 234
Stack Operations 236
Uses of the Stack 238
Procedure Instructions 239
Our First Program 241
Parameter Passing 242
Illustrative Examples 248
Summary 252
xiv Contents
12 More on Procedures
Introduction 255
Local Variables 256
Our First Program 257
Multiple Source Program Modules 260
Illustrative Examples 261
Procedures with Variable Number of Parameters 268
Summary 272
13 Addressing Modes
Introduction 273
Memory Addressing Modes 274
Arrays 278
Our First Program 281
Illustrative Examples 282
Summary 289
14 Arithmeticlnstructions
Introduction 291
Status Flags 292
Arithmetic Instructions 302
Our First Program 309
Illustrative Examples 310
Summary 316
15 Conditional Execution
Introduction 317
Unconditional Jump 318
Compare Instruction 321
Conditional Jumps 322
Looping Instructions 327
Our First Program 328
Illustrative Examples 330
Indirect Jumps 335
Summary 339
16 Logical and Bit Operations
Introduction 341
Logical Instructions 342
Shift Instructions 347
Rotate Instructions 353
Bit Instructions 354
Our First Program 355
Illustrative Examples 357
Summary 360
Contents xv
PART VI Advanced Assembly Language
17 String Processing
String Representation 363
String Instructions 364
Our First Program 372
Illustrative Examples 373
Testing String Procedures 376
Summary 378
18 ASCII and BCD Arithmetic
Introduction 379
Processing in ASCII Representation 381
Our First Program 384
Processing Packed BCD Numbers 385
Illustrative Example 387
Decimal Versus Binary Arithmetic 389
Summary 390
19 Recursion
Introduction 391
Our First Program 392
Illustrative Examples 394
Recursion Versus Iteration 400
Summary 401
20 Protected-Mode Interrupt Processing
Introduction 403
A Taxonomy of Interrupts 404
Interrupt Processing in the Protected Mode 405
Exceptions 408
Software Interrupts 410
File I/O 411
Our First Program 415
Illustrative Examples 415
Hardware Interrupts 418
Direct Control of I/O Devices 419
Summary 420
21 High-Level Language Interface
Introduction 423
Calling Assembly Procedures from C 424
Our First Program 427
Illustrative Examples 428
Calling C Functions from Assembly . 432
Inline Assembly 434
Summary 441
xvi Contents
22 Floating-Point Operations
Introduction 443
Floating-Point Unit Organization 444
Floating-Point Instructions 447
Our First Program 453
Illustrative Examples 455
Summary 458
APPENDICES
A Number Systems
Positional Number Systems 461
Conversion to Decimal 463
Conversion from Decimal 463
Binary/Octal/Hexadecimal Conversion 464
Unsigned Integers 466
Signed Integers 466
Floating-Point Representation 469
Summary 471
B Character Representation
Character Representation 473
ASCII Character Set 474
C Programming Exercises
D IA-32 Instruction Set
Instruction Format 485
Selected Instructions 487
E Glossary
Index 527
●▬▬▬▬▬❂❂❂▬▬▬▬▬●
●▬▬❂❂▬▬●
●▬❂▬●
●❂●