he provided code and various examples should work under any Linux- based 64-bit OS, they have
Previous PDF | Next PDF |
Assembly Language Tutorial - Tutorialspoint
y language is converted into executable machine code by a utility program referred to as an
Assembly Language: Step-by-Step
1992 · Cité 49 fois — Even if you can't play the piano, I hold that you can learn to program in assembly language, if: • You've 6 4 An Assembly-Language Reference for Beginners 168 6 5 Rally 'Round the
Beginners Introduction to the Assembly Language - IC/Unicamp
2009 · Cité 3 fois — Added chapter on code structures in April 2009 Additional http://www avr-asm- tutorial net Content
Assembly Language Programming Basics
mple, an 8-bit processor with 16 address lines may be able to address 216 bytes of memory,
Assembly language tutorial for beginner pdf - Squarespace
ming the assembly language eradicates most of the program, which for older computers is
Windows Assembly Programming Tutorial
hlp which contains the MASM32 manual It has all the macros, registers, flags, Pentium
Introduction to Assembler Programming
and the basics of assembler programming on System z – Use a variety Each instruction to learn is as easy as the next • Syntax is POPs is the processor manual – Optionally
x86-64 Assembly Language Programming with Ubuntu
he provided code and various examples should work under any Linux- based 64-bit OS, they have
[PDF] assembly language tutorial for beginners pdf
[PDF] assembly language tutorial pdf download
[PDF] assessing english language proficiency in u.s. k 12 schools
[PDF] assessment for english language learners
[PDF] assigning values to variables in java
[PDF] association habitat et humanisme ile de france paris
[PDF] assume that there are two alleles r and r
[PDF] assumed mean method pdf
[PDF] atelier langage petite section maternelle
[PDF] atf firearm database
[PDF] athabaskan alphabet
[PDF] athens paris flight schedule
[PDF] atlanta reign members
[PDF] atlas bar interior designer
x86-64
Assembly Language
Programming
withUbuntu
Ed Jorgensen, Ph.D.
Version 1.1.44
May 2022
Cover image:
Top view of an Intel central processing unit Core i7 Skylake type core, model 6700K, released in June 2015. Source: Eric Gaba, https://commons.wikimedia.org/wiki/File :Intel_CPU_Core_i7_6700K_Skylake_top.jpg
Cover background:
By Benjamint444 (Own work)
Source: http://commons.wikimedia.org/wiki/File%3ASwirly_belt444.jpgCopyright © 2015 - 2022 by Ed Jorgensen
You are free:
To Share - to copy, distribute and transmit the workTo 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.2 Understanding the Tool Chain.....................................................................3
1.3.3 Improve Algorithm Development Skills.....................................................3
1.3.4 Improve Understanding of Functions/Procedures.......................................3
1.3.5 Gain an Understanding of I/O Buffering.....................................................4
1.3.6 Understand Compiler Scope........................................................................4
1.3.7 Introduction Multi-processing Concepts.....................................................4
1.3.8 Introduction 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....................................................................................................15
2.5 Memory Layout.................................................................................................17
Page iii
Table of Contents
2.6 Memory Hierarchy............................................................................................17
2.7 Exercises............................................................................................................19
2.7.1 Quiz Questions..........................................................................................19
3.0 Data Representation...........................................................................................21
3.1 Integer Representation.......................................................................................21
3.1.1 Two's Complement....................................................................................23
3.1.2 Byte Example............................................................................................23
3.1.3 Word Example...........................................................................................24
3.2 Unsigned and Signed Addition.........................................................................24
3.3 Floating-point Representation...........................................................................24
3.3.1 IEEE 32-bit Representation.......................................................................25
3.3.1.1 IEEE 32-bit Representation Examples..............................................26
3.3.1.1.1 Example → -7.7510.....................................................................26
3.3.1.1.2 Example → -0.12510...................................................................26
3.3.1.1.3 Example → 4144000016.............................................................27
3.3.2 IEEE 64-bit Representation.......................................................................27
3.3.3 Not a Number (NaN).................................................................................27
3.4 Characters and Strings.......................................................................................28
3.4.1 Character Representation...........................................................................28
3.4.1.1 American Standard Code for Information Interchange.....................28
3.4.1.2 Unicode..............................................................................................29
3.4.2 String Representation................................................................................29
3.5 Exercises............................................................................................................29
3.5.1 Quiz Questions..........................................................................................30
4.0 Program Format.................................................................................................33
4.1 Comments..........................................................................................................33
4.2 Numeric Values.................................................................................................33
4.3 Defining Constants............................................................................................34
4.4 Data Section......................................................................................................34
4.5 BSS Section.......................................................................................................35
4.6 Text Section.......................................................................................................36
4.7 Example Program..............................................................................................37
4.8 Exercises............................................................................................................39
4.8.1 Quiz Questions..........................................................................................39
5.0 Tool Chain...........................................................................................................41
5.1 Assemble/Link/Load Overview........................................................................41
5.2 Assembler..........................................................................................................43
Page iv
Table of Contents
5.2.1 Assemble Commands................................................................................43
5.2.2 List File......................................................................................................43
5.2.3 Two-Pass Assembler.................................................................................45
5.2.3.1 First Pass............................................................................................46
5.2.3.2 Second Pass........................................................................................46
5.2.4 Assembler Directives.................................................................................47
5.3 Linker................................................................................................................47
5.3.1 Linking Multiple Files...............................................................................48
5.3.2 Linking Process.........................................................................................48
5.3.3 Dynamic Linking.......................................................................................49
5.4 Assemble/Link Script........................................................................................50
5.5 Loader................................................................................................................51
5.6 Debugger...........................................................................................................52
5.7 Exercises............................................................................................................52
5.7.1 Quiz Questions..........................................................................................52
6.0 DDD Debugger....................................................................................................55
6.1 Starting DDD.....................................................................................................55
6.1.1 DDD Configuration Settings.....................................................................57
6.2 Program Execution with DDD..........................................................................57
6.2.1 Setting Breakpoints...................................................................................57
6.2.2 Executing Programs...................................................................................58
6.2.2.1 Run / Continue...................................................................................60
6.2.2.2 Next / Step.........................................................................................60
6.2.3 Displaying Register Contents....................................................................60
6.2.4 DDD/GDB Commands Summary.............................................................62
6.2.4.1 DDD/GDB Commands, Examples....................................................64
6.2.5 Displaying Stack Contents........................................................................65
6.2.6 Interactive Debugger Commands File.......................................................65
6.2.6.1 Debugger Commands File (non-interactive).....................................66
6.2.6.2 Non-interactive Debugger Commands File.......................................67
6.3 Exercises............................................................................................................67
6.3.1 Quiz Questions..........................................................................................67
6.3.2 Suggested Projects.....................................................................................68
7.0 Instruction Set Overview....................................................................................71
7.1 Notational Conventions.....................................................................................71
7.1.1 Operand Notation......................................................................................72
7.2 Data Movement.................................................................................................73
Page v
Table of Contents
7.3 Addresses and Values........................................................................................75
7.4 Conversion Instructions.....................................................................................76
7.4.1 Narrowing Conversions.............................................................................76
7.4.2 Widening Conversions..............................................................................76
7.4.2.1 Unsigned Conversions.......................................................................77
7.4.2.2 Signed Conversions...........................................................................78
7.5 Integer Arithmetic Instructions.........................................................................80
7.5.1 Addition.....................................................................................................80
7.5.1.1 Addition with Carry...........................................................................83
7.5.2 Subtraction.................................................................................................86
7.5.3 Integer Multiplication................................................................................89
7.5.3.1 Unsigned Multiplication....................................................................89
7.5.3.2 Signed Multiplication........................................................................93
7.5.4 Integer Division.........................................................................................97
7.6 Logical Instructions.........................................................................................104
7.6.1 Logical Operations..................................................................................105
7.6.2 Shift Operations.......................................................................................106
7.6.2.1 Logical Shift....................................................................................106
7.6.2.2 Arithmetic Shift...............................................................................108
7.6.3 Rotate Operations....................................................................................110
7.7 Control Instructions.........................................................................................111
7.7.1 Labels......................................................................................................112
7.7.2 Unconditional Control Instructions.........................................................112
7.7.3 Conditional Control Instructions.............................................................112
7.7.3.1 Jump Out of Range..........................................................................115
7.7.4 Iteration....................................................................................................118
7.8 Example Program, Sum of Squares.................................................................120
7.9 Exercises..........................................................................................................121
7.9.1 Quiz Questions........................................................................................121
7.9.2 Suggested Projects...................................................................................125
8.0 Addressing Modes.............................................................................................129
8.1 Addresses and Values......................................................................................129
8.1.1 Register Mode Addressing......................................................................130
8.1.2 Immediate Mode Addressing..................................................................130
8.1.3 Memory Mode Addressing......................................................................130
8.2 Example Program, List Summation................................................................133
8.3 Example Program, Pyramid Areas and Volumes............................................135
8.4 Exercises..........................................................................................................140
Page vi
Table of Contents
8.4.1 Quiz Questions........................................................................................140
8.4.2 Suggested Projects...................................................................................143
9.0 Process Stack.....................................................................................................145
9.1 Stack Example.................................................................................................145
9.2 Stack Instructions............................................................................................146
9.3 Stack Implementation......................................................................................147
9.3.1 Stack Layout............................................................................................147
9.3.2 Stack Operations......................................................................................149
9.4 Stack Example.................................................................................................151
9.5 Exercises..........................................................................................................152
9.5.1 Quiz Questions........................................................................................152
9.5.2 Suggested Projects...................................................................................153
10.0 Program Development....................................................................................155
10.1 Understand the Problem................................................................................155
10.2 Create the Algorithm.....................................................................................156
10.3 Implement the Program.................................................................................158
10.4 Test/Debug the Program................................................................................160
10.5 Error Terminology.........................................................................................161
10.5.1 Assembler Error.....................................................................................161
10.5.2 Run-time Error.......................................................................................161
10.5.3 Logic Error............................................................................................162
10.6 Exercises........................................................................................................162
10.6.1 Quiz Questions......................................................................................162
10.6.2 Suggested Projects.................................................................................162
11.0 Macros..............................................................................................................165
11.1 Single-Line Macros.......................................................................................165
11.2 Multi-Line Macros........................................................................................166
11.2.1 Macro Definition...................................................................................166
11.2.2 Using a Macro.......................................................................................166
11.3 Macro Example.............................................................................................167
11.4 Debugging Macros........................................................................................169
11.5 Exercises........................................................................................................169
11.5.1 Quiz Questions......................................................................................169
11.5.2 Suggested Projects.................................................................................170
12.0 Functions..........................................................................................................171
12.1 Updated Linking Instructions........................................................................171
Page vii
Table of Contents
12.2 Debugger Commands....................................................................................172
12.2.1 Debugger Command, next.....................................................................172
12.2.2 Debugger Command, step.....................................................................172
12.3 Stack Dynamic Local Variables....................................................................172
12.4 Function Declaration.....................................................................................173
12.5 Standard Calling Convention........................................................................173
12.6 Linkage..........................................................................................................174
12.7 Argument Transmission................................................................................175
12.8 Calling Convention.......................................................................................175
12.8.1 Parameter Passing..................................................................................176
12.8.2 Register Usage.......................................................................................177
12.8.3 Call Frame.............................................................................................178
12.8.3.1 Red Zone........................................................................................180
12.9 Example, Statistical Function 1 (leaf)...........................................................180
12.9.1 Caller.....................................................................................................181
12.9.2 Callee.....................................................................................................181
12.10 Example, Statistical Function2 (non-leaf)...................................................183
12.10.1 Caller...................................................................................................183
12.10.2 Callee...................................................................................................184
12.11 Stack-Based Local Variables......................................................................187
12.12 Summary.....................................................................................................190
12.13 Exercises......................................................................................................192
12.13.1 Quiz Questions....................................................................................192
12.13.2 Suggested Projects...............................................................................193
13.0 System Services...............................................................................................197
13.1 Calling System Services................................................................................197
13.2 Newline Character.........................................................................................198
13.3 Console Output..............................................................................................199
13.3.1 Example, Console Output......................................................................200
13.4 Console Input................................................................................................203
13.4.1 Example, Console Input........................................................................204
13.5 File Open Operations....................................................................................208
13.5.1 File Open...............................................................................................209
13.5.2 File Open/Create....................................................................................210
13.6 File Read.......................................................................................................211
13.7 File Write.......................................................................................................211
13.8 File Operations Examples.............................................................................212
13.8.1 Example, File Write...............................................................................212
Page viii
Table of Contents
13.8.2 Example, File Read...............................................................................217
13.9 Exercises........................................................................................................223
13.9.1 Quiz Questions......................................................................................223
13.9.2 Suggested Projects.................................................................................223
14.0 Multiple Source Files......................................................................................225
14.1 Extern Statement...........................................................................................225
14.2 Example, Sum and Average..........................................................................226
14.2.1 Assembly Main......................................................................................226
14.2.2 Function Source.....................................................................................228
14.2.3 Assemble and Link................................................................................229
14.3 Interfacing with a High-Level Language......................................................230
14.3.1 Example, C++ Main / Assembly Function............................................230
14.3.2 Compile, Assemble, and Link...............................................................232
14.4 Exercises........................................................................................................232
14.4.1 Quiz Questions......................................................................................233
14.4.2 Suggested Projects.................................................................................233
15.0 Stack Buffer Overflow....................................................................................235
15.1 Understanding a Stack Buffer Overflow.......................................................236
15.2 Code to Inject................................................................................................237
15.3 Code Injection...............................................................................................240
15.4 Code Injection Protections............................................................................241
15.4.1 Data Stack Smashing Protector (or Canaries).......................................242
15.4.2 Data Execution Prevention....................................................................242
15.4.3 Data Address Space Layout Randomization.........................................242
15.5 Exercises........................................................................................................242
15.5.1 Quiz Questions......................................................................................242
15.5.2 Suggested Projects.................................................................................243
16.0 Command Line Arguments............................................................................245
16.1 Parsing Command Line Arguments..............................................................245
16.2 High-Level Language Example....................................................................246
16.3 Argument Count and Argument Vector Table..............................................247
16.4 Assembly Language Example.......................................................................248
16.5 Exercises........................................................................................................252
16.5.1 Quiz Questions......................................................................................252
16.5.2 Suggested Projects.................................................................................252
17.0 Input/Output Buffering..................................................................................255
Page ix
Table of Contents
17.1 Why Buffer?..................................................................................................255
17.2 Buffering Algorithm......................................................................................257
17.3 Exercises........................................................................................................260
17.3.1 Quiz Questions......................................................................................260
17.3.2 Suggested Projects.................................................................................261
18.0 Floating-Point Instructions............................................................................263
18.1 Floating-Point Values....................................................................................263
18.2 Floating-Point Registers................................................................................264
18.3 Data Movement.............................................................................................264
18.4 Integer / Floating-Point Conversion Instructions..........................................266
18.5 Floating-Point Arithmetic Instructions..........................................................268
18.5.1 Floating-Point Addition.........................................................................268
18.5.2 Floating-Point Subtraction.....................................................................270
18.5.3 Floating-Point Multiplication................................................................271
18.5.4 Floating-Point Division.........................................................................273
18.5.5 Floating-Point Square Root...................................................................275
18.6 Floating-Point Control Instructions...............................................................277
18.6.1 Floating-Point Comparison...................................................................277
18.7 Floating-Point Calling Conventions..............................................................281
18.8 Example Program, Sum and Average...........................................................281
18.9 Example Program, Absolute Value...............................................................283
18.10 Exercises......................................................................................................284
18.10.1 Quiz Questions....................................................................................284
18.10.2 Suggested Projects...............................................................................284
19.0 Parallel Processing..........................................................................................287
19.1 Distributed Computing..................................................................................288
19.2 Multiprocessing.............................................................................................288
19.2.1 POSIX Threads......................................................................................289
19.2.2 Race Conditions.....................................................................................290
19.3 Exercises........................................................................................................293
19.3.1 Quiz Questions......................................................................................293
19.3.2 Suggested Projects.................................................................................294
20.0 Interrupts.........................................................................................................295
20.1 Multi-user Operating System........................................................................295
20.1.1 Interrupt Classification..........................................................................296
20.1.2 Interrupt Timing....................................................................................296
20.1.2.1 Asynchronous Interrupts................................................................296
Page x
Table of Contents
20.1.2.2 Synchronous Interrupts..................................................................296
20.1.3 Interrupt Categories...............................................................................297
20.1.3.1 Hardware Interrupt.........................................................................297
20.1.3.1.1 Exceptions..............................................................................297
20.1.3.2 Software Interrupts........................................................................298
20.2 Interrupt Types and Levels............................................................................298
20.2.1 Interrupt Types......................................................................................298
20.2.2 Privilege Levels.....................................................................................299
20.3 Interrupt Processing.......................................................................................300
20.3.1 Interrupt Service Routine (ISR).............................................................300
20.3.2 Processing Steps....................................................................................301
20.3.2.1 Suspension.....................................................................................301
20.3.2.2 Obtaining ISR Address..................................................................301
20.3.2.3 Jump to ISR...................................................................................301
20.3.2.4 Suspension Execute ISR................................................................302
20.3.2.5 Resumption....................................................................................302
20.4 Suspension Interrupt Processing Summary...................................................302
20.5 Exercises........................................................................................................304
20.5.1 Quiz Questions......................................................................................304
20.5.2 Suggested Projects.................................................................................305
21.0 Appendix A - ASCII Table............................................................................307
22.0 Appendix B - Instruction Set Summary.......................................................309
22.1 Notation.........................................................................................................309
22.2 Data Movement Instructions.........................................................................310
22.3 Data Conversion instructions........................................................................310
22.4 Integer Arithmetic Instructions.....................................................................311
22.5 Logical, Shift, and Rotate Instructions..........................................................313
22.6 Control Instructions.......................................................................................315
22.7 Stack Instructions..........................................................................................317
22.8 Function Instructions.....................................................................................317
22.9 Floating-Point Data Movement Instructions.................................................317
22.10 Floating-Point Data Conversion Instructions..............................................318
22.11 Floating-Point Arithmetic Instructions........................................................319
22.12 Floating-Point Control Instructions.............................................................323
23.0 Appendix C - System Services.......................................................................325
23.1 Return Codes.................................................................................................325
23.2 Basic System Services...................................................................................325
Page xi
Table of Contents
23.3 File Modes.....................................................................................................327
23.4 Error Codes...................................................................................................328
24.0 Appendix D - Quiz Question Answers..........................................................331
24.1 Quiz Question Answers, Chapter 1...............................................................331
24.2 Quiz Question Answers, Chapter 2...............................................................331
24.3 Quiz Question Answers, Chapter 3...............................................................332
24.4 Quiz Question Answers, Chapter 4...............................................................334
24.5 Quiz Question Answers, Chapter 5...............................................................335
24.6 Quiz Question Answers, Chapter 6...............................................................336
24.7 Quiz Question Answers, Chapter 7...............................................................337
24.8 Quiz Question Answers, Chapter 8...............................................................340
24.9 Quiz Question Answers, Chapter 9...............................................................341
24.10 Quiz Question Answers, Chapter 10...........................................................341
24.11 Quiz Question Answers, Chapter 11...........................................................342
24.12 Quiz Question Answers, Chapter 12...........................................................342
24.13 Quiz Question Answers, Chapter 13...........................................................343
24.14 Quiz Question Answers, Chapter 14...........................................................343
24.15 Quiz Question Answers, Chapter 15...........................................................344
24.16 Quiz Question Answers, Chapter 16...........................................................344
24.17 Quiz Question Answers, Chapter 17...........................................................345
24.18 Quiz Question Answers, Chapter 18...........................................................345
24.19 Quiz Question Answers, Chapter 19...........................................................346
24.20 Quiz Question Answers, Chapter 20...........................................................346
25.0 Alphabetical Index..........................................................................................349
Page xii
Table of Contents
Illustration IndexIllustration 1: Computer Architecture................................................................................7
Illustration 2: CPU Block Diagram..................................................................................15
Illustration 3: Little-Endian Data Layout.........................................................................16
Illustration 4: General Memory Layout...........................................................................17
Illustration 5: Memory Hierarchy....................................................................................18
Illustration 6: Overview: Assemble, Link, Load.............................................................42
Illustration 7: Little-Endian, Multiple Variable Data Layout..........................................44
Illustration 8: Linking Multiple Files...............................................................................49
Illustration 9: Initial Debugger Screen.............................................................................56
Illustration 10: Debugger Screen with Breakpoint Set....................................................58
Illustration 11: Debugger Screen with Green Arrow.......................................................59
Illustration 12: DDD Command Bar................................................................................60
Illustration 13: Register Window.....................................................................................61
Illustration 14: MOV Instruction Overview....................................................................73
Illustration 15: Integer Multiplication Overview.............................................................90
Illustration 16: Integer Division Overview......................................................................99
Illustration 17: Logical Operations................................................................................105
Illustration 18: Logical Shift Overview.........................................................................107
Illustration 19: Logical Shift Operations.......................................................................107
Illustration 20: Arithmetic Left Shift.............................................................................109
Illustration 21: Arithmetic Right Shift...........................................................................109
Illustration 22: Process Memory Layout........................................................................148
Illustration 23: Process Memory Layout Example........................................................149
Illustration 24: Stack Frame Layout..............................................................................179
Illustration 25: Stack Frame Layout with Red Zone......................................................180
Illustration 26: Stack Call Frame Example....................................................................236
Illustration 27: Stack Call Frame Corruption.................................................................241
Illustration 28: Argument Vector Layout......................................................................248
Illustration 29: Privilege Levels.....................................................................................300
Illustration 30: Interrupt Processing Overview..............................................................303