Introduction

This book is the culmination of 30 years’ work. The very earliest versions of this book were notes I copied for my students at Cal Poly Pomona and UC Riverside under the title “How to Program the IBM PC Using 8088 Assembly Language.” I had lots of input from students and a good friend of mine, Mary Philips, that softened the edges a bit. Bill Pollock rescued that early version from obscurity on the internet, and with the help of Karol Jurado, the first edition of The Art of Assembly Language became a reality in 2003.

Thousands of readers (and suggestions) later, along with input from Bill Pollock, Alison Peterson, Ansel Staton, Riley Hoffman, Megan Dunchak, Linda Recktenwald, Susan Glinert Stevens, and Nancy Bell at No Starch Press (and a technical review by Nathan Baker), the second edition of this book arrived in 2010.

Ten years later, The Art of Assembly Language (or AoA as I refer to it) was losing popularity because it was tied to the 35-year-old 32-bit design of the Intel x86. Today, someone who was going to learn 80x86 assembly language would want to learn 64-bit assembly on the newer x86-64 CPUs. So in early 2020, I began the process of translating the old 32-bit AoA (based on the use of the High-Level Assembler, or HLA) to 64 bits by using the Microsoft Macro Assembler (MASM).

When I first started the project, I thought I’d translate a few HLA programs to MASM, tweak a little text, and wind up with The Art of 64-Bit Assembly with minimal effort. I was wrong. Between the folks at No Starch Press wanting to push the envelope on readability and understanding, and the incredible job Tony Tribelli has done in his technical review of every line of text and code in this book, this project turned out to be as much work as writing a new book from scratch. That’s okay; I think you’ll really appreciate the work that has gone into this book.

A Note About the Source Code in This Book

A considerable amount of x86-64 assembly language (and C/C++) source code is presented throughout this book. Typically, source code comes in three flavors: code snippets, single assembly language procedures or functions, and full-blown programs.

Code snippets are fragments of a program; they are not stand-alone, and you cannot compile (assemble) them using MASM (or a C++ compiler in the case of C/C++ source code). Code snippets exist to make a point or provide a small example of a programming technique. Here is a typical example of a code snippet you will find in this book:

someConst = 5
   .
   .
   .
mov eax, someConst

The vertical ellipsis (. . .) denotes arbitrary code that could appear in its place (not all snippets use the ellipsis, but it’s worthwhile to point this out).

Assembly language procedures are also not stand-alone code. While you can assemble many assembly language procedures appearing in this book (by simply copying the code straight out of the book into an editor and then running MASM on the resulting text file), they will not execute on their own. Code snippets and assembly language procedures differ in one major way: procedures appear as part of the downloadable source files for this book (at https://artofasm.randallhyde.com/).

Full-blown programs, which you can compile and execute, are labeled as listings in this book. They have a listing number/identifier of the form “Listing C-N,” where C is the chapter number and N is a sequentially increasing listing number, starting at 1 for each chapter. Here is an example of a program listing that appears in this book:

; Listing 1-3

; A simple MASM module that contains
; an empty function to be called by
; the C++ code in Listing 1-2.

        .CODE
        
; The "option casemap:none" statement
; tells MASM to make all identifiers
; case-sensitive (rather than mapping
; them to uppercase). This is necessary
; because C++ identifiers are case-
; sensitive.

        option  casemap:none

; Here is the "asmFunc" function.

        public  asmFunc
asmFunc PROC

; Empty function just returns to C++ code.
        
        ret     ; Returns to caller
        
asmFunc ENDP
        END

Listing 1: A MASM program that the C++ program in Listing 1-2 calls

Like procedures, all listings are available in electronic form at my website: https://artofasm.randallhyde.com/. This link will take you to the page containing all the source files and other support information for this book (such as errata, electronic chapters, and other useful information). A few chapters attach listing numbers to procedures and macros, which are not full programs, for legibility purposes. A couple of listings demonstrate MASM syntax errors or are otherwise unrunnable. The source code still appears in the electronic distribution under that listing name.

Typically, this book follows executable listings with a build command and sample output. Here is a typical example (user input is given in a boldface font):

C:\>build listing4-7

C:\>echo off
 Assembling: listing4-7.asm
c.cpp

C:\>listing4-7
Calling Listing 4-7:
aString: maxLen:20, len:20, string data:'Initial String Data'
Listing 4-7 terminated

Most of the programs in this text run from a Windows command line (that is, inside the cmd.exe application). By default, this book assumes you’re running the programs from the root directory on the C: drive. Therefore, every build command and sample output typically has the text prefix C:\> before any command you would type from the keyboard on the command line. However, you can run the programs from any drive or directory.

If you are completely unfamiliar with the Windows command line, please take a little time to learn about the Windows command line interpreter (CLI). You can start the CLI by executing the cmd.exe program from the Windows run command. As you’re going to be running the CLI frequently while reading this book, I recommend creating a shortcut to cmd.exe on your desktop. In Appendix C, I describe how to create this shortcut to automatically set up the environment variables you will need to easily run MASM (and the Microsoft Visual C++ compiler). Appendix D provides a quick introduction to the Windows CLI for those who are unfamiliar with it.