Assembly Language Programming

X86-64 Assembly Language Programming with Ubuntu Ed Jorgensen Version 1.0.10 September 2015 Cover image: AMD Opteron, the first CPU to introduce the x86-64 extensions in 2003. Source: http://en.wikipedia.org/wiki/File:AMD_Opteron_146_Venus,_2005.jpg Cover background: By Benjamint444 (Own work) Source: http://commons.wikimedia.org/wiki/File%3ASwirly_belt444.jpg Copyright © 2015 by Ed Jorgensen You are free: to Share — to copy, distribute and transmit the work to Remix — to adapt the work Under the following conditions: Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). Noncommercial — You may not use this work for commercial purposes. Share Alike — If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one. Table of Contents Table of Contents 1.0 Introduction…………………………………………………………………………………………………1 1.1 Prerequisites……………………………………………………………………………………………..1 1.2 What is Assembly Language……………………………………………………………………….2 1.3 Why Learn Assembly Language………………………………………………………………….2 1.3.1 Gain a Better Understanding of Architecture Issues………………………………..3 1.3.1 Understanding the Tool Chain………………………………………………………………3 1.3.1 Improve Algorithm Development Skills………………………………………………..3 1.3.1 Improves Understanding of Functions/Procedures………………………………….3 1.3.1 Gain an Understanding of I/O Buffering………………………………………………..4 1.3.1 Understand Compiler Scope…………………………………………………………………4 1.3.1 Introduction to Multi-processing Concepts…………………………………………….4 1.3.1 Introduction to Interrupt Processing Concepts………………………………………..4 1.4 Additional References………………………………………………………………………………..4 1.4.1 Ubuntu References……………………………………………………………………………..5 1.4.2 BASH Command Line References………………………………………………………..5 1.4.3 Architecture References……………………………………………………………………….5 1.4.4 Tool Chain References…………………………………………………………………………5 1.4.4.1 YASM References………………………………………………………………………..6 1.4.4.2 DDD Debugger References…………………………………………………………..6 2.0 Architecture Overview………………………………………………………………………………….7 2.1 Architecture Overview……………………………………………………………………………….7 2.2 Data Storage Sizes……………………………………………………………………………………..8 2.3 Central Processing Unit………………………………………………………………………………9 2.3.1 CPU Registers………………………………………………………………………………….10 2.3.1.1 General Purpose Registers (GPRs)……………………………………………….10 2.3.1.2 Stack Pointer Register (RSP)……………………………………………………….12 2.3.1.3 Base Pointer Register (RBP)………………………………………………………..12 2.3.1.4 Instruction Pointer Register (RIP)………………………………………………..12 2.3.1.5 Flag Register (rFlags)…………………………………………………………………12 2.3.1.6 XMM Registers………………………………………………………………………….13 2.3.2 Cache Memory…………………………………………………………………………………14 2.4 Main Memory………………………………………………………………………………………….16 2.5 Memory Layout……………………………………………………………………………………….17 Page iii Table of Contents 2.6 CPU Registers…………………………………………………………………………………………17 2.6.1 General Purpose Registers (GPRs)………………………………………………………18 2.6.2 Stack Pointer Register (RSP)………………………………………………………………20 2.6.3 Base Pointer Register (RBP)………………………………………………………………20 2.6.4 Instruction Pointer Register (RIP)……………………………………………………….20 2.6.5 Flag Register (rFlags)………………………………………………………………………..20 2.6.6 XMM Registers………………………………………………………………………………..21 2.7 Memory Hierarchy…………………………………………………………………………………..22 2.8 Exercises…………………………………………………………………………………………………24 2.8.1 Quiz Questions…………………………………………………………………………………24 3.0 Data Representation…………………………………………………………………………………..27 3.1 Integer Representation………………………………………………………………………………27 3.1.1 Two’s Compliment…………………………………………………………………………….29 3.1.2 Byte Example…………………………………………………………………………………..29 3.1.3 Word Example………………………………………………………………………………….30 3.2 Unsigned and Signed Addition…………………………………………………………………..30 3.3 Floating-point Representation……………………………………………………………………30 3.3.1 IEEE 32-bit Representation………………………………………………………………..31 3.3.1.1 IEEE 32-bit Representation Examples………………………………………….32 3.3.1.1.1 Example → 7.7510……………………………………………………………..32 3.3.1.1.2 Example → 0.12510……………………………………………………………32 3.3.1.1.3 Example → 4144000016……………………………………………………..33 3.3.2 IEEE 64-bit Representation………………………………………………………………..33 3.3.3 Not a Number (NaN)…………………………………………………………………………33 3.4 Characters and Strings………………………………………………………………………………34 3.4.1 Character Representation……………………………………………………………………34 3.4.1.1 American Standard Code for Information Interchange…………………….34 3.4.1.2 Unicode…………………………………………………………………………………….35 3.4.2 String Representation………………………………………………………………………..35 3.5 Exercises…………………………………………………………………………………………………35 3.5.1 Quiz Questions…………………………………………………………………………………36 4.0 Program Format………………………………………………………………………………………..39 4.1 Comments……………………………………………………………………………………………….39 4.2 Numeric Values……………………………………………………………………………………….39 4.3 Defining Constants…………………………………………………………………………………..40 4.4 Data Section……………………………………………………………………………………………40 4.5 BSS Section…………………………………………………………………………………………….41 Page iv Table of Contents 4.6 Text Section…………………………………………………………………………………………….42 4.7 Example Program…………………………………………………………………………………….43 4.8 Exercises…………………………………………………………………………………………………45 4.8.1 Quiz Questions…………………………………………………………………………………45 5.0 Tool Chain………………………………………………………………………………………………….47 5.1 Assemble/Link/Load Overview………………………………………………………………….47 5.2 Assembler……………………………………………………………………………………………….49 5.2.1 Assemble Commands………………………………………………………………………..49 5.2.2 List File……………………………………………………………………………………………49 5.2.3 Two-Pass Assembler………………………………………………………………………….51 5.2.3.1 First Pass…………………………………………………………………………………..52 5.2.3.2 Second Pass……………………………………………………………………………….52 5.2.4 Assembler Directives…………………………………………………………………………53 5.3 Linker…………………………………………………………………………………………………….53 5.3.1 Linking Multiple Files……………………………………………………………………….54 5.3.2 Linking Process………………………………………………………………………………..54 5.3.3 Dynamic Linking………………………………………………………………………………56 5.4 Assemble/Link Script……………………………………………………………………………….56 5.5 Loader…………………………………………………………………………………………………….58 5.6 Debugger………………………………………………………………………………………………..58 5.7 Exercises…………………………………………………………………………………………………59 5.7.1 Quiz Questions…………………………………………………………………………………59 6.0 DDD Debugger…………………………………………………………………………………………..61 6.1 Starting DDD…………………………………………………………………………………………..61 6.1.1 DDD Configuration Settings………………………………………………………………63 6.2 Program Execution with DDD…………………………………………………………………..63 6.2.1 Setting Breakpoints…………………………………………………………………………..63 6.2.2 Executing Programs…………………………………………………………………………..64 6.2.2.1 Run / Continue…………………………………………………………………………..66 6.2.2.2 Next / Step………………………………………………………………………………..66 6.2.3 Displaying Register Contents……………………………………………………………..66 6.2.4 DDD/GDB Commands Summary……………………………………………………….68 6.2.4.1 DDD/GDB Commands, Examples……………………………………………….70 6.2.5 Displaying Stack Contents…………………………………………………………………71 6.2.6 Debugger Commands File (interactive)……………………………………………….71 6.2.6.1 Debugger Commands File (non-interactive)………………………………….72 6.2.6.2 Debugger Commands File (non-interactive)………………………………….73 Page v Table of Contents 6.3 Exercises…………………………………………………………………………………………………73 6.3.1 Quiz Questions…………………………………………………………………………………73 6.3.2 Suggested Projects…………………………………………………………………………….74 7.0 Instruction Set Overview…………………………………………………………………………….77 7.1 Notational Conventions…………………………………………………………………………….77 7.1.1 Operand Notation……………………………………………………………………………..78 7.2 Data Movement……………………………………………………………………………………….79 7.3 Addresses vs Values…………………………………………………………………………………81 7.4 Conversion Instructions…………………………………………………………………………….82 7.4.1 Narrowing Conversions……………………………………………………………………..82 7.4.2 Widening Conversions……………………………………………………………………….82 7.4.2.1 Unsigned Conversions………………………………………………………………..83 7.4.2.2 Signed Conversions……………………………………………………………………84 7.5 Integer Arithmetic Instructions…………………………………………………………………..86 7.5.1 Addition…………………………………………………………………………………………..86 7.5.1.1 Addition with Carry……………………………………………………………………89 7.5.2 Subtraction……………………………………………………………………………………….92 7.5.3 Integer Multiplication………………………………………………………………………..95 7.5.3.1 Unsigned Multiplication……………………………………………………………..95 7.5.3.2 Signed Multiplication…………………………………………………………………98 7.5.4 Integer Division………………………………………………………………………………102 7.6 Logical Instructions………………………………………………………………………………..108 7.6.1 Logical Operations………………………………………………………………………….109 7.6.2 Shift Operations………………………………………………………………………………110 7.6.2.1 Logical Shift…………………………………………………………………………….110 7.6.2.2 Arithmetic Shift………………………………………………………………………..112 7.6.3 Rotate Operations……………………………………………………………………………114 7.7 Control Instructions………………………………………………………………………………..115 7.7.1 Labels…………………………………………………………………………………………….115 7.7.2 Unconditional Control Instructions……………………………………………………116 7.7.3 Conditional Control Instructions……………………………………………………….116 7.7.3.1 Jump Out Of Range………………………………………………………………….119 7.7.4 Iteration………………………………………………………………………………………….122 7.8 Example Program, Sum of Squares…………………………………………………………..124 7.9 Exercises……………………………………………………………………………………………….125 7.9.1 Quiz Questions……………………………………………………………………………….125 7.9.2 Suggested Projects…………………………………………………………………………..129 Page vi Table of Contents 8.0 Addressing Modes…………………………………………………………………………………….133 8.1 Addresses vs Values……………………………………………………………………………….133 8.1.1 Register Mode Addressing……………………………………………………………….134 8.1.2 Immediate Mode Addressing…………………………………………………………….134 8.1.3 Memory Mode Addressing……………………………………………………………….135 8.2 Example Program, List Summation………………………………………………………….138 8.3 Example Program, Pyramid Areas and Volumes…………………………………………139 8.4 Exercises……………………………………………………………………………………………….144 8.4.1 Quiz Questions……………………………………………………………………………….145 8.4.2 Suggested Projects…………………………………………………………………………..148 9.0 Process Stack……………………………………………………………………………………………151 9.1 Stack Example……………………………………………………………………………………….151 9.2 Stack Instructions…………………………………………………………………………………..152 9.3 Stack Implementation……………………………………………………………………………..153 9.3.1 Stack Layout…………………………………………………………………………………..154 9.3.2 Stack Operations……………………………………………………………………………..155 9.4 Stack Example……………………………………………………………………………………….157 9.5 Exercises……………………………………………………………………………………………….158 9.5.1 Quiz Questions……………………………………………………………………………….159 9.5.2 Suggested Projects…………………………………………………………………………..160 10.0 Program Development…………………………………………………………………………….161 10.1 Understand the Problem………………………………………………………………………..161 10.2 Create the Algorithm…………………………………………………………………………….162 10.3 Implement the Program…………………………………………………………………………164 10.4 Test/Debug the Program………………………………………………………………………..166 10.5 Error Terminology………………………………………………………………………………..167 10.5.1 Assembler Error…………………………………………………………………………….167 10.5.2 Run-time Error………………………………………………………………………………168 10.5.3 Logic Error…………………………………………………………………………………..168 10.6 Exercises……………………………………………………………………………………………..168 10.6.1 Quiz Questions……………………………………………………………………………..168 10.6.2 Suggested Projects…………………………………………………………………………169 11.0 Macros……………………………………………………………………………………………………171 11.1 Single-Line Macros………………………………………………………………………………171 11.2 Multi-Line Macros……………………………………………………………………………….172 11.2.1 Macro Definition…………………………………………………………………………..172 11.2.2 Using a Macros……………………………………………………………………………..173 Page vii Table of Contents 11.3 Macro Example……………………………………………………………………………………173 11.4 Debugging Macros……………………………………………………………………………….175 11.5 Exercises……………………………………………………………………………………………..176 11.5.1 Quiz Questions………………………………………………………………………………176 11.5.2 Suggested Projects…………………………………………………………………………176 12.0 Functions………………………………………………………………………………………………..177 12.1 Stack Dynamic Local Variables……………………………………………………………..177 12.2 Function Declaration…………………………………………………………………………….178 12.3 Standard Calling Convention…………………………………………………………………178 12.4 Linkage……………………………………………………………………………………………….179 12.5 Argument Transmission…………………………………………………………………………180 12.6 Calling Convention………………………………………………………………………………180 12.6.1 Parameters Passing………………………………………………………………………..181 12.6.2 Register Usage………………………………………………………………………………182 12.6.3 Call Frame……………………………………………………………………………………183 12.6.3.1 Red Zone……………………………………………………………………………….185 12.7 Example, Statistical Function 1 (leaf)……………………………………………………..185 12.7.1 Caller…………………………………………………………………………………………..186 12.7.2 Callee…………………………………………………………………………………………..186 12.8 Example, Statistical Function2 (non-leaf)………………………………………………..188 12.8.1 Caller…………………………………………………………………………………………..188 12.8.2 Callee…………………………………………………………………………………………..189 12.9 Stack-Based Local Variables………………………………………………………………….193 12.10 Summary…………………………………………………………………………………………..196 12.11 Exercises……………………………………………………………………………………………197 12.11.1 Quiz Questions…………………………………………………………………………….197 12.11.2 Suggested Projects……………………………………………………………………….199 13.0 System Services………………………………………………………………………………………203 13.1 Calling System Services………………………………………………………………………..203 13.2 Newline Character………………………………………………………………………………..204 13.3 Console Output…………………………………………………………………………………….205 13.3.1 Example, Console Output……………………………………………………………….206 13.4 Console Input………………………………………………………………………………………209 13.4.1 Example, Console Input…………………………………………………………………210 13.5 File Open Operations……………………………………………………………………………214 13.5.1 File Open……………………………………………………………………………………..215 13.5.2 File Open/Create……………………………………………………………………………216 Page viii Table of Contents 13.6 File Read…………………………………………………………………………………………….217 13.7 File Write…………………………………………………………………………………………….217 13.8 File Operations Examples……………………………………………………………………..218 13.8.1 Example, File Write……………………………………………………………………….218 13.8.2 Example, File Read……………………………………………………………………….224 13.9 Exercises……………………………………………………………………………………………..230 13.9.1 Quiz Questions……………………………………………………………………………..230 13.9.2 Suggested Projects…………………………………………………………………………231 14.0 Multiple Source Files………………………………………………………………………………233 14.1 Extern Statement………………………………………………………………………………….233 14.2 Example, Sum and Average……………………………………………………………………234 14.2.1 Assembly Main……………………………………………………………………………..234 14.2.2 Function Source…………………………………………………………………………….236 14.2.3 Assemble and Link………………………………………………………………………..238 14.3 Interfacing with a High-Level Language…………………………………………………238 14.3.1 Example, C++ Main / Assembly Function………………………………………..238 14.3.2 Compile, Assemble, and Link…………………………………………………………240 14.4 Exercises……………………………………………………………………………………………..241 14.4.1 Quiz Questions……………………………………………………………………………..241 14.4.2 Suggested Projects…………………………………………………………………………241 15.0 Stack Buffer Overflow…………………………………………………………………………….243 15.1 Understanding a Stack Buffer Overflow………………………………………………….244 15.2 Code to Inject………………………………………………………………………………………245 15.3 Code Injection……………………………………………………………………………………..248 15.4 Code Injection Protections…………………………………………………………………….249 15.4.1 Data Stack Smashing Protector (or Canaries)……………………………………249 15.4.2 Data Execution Prevention……………………………………………………………..250 15.4.3 Data Address Space Layout Randomization……………………………………..250 15.5 Exercises……………………………………………………………………………………………..250 15.5.1 Quiz Questions……………………………………………………………………………..250 15.5.2 Suggested Projects…………………………………………………………………………251 16.0 Command Line Arguments……………………………………………………………………..253 16.1 Parsing Command Line Arguments………………………………………………………..253 16.2 High-Level Language Example……………………………………………………………..254 16.3 Argument Count and Argument Vector Table…………………………………………..255 16.4 Assembly Language Example………………………………………………………………..256 16.5 Exercises……………………………………………………………………………………………..260 Page ix Table of Contents 16.5.1 Quiz Questions……………………………………………………………………………..260 16.5.2 Suggested Projects…………………………………………………………………………261 17.0 Input/Output Buffering…………………………………………………………………………..263 17.1 Why Buffer………………………………………………………………………………………….263 17.2 Buffering Algorithm……………………………………………………………………………..265 17.3 Exercises……………………………………………………………………………………………..268 17.3.1 Quiz Questions……………………………………………………………………………..268 17.3.2 Suggested Projects…………………………………………………………………………269 18.0 Floating Point Instructions………………………………………………………………………271 18.1 Floating Point Values……………………………………………………………………………271 18.2 Floating Point Registers………………………………………………………………………..271 18.3 Data Movement……………………………………………………………………………………272 18.4 Integer / Floating Point Conversion Instructions………………………………………274 18.5 Floating Point Arithmetic Instructions…………………………………………………….276 18.5.1 Floating Point Addition………………………………………………………………….276 18.5.2 Floating Point Subtraction………………………………………………………………278 18.5.3 Floating Point Multiplication………………………………………………………….279 18.5.4 Floating Point Division…………………………………………………………………..281 18.5.5 Floating Point Square Root…………………………………………………………….283 18.6 Floating Point Control Instructions…………………………………………………………285 18.6.1 Floating Point Comparison……………………………………………………………..286 18.7 Floating Point Calling Conventions………………………………………………………..289 18.8 Example Program, Sum and Average…………………………………………â€

4 Comments

  1. Maglia Roma Bambino

    Pretty nice post. I just stumbled upon your blog and wished to say that I’ve really loved browsing your weblog posts. In any case I will be subscribing to your feed and I am hoping you write again soon!
    [url=http://www.ayacara.org/category/health/emergency-services/]Maglia Roma Bambino[/url]

  2. målvaktströja fotboll

    I love looking through your web site. thnx!
    målvaktströja fotboll

  3. giovani salg

    Surprisingly individual pleasant website. Astounding details offered on few gos to.
    giovani salg

  4. edfella-yestoday

    EwanLutes Slovakien HMIMalind
    MatildaPi Dortmund IrmaZupah
    Christoph Elfenbenskusten RoscoeWea
    Shayneixz Ryssland MilagrosH
    SDNGeorgi Manchester United MitziQuw
    KennethBa Osterrike LarueJohn
    Teravcmxt Island ByronEber
    CarmellaS England MarshallD
    FlorBeck Atletico Madrid UlrichLaz
    EloisaMen Italien MarkAhMou
    Madeleine Rumanien OliverWil
    HeidiThe Seattle Sounders DiegoThar
    AmandaBru Colombia Wilhemina
    MarinaAsh USA EPOZaceep
    EmileRobi Atletico Madrid HenryBlau

Leave a Comment

Your email address will not be published.