x64 Assembly Language Step-by-Step: Programming with Linux [4 ed.] 1394155247, 9781394155248, 9781394155545, 9781394155255

The long-awaited x64 edition of the bestselling introduction to Intel assembly language In the newly revised fourth edi

163 67 6MB

English Pages 640 [636] Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Title Page
Copyright Page
About the Author
Acknowledgments
Contents at a Glance
Contents
Introduction
Chapter 1 It’s All in the Plan: Understanding What Computers Really Do
Another Pleasant Valley Saturday
Steps and Tests
More Than Two Ways?
Computers Think Like Us
Had This Been the Real Thing . . .
Assembly Language Programming As a Square Dance
Assembly Language Programming As a Board Game
Code and Data
Addresses
Metaphor Check!
Chapter 2 Alien Bases: Getting Your Arms Around Binary and Hexadecimal
The Return of the New Math Monster
Counting in Martian
Dissecting a Martian Number
The Essence of a Number Base
Octal: How the Grinch Stole Eight and Nine
Who Stole Eight and Nine?
Hexadecimal: Solving the Digit Shortage
From Hex to Decimal and from Decimal to Hex
From Hex to Decimal
From Decimal to Hex
Practice. Practice! PRACTICE!
Arithmetic in Hex
Columns and Carries
Subtraction and Borrows
Borrows Across Multiple Columns
What’s the Point?
Binary
Values in Binary
Why Binary?
Hexadecimal as Shorthand for Binary
Prepare to Compute
Chapter 3 Lifting the Hood: Discovering What Computers Actually Are
RAXie, We Hardly Knew Ye
Gus to the Rescue
Switches, Transistors, and Memory
One If by Land...
Transistor Switches
The Incredible Shrinking Bit
Random Access
Memory Access Time
Bytes, Words, Double Words, and Quad Words
Pretty Chips All in a Row
The Shop Supervisor and the Assembly Line
Talking to Memory
Riding the Data Bus
The Shop Supervisor’s Pockets
The Assembly Line
The Box That Follows a Plan
Fetch and Execute
The Supervisor’s Innards
Changing Course
What vs. How: Architecture and Microarchitecture
Evolving Architectures
The Secret Machinery in the Basement
Enter the Plant Manager
Operating Systems: The Corner Office
BIOS: Software, Just Not as Soft
Multitasking Magic
Promotion to Kernel
The Core Explosion
The Plan
Chapter 4 Location, Location, Location: Registers, Memory Addressing, and Knowing Where Things Are
The Joy of Memory Models
16 Bits’ll Buy You 64 KB
The Nature of a Megabyte
Backward Compatibility and Virtual 86 Mode
16-BitBlinders
The Nature of Segments
A Horizon, Not a Place
Making 20-BitAddresses Out of 16-BitRegisters
Segment Registers
Segment Registers and x64
General-PurposeRegisters
Register Halves
The Instruction Pointer
The Flags Register
Math Coprocessors and Their Registers
The Four Major Assembly Programming Models
Real-ModeFlat Model
Real-ModeSegmented Model
32-BitProtected Mode Flat Model
64-BitLong Mode
Chapter 5 The Right to Assemble: The Process of Creating Assembly Language Programs
The Nine and Sixty Ways to Code
Files and What’s Inside Them
Binary vs. Text Files
Looking at Binary File Internals with the GHex Hex Editor
Interpreting Raw Data
“Endianness”
Text In, Code Out
Assembly Language
Comments
Beware “Write-Only” Source Code!
Object Code, Linkers, and Libraries
Relocatability
The Assembly Language Development Process
The Discipline of Working Directories
Editing the Source Code File
Assembling the Source Code File
Assembler Errors
Back to the Editor
Assembler Warnings
Linking the Object Code File
Linker Errors
Testing the EXE File
Errors vs. Bugs
Are We There Yet?
Debuggers and Debugging
Taking a Trip Down Assembly Lane
Installing the Software
Step 1: Edit the Program in an Editor
Step 2: Assemble the Program with NASM
Step 3: Link the Program with ld
Step 4: Test the Executable File
Step 5: Watch It Run in the Debugger
Chapter 6 A Place to Stand, with Access to Tools: Linux and the Tools That Shape the Way You Work
Integrated Development Environments
Introducing SASM
Configuring SASM
SASM’s Fonts
Using a Compiler to Link
A Quick Tour of SASM
SASM’s Editor
What SASM Demands of Your Code
Linux and Terminals
The Linux Console
Character Encoding in Konsole
The Three Standard Unix Files
I/O Redirection
Simple Text Filters
Using Standard Input and Standard Output from Inside SASM
Terminal Control with Escape Sequences
So Why Not GUI Apps?
Using Linux Make
Dependencies
When a File Is Up-to-Date
Chains of Dependencies
Invoking Make
Creating a Custom Key Binding for Make
Using Touch to Force a Build
Debugging with SASM
Pick up Your Tools. . .
Chapter 7 Following Your Instructions: Meeting Machine Instructions Up Close and Personal
Build Yourself a Sandbox
A Minimal NASM Program for SASM
Instructions and Their Operands
Source and Destination Operands
Immediate Data
Register Data
Memory Data and Effective Addresses
Confusing Data and Its Address
The Size of Memory Data
The Bad Old Days
Rally Round the Flags, Boys!
Flag Etiquette
Watching Flags from SASM
Adding and Subtracting One with INC and DEC
How Flags Change Program Execution
How to Inspect Variables in SASM
Signed and Unsigned Values
Two’s Complement and NEG
Sign Extension and MOVSX
Implicit Operands and MUL
MUL and the Carry Flag
Unsigned Division with DIV
MUL and DIV Are Slowpokes
Reading and Using an Assembly Language Reference
Memory Joggers for Complex Memories
An Assembly Language Reference for Beginners
Flags
NEG Negate (Two’s Complement; i.e., Multiply by 1)
Flags Affected
Legal Forms
Examples
Notes
Legal Forms
Operand Symbols
Examples
Notes
What’s Not Here. . .
Chapter 8 Our Object All Sublime: Creating Programs That Work
The Bones of an Assembly Language Program
The Initial Comment Block
The .data Section
The .bss Section
The .text Section
Labels
Variables for Initialized Data
String Variables
Deriving String Length with EQU and $
Last In, First Out via the Stack
Five Hundred Plates an Hour
Stacking Things Upside Down
Push-y Instructions
POP Goes the Opcode
PUSHA and POPA Are Gone
Pushing and Popping in Detail
Storage for the Short Term
Using Linux Kernel Services Through Syscall
X64 Kernel Services via the SYSCALL Instruction
ABI vs. API?
The ABI’s Register Parameter Scheme
Exiting a Program via SYSCALL
Which Registers Are Trashed by SysCall?
Designing a Nontrivial Program
Defining the Problem
Starting with Pseudocode
Successive Refinement
Those Inevitable “Whoops!” Moments
Scanning a Buffer
“Off by One” Errors
From Pseudocode to Assembly Code
The SASM Output Window Gotcha
Going Further
Chapter 9 Bits, Flags, Branches, and Tables: Easing Into Mainstream Assembly Coding
Bits Is Bits (and Bytes Is Bits)
Bit Numbering
“It’s the Logical Thing to Do, Jim. . .”
The AND Instruction
Masking Out Bits
The OR Instruction
The XOR Instruction
The NOT Instruction
Segment Registers Don’t Respond to Logic!
Shifting Bits
Shift by What?
How Bit Shifting Works
Bumping Bits into the Carry Flag
The Rotate Instructions
Rotating Bits Through the Carry Flag
Setting a Known Value into the Carry Flag
Bit-Bashing in Action
Splitting a Byte into Two Nybbles
Shifting the High Nybble into the Low Nybble
Using a Lookup Table
Multiplying by Shifting and Adding
Flags, Tests, and Branches
Unconditional Jumps
Conditional Jumps
Jumping on the Absence of a Condition
Flags
Comparisons with CMP
A Jungle of Jump Instructions
“Greater Than” Versus “Above”
Looking for 1-Bits with TEST
Looking for 0-Bits with BT
X64 Long Mode Memory Addressing in Detail
Effective Address Calculations
Displacements
The x64 Displacement Size Problem
Base Addressing
Base + Displacement Addressing
Base + Index Addressing
Index X Scale + Displacement Addressing
Other Addressing Schemes
LEA: The Top-Secret Math Machine
Character Table Translation
Translation Tables
Translating with MOV or with XLAT
Tables Instead of Calculations
Chapter 10 Dividing and Conquering: Using Procedures and Macros to Battle Program Complexity
Boxes within Boxes
Procedures as Boxes for Code
Calling and Returning
Calls Within Calls
The Dangers of Accidental Recursion
A Flag Etiquette Bug to Beware Of
Procedures and the Data They Need
Saving the Caller’s Registers
Preserving Registers Across Linux System Calls
PUSHAD and POPAD Are Gone
Local Data
Placing Constant Data in Procedure Definitions
More Table Tricks
Local Labels and the Lengths of Jumps
“Forcing” Local Label Access
Short, Near, and Far Jumps
Building External Procedure Libraries
When Tools Reach Their Limits
Using Include Files in SASM
Where SASM’s Include Files Must Be Stored
The Best Way to Create an Include File Library
Separate Assembly and Modules
Global and External Declarations
The Mechanics of Globals and Externals
Linking Libraries into Your Programs
The Dangers of Too Many Procedures and Too Many Libraries
The Art of Crafting Procedures
Maintainability and Reuse
Deciding What Should Be a Procedure
Use Comment Headers!
Simple Cursor Control in the Linux Console
Listing 10.6eattermgcc.asm
Console Control Cautions
Creating and Using Macros
The Mechanics of Macro Definition
Defining Macros with Parameters
The Mechanics of Invoking Macros
Local labels Within Macros
Macro Libraries as Include Files
Macros vs. Procedures: Pros and Cons
Chapter 11 Strings and Things: Those Amazing String Instructions
The Notion of an Assembly Language String
Turning Your “String Sense” Inside-Out
Source Strings and Destination Strings
A Text Display Virtual Screen
REP STOSB, the Software Machine Gun
Machine-Gunning the Virtual Display
Executing the STOSB Instruction
STOSB and the Direction Flag DF
Defining Lines in the Display Buffer
Sending the Buffer to the Linux Console
The Semiautomatic Weapon: STOSB Without REP
Who Decrements RCX?
The LOOP Instructions
Displaying a Ruler on the Screen
MUL Is Not IMUL
Ruler’s Lessons
The Four Sizes of STOS
Goodbye, BCD Math
MOVSB: Fast Block Copies
DF and Overlapping Block Moves
Single-Stepping REP String Instructions
Storing Data to Discontinuous Strings
Displaying an ASCII Table
Nested Instruction Loops
Jumping When RCX Goes to 0
Closing the Inner Loop
Closing the Outer Loop
Showchar Recap
Command-Line Arguments, String Searches, and the Linux Stack
Displaying Command-Line Arguments from SASM
String Searches with SCASB
REPNE vs. REPE
You Can’t Pass Command-Line Arguments to Programs Within SASM
The Stack, Its Structure, and How to Use It
Accessing the Stack Directly
Program Prologs and Epilogs
Addressing Data on the Stack
Don’t Pop!
Chapter 12 Heading Out to C: Calling External Functions Written in the C Language
What’s GNU?
The Swiss Army Compiler
Building Code the GNU Way
SASM Uses GCC
How to Use gcc in Assembly Work
Why Not gas?
Linking to the Standard C Library
C Calling Conventions
Callers, Callees, and Clobbers
Setting Up a Stack Frame
Destroying a Stack Frame in the Epilog
Stack Alignment
Characters Out Via puts()
Formatted Text Output with printf()
Passing Parameters to printf()
Printf() Needs a Preceding 0 in RAX
You Shall Have –No-Pie
Data In with fgets() and scanf()
Using scanf() for Entry of Numeric Values
Be a Linux Time Lord
The C Library’s Time Machine
Fetching time_t Values from the System Clock
Converting a time_t Value to a Formatted String
Generating Separate Local Time Values
Making a Copy of glibc’s tm Struct with MOVSD
Understanding AT&T Instruction Mnemonics
AT&T Mnemonic Conventions
AT&T Memory Reference Syntax
Generating Random Numbers
Seeding the Generator with srand()
Generating Pseudorandom Numbers
Some Bits Are More Random Than Others
Calls to Addresses in Registers
Using puts() to Send a Naked Linefeed to the Console
How to Pass a libc Function More Than Six Parameters
How C Sees Command-Line Arguments
Simple File I/O
Converting Strings into Numbers with sscanf()
Creating and Opening Files
Reading Text from Files with fgets()
Writing Text to Files with fprintf()
Notes on Gathering Your Procedures into Libraries
Conclusion: Not the End, But Only the Beginning
Appendix A: The Return of the Insight Debugger
Insight’s Shortcomings
Opening a Program Under Insight
Setting Command-Line Arguments with Insight
Running and Stepping a Program
The Memory Window
Showing the Stack in Insight’s Memory View
Examining the Stack with Insight’s Memory View
Learn gdb!
Appendix B: Partial x64 Instruction Reference
What’s Been Removed from x64
Flag Results
Size Specifiers
Instruction Index
ADC: Arithmetic Addition with Carry
Flags Affected
Legal Forms
Examples
Notes
ADD: Arithmetic Addition
Flags Affected
Legal Forms
Examples
Notes
AND: Logical AND
Flags Affected
Legal Forms
Examples
Notes
BT: Bit Test
Flags Affected
Legal Forms
Examples
Notes
CALL: Call Procedure
Flags Affected
Legal Forms
Examples
Notes
CLC: Clear Carry Flag (CF)
Flags Affected
Legal Forms
Examples
Notes
CLD: Clear Direction Flag (DF)
Flags Affected
Legal Forms
Examples
Notes
CMP: Arithmetic Comparison
Flags Affected
Legal Forms
Examples
Notes
DEC: Decrement Operand
Flags Affected
Legal Forms
Examples
Notes
DIV: Unsigned Integer Division
Flags Affected
Legal Forms
Examples
Notes
INC: Increment Operand
Flags Affected
Legal Forms
Examples
Notes
J??: Jump If Condition Is Met
Flags Affected
Examples
Notes
JECXZ: Jump if ECX=0
Flags Affected
Legal Forms
Examples
Notes
JRCXZ: Jump If RCX=0
Flags Affected
Legal Forms
Examples
Notes
JMP: Unconditional Jump
Flags Affected
Legal Forms
Examples
Notes
LEA: Load Effective Address
Flags Affected
Legal Forms
Examples
Notes
LOOP: Loop Until CX/ECX/RCX=0
Flags Affected
Legal Forms
Examples
Notes
LOOPNZ/LOOPNE: Loop Until CX/ECX/RCX=0 and ZF=0
Flags Affected
Legal Forms
Examples
Notes
LOOPZ/LOOPE: Loop Until CX/ECX/RCX=0 and ZF=1
Flags Affected
Legal Forms
Examples
Notes
MOV: Copy Right Operand into Left Operand
Flags Affected
Legal Forms
Examples
Notes
MOVS: Move String
Flags Affected
Legal Forms
Examples
Notes
MOVSX: Copy with Sign Extension
Flags Affected
Legal Forms
Examples
Notes
MUL: Unsigned Integer Multiplication
Flags Affected
Legal Forms
Examples
Notes
NEG: Negate (Two’s Complement; i.e., Multiply by 1)
Flags Affected
Legal Forms
Examples
Notes
NOP: No Operation
Flags Affected
Legal Forms
Examples
Notes
NOT: Logical NOT (One’s Complement)
Flags Affected
Legal Forms
Examples
Notes
OR: Logical OR
Flags Affected
Legal Forms
Examples
Notes
POP: Copy Top of Stack into Operand
Flags Affected
Legal Forms
Examples
Notes
POPF/D/Q: Copy Top of Stack into Flags Register
Flags Affected
Legal Forms
Examples
Notes
PUSH: Push Operand onto Top of Stack
Flags Affected
Legal Forms
Examples
Notes
PUSHF/D/Q: Push Flags Onto the Stack
Flags Affected
Legal Forms
Examples
Notes
RET: Return from Procedure
Flags Affected
Legal Forms
Examples
Notes
ROL/ROR: Rotate Left/Rotate Right
Flags Affected
Legal Forms
Examples
Notes
SBB: Arithmetic Subtraction with Borrow
Flags Affected
Legal Forms
Examples
Notes
SHL/SHR: Shift Left/Shift Right
Flags Affected
Legal Forms
Examples
Notes
STC: Set Carry Flag (CF)
Flags Affected
Legal Forms
Examples
Notes
STD: Set Direction Flag (DF)
Flags Affected
Legal Forms
Examples
Notes
STOS/B/W/D/Q: Store String
Flags Affected
Legal Forms
Examples
Notes
SUB: Arithmetic Subtraction
Flags Affected
Legal Forms
Examples
Notes
SYSCALL: Fast System Call into Linux
Flags Affected
Legal Forms
Examples
Notes
XCHG: Exchange Operands
Flags Affected
Legal Forms
Examples
Notes
XLAT: Translate Byte Via Table
Flags Affected
Legal Forms
Examples
Notes
XOR: Exclusive OR
Flags Affected
Legal Forms
Examples
Notes
Appendix C: Character Set Charts
Index
EULA

x64 Assembly Language Step-by-Step: Programming with Linux [4 ed.]
 1394155247, 9781394155248, 9781394155545, 9781394155255

  • Commentary
  • Publisher PDF | Published: October 24, 2023
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers