x64 Assembly Language Step-by-Step: Programming with Linux (Tech Today), 4th Edition Jeff Duntemann instant download
x64 Assembly Language Step-by-Step: Programming with Linux (Tech Today), 4th Edition Jeff Duntemann instant download
https://ebookmeta.com/product/x64-assembly-language-step-by-step-
programming-with-linux-tech-today-4th-edition-jeff-duntemann/
https://ebookmeta.com/product/x64-assembly-language-step-by-step-
programming-with-linux-4th-edition-jeff-duntemann/
https://ebookmeta.com/product/php-programming-a-step-by-step-
guide-to-learn-in-an-easy-way-the-fundamentals-of-php-
programming-language-3nd-edition-daniel-robinson/
https://ebookmeta.com/product/beginning-t-sql-a-step-by-step-
approach-4th-edition-kathi-kellenberger/
https://ebookmeta.com/product/indian-economy-and-neoliberal-
globalization-finance-trade-industry-and-employment-1st-edition-
murali-kallummal-editor-santosh-kumar-editor-p-l-beena-editor/
Cambridge IGCSE and O Level Geography Coursebook with
CD ROM 2nd Edition Cambers
https://ebookmeta.com/product/cambridge-igcse-and-o-level-
geography-coursebook-with-cd-rom-2nd-edition-cambers/
https://ebookmeta.com/product/whole-lotta-grump-grumps-unleashed-
book-3-1st-edition-cassie-mint/
https://ebookmeta.com/product/scats-and-tracks-of-the-rocky-
mountains-a-field-guide-to-the-signs-of-70-wildlife-species-3rd-
edition-james-halfpenny/
https://ebookmeta.com/product/lores-of-ruin-legacy-of-
dezrothia-1-1st-edition-mj-colgan/
https://ebookmeta.com/product/freemasonry-and-the-christian-
faith-fr-ashley-beck/
The Transgender Child Revised Updated Edition A
Handbook for Parents and Professionals Supporting
Transgender and Nonbinary Children Stephanie Brill
https://ebookmeta.com/product/the-transgender-child-revised-
updated-edition-a-handbook-for-parents-and-professionals-
supporting-transgender-and-nonbinary-children-stephanie-brill/
Table of Contents
Cover
Table of Contents
Title Page
Introduction
CHAPTER 1: It's All in the Plan
Another Pleasant Valley Saturday
Had This Been the Real Thing …
Assembly Language Programming As a Square Dance
Assembly Language Programming As a Board Game
CHAPTER 2: Alien Bases
The Return of the New Math Monster
Octal: How the Grinch Stole Eight and Nine
Hexadecimal: Solving the Digit Shortage
From Hex to Decimal and from Decimal to Hex
Practice. Practice! PRACTICE!
Arithmetic in Hex
Binary
Hexadecimal as Shorthand for Binary
Prepare to Compute
CHAPTER 3: Lifting the Hood
RAXie, We Hardly Knew Ye
Switches, Transistors, and Memory
The Shop Supervisor and the Assembly Line
The Box That Follows a Plan
What vs. How: Architecture and Microarchitecture
Enter the Plant Manager
CHAPTER 4: Location, Location, Location
The Joy of Memory Models
The Nature of Segments
Segment Registers
The Four Major Assembly Programming Models
64-Bit Long Mode
CHAPTER 5: The Right to Assemble
The Nine and Sixty Ways to Code
Files and What's Inside Them
Text In, Code Out
The Assembly Language Development Process
Linking the Object Code File
Taking a Trip Down Assembly Lane
CHAPTER 6: A Place to Stand, with Access to Tools
Integrated Development Environments
Introducing SASM
Linux and Terminals
Using Linux Make
Debugging with SASM
CHAPTER 7: Following Your Instructions
Build Yourself a Sandbox
Instructions and Their Operands
Source and Destination Operands
Rally Round the Flags, Boys!
Signed and Unsigned Values
Implicit Operands and MUL
Reading and Using an Assembly Language Reference
NEG Negate (Two's Complement; i.e., Multiply by −1)
CHAPTER 8: Our Object All Sublime
The Bones of an Assembly Language Program
Last In, First Out via the Stack
Using Linux Kernel Services Through Syscall
Designing a Nontrivial Program
Going Further
CHAPTER 9: Bits, Flags, Branches, and Tables
Bits Is Bits (and Bytes Is Bits)
Shifting Bits
Bit-Bashing in Action
Flags, Tests, and Branches
X64 Long Mode Memory Addressing in Detail
Character Table Translation
Tables Instead of Calculations
CHAPTER 10: Dividing and Conquering
Boxes within Boxes
Calling and Returning
Local Labels and the Lengths of Jumps
Building External Procedure Libraries
The Art of Crafting Procedures
Simple Cursor Control in the Linux Console
Creating and Using Macros
CHAPTER 11: Strings and Things
The Notion of an Assembly Language String
REP STOSB, the Software Machine Gun
The Semiautomatic Weapon: STOSB Without REP
MOVSB: Fast Block Copies
Storing Data to Discontinuous Strings
Command-Line Arguments, String Searches, and the Linux
Stack
The Stack, Its Structure, and How to Use It
CHAPTER 12: Heading Out to C
What's GNU?
Linking to the Standard C Library
Formatted Text Output with printf()
Data In with fgets() and scanf()
Be a Linux Time Lord
Understanding AT&T Instruction Mnemonics
Generating Random Numbers
How C Sees Command-Line Arguments
Simple File I/O
Conclusion: Not the End, But Only the Beginning
Where to Now?
The Art of 64-bit Assembly by Randall Hyde (No Starch
Press, 2022)
Modern x86 Assembly Language Programming by David
Kusswurm (Apress, 2018)
Stepping off Square One
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
ADD: Arithmetic Addition
AND: Logical AND
BT: Bit Test
CALL: Call Procedure
CLC: Clear Carry Flag (CF)
CLD: Clear Direction Flag (DF)
CMP: Arithmetic Comparison
DEC: Decrement Operand
DIV: Unsigned Integer Division
INC: Increment Operand
J??: Jump If Condition Is Met
JECXZ: Jump if ECX=0
JRCXZ: Jump If RCX=0
JMP: Unconditional Jump
LEA: Load Effective Address
LOOP: Loop Until CX/ECX/RCX=0
LOOPNZ/LOOPNE: Loop Until CX/ECX/RCX=0 and ZF=0
LOOPZ/LOOPE: Loop Until CX/ECX/RCX=0 and ZF=1
MOV: Copy Right Operand into Left Operand
MOVS: Move String
MOVSX: Copy with Sign Extension
MUL: Unsigned Integer Multiplication
NEG: Negate (Two's Complement; i.e., Multiply by −1)
NOP: No Operation
NOT: Logical NOT (One's Complement)
OR: Logical OR
POP: Copy Top of Stack into Operand
POPF/D/Q: Copy Top of Stack into Flags Register
PUSH: Push Operand onto Top of Stack
PUSHF/D/Q: Push Flags Onto the Stack
RET: Return from Procedure
ROL/ROR: Rotate Left/Rotate Right
SBB: Arithmetic Subtraction with Borrow
SHL/SHR: Shift Left/Shift Right
STC: Set Carry Flag (CF)
STD: Set Direction Flag (DF)
STOS/B/W/D/Q: Store String
SUB: Arithmetic Subtraction
SYSCALL: Fast System Call into Linux
XCHG: Exchange Operands
XLAT: Translate Byte Via Table
XOR: Exclusive OR
APPENDIX C: Character Set Charts
Index
Copyright
Dedication
About the Author
About the Technical Editor
Acknowledgments
End User License Agreement
List of Tables
Chapter 2
Table 2.1: Counting in Martian, Base Fooby
Table 2.2: Powers of Fooby
Table 2.3: Counting in Octal, Base 8
Table 2.4: Octal Columns as Powers of Eight
Table 2.5: Counting in Hexadecimal, Base 16
Table 2.6: Hexadecimal Columns as Powers of 16
Table 2.7: Binary Columns as Powers of 2
Chapter 4
Table 4.1: Collective Terms for Memory
Chapter 6
Table 6.1: The Three Standard Unix Files
Chapter 7
Table 7.1: MOV and Its Operands
Table 7.2: The Ranges of Signed Values
Table 7.3: The MOVSX Instruction
Table 7.4: The MUL Instruction
Table 7.5: The DIV Instruction
Chapter 8
Table 8.1: System Call Conventions for the System V ABI
Chapter 9
Table 9.1: The AND Truth Table for Formal Logic
Table 9.2: The AND Truth Table for Assembly Language
Table 9.3: The OR Truth Table for Assembly Language
Table 9.4: The XOR Truth Table for Assembly Language
Table 9.5: The NOT Truth Table for Assembly Language
Table 9.6: Jump Instruction Mnemonics and Their
Synonyms
Table 9.7: Arithmetic Tests Useful After a CMP Instruction
Table 9.8: 64-Bit Long Mode Memory-Addressing Schemes
Chapter 12
Table 12.1: Printf() Formatting Codes
Table 12.2: The Values Contained in the tm Structure
Table 12.3: File Access Codes for Use with fopen()
List of Illustrations
Chapter 1
Figure 1.1: The Game of Assembly Language
Chapter 2
Figure 2.1: The anatomy of ∩≡ ⌠ Θ ≡
Figure 2.2: The anatomy of 76225 octal
Figure 2.3: The anatomy of 3C0A9H
Chapter 3
Figure 3.1: Transistor switches and memory cells
Figure 3.2: A RAM chip
Figure 3.3: A simple 1-megabyte memory system
Figure 3.4: The CPU and memory
Figure 3.5: The idea of multitasking
Figure 3.6: A mature protected-mode operating system
Chapter 4
Figure 4.1: The 8080 memory model
Figure 4.2: The 8080 memory model inside an 8086
memory system
Figure 4.3: Seeing a megabyte through 64 KB blinders
Figure 4.4: Memory addresses versus segment addresses
Figure 4.5: Segments and offsets
Figure 4.6: Registers inside registers
Figure 4.7: 8-bit, 16-bit, 32-bit, and 64-bit registers
Figure 4.8: Real-mode flat model
Figure 4.9: The real-mode segmented model
Figure 4.10: 32-bit protected mode flat model
Chapter 5
Figure 5.1: Displaying a Linux text file with the GHex editor
Figure 5.2: Displaying a Windows text file with the GHex
editor
Figure 5.3: A Linux text file displayed under Windows
Figure 5.4: Differences in display order versus differences in
evaluation or...
Figure 5.5: Big endian versus little endian for a 16-bit value
Figure 5.6: Big endian versus little endian for a 32-bit value
Figure 5.7: What the assembler does
Figure 5.8: The assembler and linker
Figure 5.9: The assembly language development process
Figure 5.10: The Linux Mint Software Manager
Figure 5.11: The anatomy of a NASM command line
Figure 5.12: The anatomy of an ld command line
Chapter 6
Figure 6.1: The SASM Build dialog
Figure 6.2: The full SASM window in debug mode
Figure 6.3: Changing Konsole's character encoding to IBM-
850
Figure 6.4: I/O redirection
Figure 6.5: Adding a key binding to Konsole
Chapter 7
Figure 7.1: Character strings as immediate data
Figure 7.2: The x64 RFlags register
Chapter 8
Figure 8.1: The stack
Figure 8.2: The stack in program memory
Figure 8.3: How the stack works
Figure 8.4: The “off by one” error
Chapter 9
Figure 9.1: Bit numbering
Figure 9.2: The anatomy of an AND instruction
Figure 9.3: Using XOR to zero a register
Figure 9.4: How the rotate instructions work
Figure 9.5: How the rotate through carry instructions work
Figure 9.6: Using a lookup table
Figure 9.7: A table of 16 three-byte entries
Figure 9.8: Multiplying by shifting
Figure 9.9: x64 long mode memory addressing
Figure 9.10: How address scaling works
Chapter 10
Figure 10.1: Calling a procedure and returning
Figure 10.2: Local labels and the globals that own them
Figure 10.3: Connecting globals and externals
Figure 10.4: How macros work
Chapter 11
Figure 11.1: Using MOVSB on overlapping memory blocks
Figure 11.2: How to access parameters from within SASM
Figure 11.3: The Linux stack at program execution
Chapter 12
Figure 12.1: How gcc builds Linux executables
Figure 12.2: The structure of a hybrid C-assembly program
Figure 12.3: A stack frame
Figure 12.4: Accessing command-line arguments from the
x64 main() function
Appendix A
Figure A.1: Insight's memory display of a .data section
Figure A.2: Command-line arguments in Insight's memory
view
x64 Assembly Language Step-
by-Step
4TH Edition
Jeff Duntemann
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
Exploring the Variety of Random
Documents with Different Content
— Mitä minä höpisen? Eikö se ole totta että Karikosken kankaalla
on iso kivi?
— Niin — kukko. Vai oletko sinä joskus nähnyt kukon laulavan niin
ettei se huiskahda?
— Ohoh! pääsi häneltä. — Jos en olisi itse ollut mukana niin olisin
kaatunut.
— Olisit tullut tänne, niin minä olisin nostanut, sanoi Lissu siihen.
— Tietysti.
— No mennään sitte.
Ei! Vaikka kaikki toiset niin tekisivätkin, niin hän ei tee! Hän ei
tahdo! Olkoon vaikka tapakin sellainen!
— Ei vain päästänyt?
— Hävetkää! Siat!
Kuitenkin tuntui helpommalta nyt kun oli saanut sen sanotuksi. Oli
aivan kuin olisi se tähän saakka ollut kautta sielun hajoitettuna
likaisuutena, mutta nyt saatu paremmin yhteen paikkaan kootuksi.
Hän näki sen tahran, katsoi siihen ja itku kurkussa teki pyhän
lupauksen, että toista kertaa ei hän sellaista tee! Tämän
suuremmaksi hän ei tahraa päästä!
Kun kirje oli valmis luki hän sen. Posket kuumenivat yhä enempi ja
silmiin ilmestyi hämmästyneesti tuijottava sävy.
— Ennen on ollut niitä kytöjä niin paljon. Niistä ne niitä jyviä ovat
koonneet. Mutta nyt ei kannata enää niitäkään teettää, totesi
Nummismäki. Ei sitä kannata, eikä kehtaakaan kuokkurille kaksia ja
kolmia markkoja päivältä maksaa. Ennen antaa maan maata siellä ja
rääpystää sen mitä itse ehtii.
Tätä kaikkea seurasi hän ovisängyssä maaten. Kun hän sitten ehti
pukeutua ja oli menossa talliin, sattui hän porstuassa yhteen
kamarista tulevan mieshenkilön kanssa. — Kamariin oli nimittäin
Maijan ripille päästyä tehty porstuastakin ovi.
— Ei ole vielä yhtään yötäkään kotoa poissa ollut, muuta kun mitä
joskus vieraisilla on ollut.
— Annin kanssa — tuli mieleen aivan kuin joku veitikka olisi sen
kuiskannut.
Kaikkiko?
*****
Niilolla oli talitiainen häkissä ja hän touhusi sen kanssa. Mutta juuri
kun Lauri meni hevosta valjastamaan hän sanoa poksautti:
— Kyllä.
Hän teki huolellista työtä. Ja kun konsepti vihdoin oli valmis, oli
myöskin puolipäivänavetan aika tullut.
Hän meni navettaan ja antoi lehmille vihot niin kuin äiti oli
käskenyt, pitäen suostuttelevia puheluja uutta ruokkijaansa
oudosteleville lehmille, lampaille ja vasikoille. Kanoillekin antoi ohria
ja sialle hyppyytti äidin valmiiksi laittaman puolisen. Kävi vielä
ruunallekin ruokaa antamassa ja jälleen tupaan tultuaan nosti
kaapista pöydälle voipytyn, leipäkorin, siankinkun, vadillisen eilen
illalla keitettyä mannaryynipuuroa sekä maitovadin ja vielä haki
uuniseinältä naulasta särvinkuppinsa. Sitten alkoi itsekin syödä
puolista.
Alaiskylä 6/1
Hyvä Anni!
Lauri L—ta.
K. Neiti
Anni Niinilä.
Pieliskylä.
*****
Tästä alkaen oli taival melkein outoa Laurille. Heillä ei ollut tällä
suunnalla mitään ajoja. Ulkosarka kyllä oli siellä samalla Tunturin
maalla päin, jonne nyt oltiin menossa, mutta sen hakkuusta pitivät
sinne asettuneet torpparit huolen. Joskus siellä käytiin kannot
laskemassa ja ranka- ja hirsikasoja tarkastelemassa sekä
tuloksetonta lain lukua torppareille harjoittamassa.
Äkkiä kohotti hän päätään. Oli kuin olisi tuikahtanut valoa tuolta
vähän matkaa tieltä olevalta kuusikkokummulta. — — Hän teroitti
katsettaan. Tuli mieleen kertomukset metsissä nuotioitten ääressä
makailevista karkulaisista ja rosvoista sekä poltetuista kylistään
vainolaista pakoon lähteneistä ihmisistä.
Jälleen tuikahti valo näkyviin. Nyt sen näki selvään että se tuli
ikkunasta.