
x64 Assembly Language Step-by-Step
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
In the newly revised fourth edition of x64 Assembly Language Step-by-Step: Programming with Linux, author Jeff Duntemann delivers an extensively rewritten introduction to assembly language with a strong focus on 64-bit long-mode Linux assembler. The book offers a lighthearted, robust, and accessible approach to a challenging technical discipline, giving you a step-by-step path to learning assembly code that's engaging and easy to read.
x64 Assembly Language Step-by-Step makes quick work of programmable computing basics, the concepts of binary and hexadecimal number systems, the Intel x86/x64 computer architecture, and the process of Linux software development to dive deep into the x64 instruction set, memory addressing, procedures, macros, and interface to the C-language code libraries on which Linux is built.
You'll also find:
* A set of free and open-source development and debugging tools you can download and put to use immediately
* Numerous examples woven throughout the book to illustrate the practical implementation of the ideas discussed within
* Practical tips on software design, coding, testing, and debugging
A one-stop resource for aspiring and practicing Intel assembly programmers, the latest edition of this celebrated text provides readers with an authoritative tutorial approach to x64 technology that's ideal for self-paced instruction.
More details
Other editions
Additional editions

Person
JEFF DUNTEMANN is an accomplished author of both science fiction and technical nonfiction. He has published dozens of technical articles in magazines, co-founded The Coriolis Group, a large publishing house in Arizona, and works as a technical publishing consultant. He owns Copperwood Press, a POD imprint hosted on amazon.com.
Content
Introduction xxix
Chapter 1 It's All in the Plan 1
Another Pleasant Valley Saturday 1
Had This Been the Real Thing 5
Assembly Language Programming As a Square Dance 5
Assembly Language Programming As a Board Game 6
Chapter 2 Alien Bases 11
The Return of the New Math Monster 11
Octal: How the Grinch Stole Eight and Nine 16
Hexadecimal: Solving the Digit Shortage 20
From Hex to Decimal and from Decimal to Hex 24
Practice. Practice! PRACTICE! 27
Arithmetic in Hex 28
Binary 34
Hexadecimal as Shorthand for Binary 38
Prepare to Compute 40
Chapter 3 Lifting the Hood 41
RAXie, We Hardly Knew Ye 41
Switches, Transistors, and Memory 43
The Shop Supervisor and the Assembly Line 54
The Box That Follows a Plan 58
What vs. How: Architecture and Microarchitecture 63
Enter the Plant Manager 67
Chapter 4 Location, Location, Location 73
The Joy of Memory Models 73
The Nature of Segments 80
Segment Registers 87
The Four Major Assembly Programming Models 95
64-Bit Long Mode 101
Chapter 5 The Right to Assemble 103
The Nine and Sixty Ways to Code 103
Files and What's Inside Them 104
Text In, Code Out 115
The Assembly Language Development Process 123
Linking the Object Code File 130
Taking a Trip Down Assembly Lane 134
Chapter 6 A Place to Stand, with Access to Tools 143
Integrated Development Environments 143
Introducing SASM 146
Linux and Terminals 153
Using Linux Make 164
Debugging with SASM 172
Chapter 7 Following Your Instructions 175
Build Yourself a Sandbox 176
Instructions and Their Operands 178
Source and Destination Operands 178
Rally Round the Flags, Boys! 186
Signed and Unsigned Values 195
Implicit Operands and MUL 200
Reading and Using an Assembly Language Reference 205
NEG Negate (Two's Complement; i.e., Multiply by -1) 208
Chapter 8 Our Object All Sublime 213
The Bones of an Assembly Language Program 213
Last In, First Out via the Stack 223
Using Linux Kernel Services Through Syscall 231
Designing a Nontrivial Program 235
Going Further 248
Chapter 9 Bits, Flags, Branches, and Tables 251
Bits Is Bits (and Bytes Is Bits) 251
Shifting Bits 258
Bit-Bashing in Action 262
Flags, Tests, and Branches 270
X64 Long Mode Memory Addressing in Detail 279
Character Table Translation 290
Tables Instead of Calculations 298
Chapter 10 Dividing and Conquering 299
Boxes within Boxes 300
Calling and Returning 309
Local Labels and the Lengths of Jumps 325
Building External Procedure Libraries 330
The Art of Crafting Procedures 352
Simple Cursor Control in the Linux Console 356
Creating and Using Macros 364
Chapter 11 Strings and Things 377
The Notion of an Assembly Language String 378
REP STOSB, the Software Machine Gun 387
The Semiautomatic Weapon: STOSB Without REP 392
MOVSB: Fast Block Copies 397
Storing Data to Discontinuous Strings 402
Command-Line Arguments, String Searches, and the Linux Stack 408
The Stack, Its Structure, and How to Use It 414
Chapter 12 Heading Out to C 423
What's GNU? 424
Linking to the Standard C Library 429
Formatted Text Output with printf() 438
Data In with fgets() and scanf() 442
Be a Linux Time Lord 448
Understanding AT&T Instruction Mnemonics 456
Generating Random Numbers 460
How C Sees Command-Line Arguments 472
Simple File I/O 474
Conclusion: Not the End, But Only the Beginning 489
Appendix A The Return of the Insight Debugger 493
Insight's Shortcomings 494
Opening a Program Under Insight 495
Setting Command-Line Arguments with Insight 496
Running and Stepping a Program 496
The Memory Window 497
Showing the Stack in Insight's Memory View 498
Examining the Stack with Insight's Memory View 498
Learn gdb! 500
Appendix B Partial x64 Instruction Reference 501
What's Been Removed from x64 502
Flag Results 502
Size Specifiers 503
Instruction Index 505
ADC: Arithmetic Addition with Carry 507
ADD: Arithmetic Addition 509
AND: Logical AND 511
BT: Bit Test 513
CALL: Call Procedure 515
CLC: Clear Carry Flag (CF) 517
CLD: Clear Direction Flag (DF) 518
CMP: Arithmetic Comparison 519
DEC: Decrement Operand 521
DIV: Unsigned Integer Division 522
INC: Increment Operand 524
J??: Jump If Condition Is Met 525
JECXZ: Jump if ECX=0 527
JRCXZ: Jump If RCX=0 528
JMP: Unconditional Jump 529
LEA: Load Effective Address 531
LOOP: Loop Until CX/ECX/RCX=0 532
LOOPNZ/LOOPNE: Loop Until CX/ECX/RCX=0 and ZF=0 534
LOOPZ/LOOPE: Loop Until CX/ECX/RCX=0 and ZF=1 535
MOV: Copy Right Operand into Left Operand 536
MOVS: Move String 538
MOVSX: Copy with Sign Extension 540
MUL: Unsigned Integer Multiplication 542
NEG: Negate (Two's Complement; i.e., Multiply by -1) 544
NOP: No Operation 546
NOT: Logical NOT (One's Complement) 547
OR: Logical OR 548
POP: Copy Top of Stack into Operand 550
POPF/D/Q: Copy Top of Stack into Flags Register 552
PUSH: Push Operand onto Top of Stack 553
PUSHF/D/Q: Push Flags Onto the Stack 555
RET: Return from Procedure 556
ROL/ROR: Rotate Left/Rotate Right 558
SBB: Arithmetic Subtraction with Borrow 560
SHL/SHR: Shift Left/Shift Right 562
STC: Set Carry Flag (CF) 564
STD: Set Direction Flag (DF) 565
STOS/B/W/D/Q: Store String 566
SUB: Arithmetic Subtraction 568
SYSCALL: Fast System Call into Linux 570
XCHG: Exchange Operands 571
XLAT: Translate Byte Via Table 572
XOR: Exclusive OR 573
Appendix C Character Set Charts 575
Index 579
Introduction
"Why Would You Want to Do That?"
It was 1985, and I was in a chartered bus in New York City, heading for a press reception with a bunch of other restless media egomaniacs. I was only beginning my tech journalist career (as technical editor for PC Tech Journal), and my first book was still months in the future. I happened to be sitting next to an established programming writer/guru, with whom I was impressed and to whom I was babbling about one thing or another. I would like to eliminate this statement; it adds little to the book, and as annoying as he is, even though we don't name him, I now understand why he's so annoying: He lives and works in a completely different culture than I do.
During our chat, I happened to let slip that I was a Turbo Pascal fanatic, and what I really wanted to do was learn how to write Turbo Pascal programs that made use of the brand new Microsoft Windows user interface. He wrinkled his nose and grimaced wryly, before speaking the Infamous Question:
"Why would you want to do that?"
I had never heard the question before (though I would hear it many times thereafter), and it took me aback. Why? Because, well, because.I wanted to know how it worked.
"Heh. That's what C is for."
Further discussion got me nowhere in a Pascal direction. But some probing led me to understand that you couldn't write Windows apps in Turbo Pascal. It was impossible. Or.the programming writer/guru didn't know how. Maybe both. I never learned the truth as it stood in 1985. (Delphi answered the question once and for all in 1995.) But I did learn the meaning of the Infamous Question.
Note well: When somebody asks you, "Why would you want to do that?" what it really means is this: "You've asked me how to do something that is either impossible using tools that I favor or completely outside my experience, but I don't want to lose face by admitting it. So.how 'bout those Blackhawks?"
I heard it again and again over the years:
- Q: How can I set up a C string so that I can read its length without scanning it?
- A: Why would you want to do that?
- Q: How can I write an assembly language subroutine callable from Turbo Pascal?
- A: Why would you want to do that?
- Q: How can I write Windows apps in assembly language?
- A: Why would you want to do that?
You get the idea. The answer to the Infamous Question is always the same, and if the weasels ever ask it of you, snap back as quickly as possible: because I want to know how it works.
That is a completely sufficient answer. It's the answer I've used every single time, except for one occasion a considerable number of years ago, when I put forth that I wanted to write a book that taught people how to program in assembly language as their first experience in programming.
- Q: Good grief, why would you want to do that?
- A: Because it's the best way there is to build the skills required to understand how all the rest of the programming universe works.
Being a programmer is one thing above all else: It is understanding how things work. Learning to be a programmer, furthermore, is almost entirely a process of learning how things work. This can be done at various levels, depending on the tools you're using. If you're programming in Visual Basic, you have to understand how certain things work, but those things are by and large confined to Visual Basic itself. A great deal of machinery is hidden by the layer that Visual Basic places between the programmer and the computer. (The same is true of Delphi, Lazarus, Java, Python, and many other very high-level programming environments.) If you're using a C compiler, you're a lot closer to the machine, so you see a lot more of that machinery-and must, therefore, understand how it works to be able to use it. However, quite a bit remains hidden, even from the hardened C programmer.
If, on the other hand, you're working in assembly language, you're as close to the machine as you can get. Assembly language hides nothing, and withholds no power. The flipside, of course, is that no magical layer between you and the machine will absolve any ignorance and "take care of" things for you. If you don't understand how something works, you're dead in the water-unless you know enough to be able to figure it out on your own.
That's a key point: My goal in creating this book is not entirely to teach you assembly language per se. If this book has a prime directive at all, it is to impart a certain disciplined curiosity about the underlying machine, along with some basic context from which you can begin to explore the machine at its very lowest levels-that, and the confidence to give it your best shot. This is difficult stuff, but it's nothing you can't master given some concentration, patience, and the time it requires-which, I caution, may be considerable.
In truth, what I'm really teaching you here is how to learn.
What You'll Need
To program as I intend to teach, you're going to need a 64-bit Intel computer running a 64-bit distribution of Linux. The one I used in preparing this book is Linux Mint Cinnamon V20. 3 Una. "Una" here is a code name for this version of Linux Mint. It's nothing more than a short way of saying "Linux Mint 20.3." I recommend Mint; it's thrown me fewer curves than any other distro I've ever used-and I've used Linux here and there ever since it first appeared. I don't think which graphical shell you use matters a great deal. I like Cinnamon, but you can use whatever you like or are familiar with.
You need to be reasonably proficient with Linux at the user level. I can't teach you how to install, configure, and run Linux in this book. If you're not already familiar with Linux, get a tutorial text and work through it. There are many such online.
You'll need a piece of free software called SASM, which is a simple interactive development environment (IDE) for programming in assembly. Basically, it consists of an editor, a build system, and a front end to the standard Linux debugger gdb. You'll also need a free assembler called NASM.
You don't have to know how to download, install, and configure these tools in advance because, at the appropriate times, I'll cover all necessary tool installation and configuration.
Do note that other Unix implementations not based on the Linux kernel may not function precisely the same way under the hood. BSD Unix uses different conventions for making system calls, for example, and other Unix versions like Solaris are outside my experience.
Remember that this book is about the x64 architecture. To the extent that x64 contains x86, I will also be teaching elements of the x86 architecture. The gulf between 32-bit x86 and 64-bit x64 is a lot narrower than the gulf between 16-bit x86 and 32-bit x86. If you already have a firm grounding in 32-bit x86, you'll breeze through most of this book at a gallop. If you can do that, cool-just please remember that the book is for those who are just starting out in programming on Intel CPUs.
Also remember that this book is limited in size by its publisher: Paper, ink, and cover stock aren't free. That means I have to narrow the scope of what I teach and explain within those limits. I wish I had the space to cover the AVX math subsystem. I don't. But I'll bet that once you go through this book, you can figure much of it out by yourself.
The Master Plan
This book starts at the beginning, and I mean the beginning. Maybe you're already there, or well past it. I respect that. I still think that it wouldn't hurt to start at the first chapter and read through all the chapters in order. Review is useful, and hey-you may realize that you didn't know quite as much as you thought you did. (Happens to me all the time!)
But if time is at a premium, here's the cheat sheet:
- If you already understand the fundamental ideas of computer programming, skip Chapter 1.
- If you already understand the ideas behind number bases other than decimal (especially hexadecimal and binary), skip Chapter 2.
- If you already have a grip on the nature of computer internals (memory, CPU architectures, and so on) skip Chapter 3.
- If you already understand x64 memory addressing, skip Chapter 4.
- No. Stop. Scratch that. Even if you already understand x64 memory addressing, read Chapter 4.
The last bullet is there, and emphatic, for a reason: Assembly language programming is about memory addressing. If you don't understand memory addressing, nothing else you learn in assembly will help you one.bit. So, don't skip Chapter 4 no matter what else you know or think you know. Start from there, and see it through to the end. Memory addressing comes up regularly throughout the rest of the book. It's really the heart of the topic.
Load every example program, assemble each one, and run them all. Strive to understand every single line in every program. Take nothing on faith. Furthermore, don't stop there. Change the example programs as things...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.