100% found this document useful (1 vote)
34 views73 pages

x86 64 Assembly Language Programming with Ubuntu Ed Jorgensen - Download the entire ebook instantly and explore every detail

The document provides information about the ebook 'x86-64 Assembly Language Programming with Ubuntu' by Ed Jorgensen, along with links to download various related ebooks. It includes a comprehensive table of contents covering topics such as architecture overview, data representation, program format, tool chain, and debugging. The ebook is designed to help readers understand assembly language and its applications in programming.

Uploaded by

cerasnchavo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views73 pages

x86 64 Assembly Language Programming with Ubuntu Ed Jorgensen - Download the entire ebook instantly and explore every detail

The document provides information about the ebook 'x86-64 Assembly Language Programming with Ubuntu' by Ed Jorgensen, along with links to download various related ebooks. It includes a comprehensive table of contents covering topics such as architecture overview, data representation, program format, tool chain, and debugging. The ebook is designed to help readers understand assembly language and its applications in programming.

Uploaded by

cerasnchavo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Read Anytime Anywhere Easy Ebook Downloads at ebookmeta.

com

x86 64 Assembly Language Programming with Ubuntu


Ed Jorgensen

https://ebookmeta.com/product/x86-64-assembly-language-
programming-with-ubuntu-ed-jorgensen/

OR CLICK HERE

DOWLOAD EBOOK

Visit and Get More Ebook Downloads Instantly at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Modern X86 Assembly Language Programming: Covers X86


64-bit, AVX, AVX2, and AVX-512, 3rd Edition Daniel
Kusswurm
https://ebookmeta.com/product/modern-x86-assembly-language-
programming-covers-x86-64-bit-avx-avx2-and-avx-512-3rd-edition-daniel-
kusswurm/
ebookmeta.com

Modern X86 Assembly Language Programming: Covers x86


64-bit, AVX, AVX2, and AVX-512 2nd Edition Daniel Kusswurm

https://ebookmeta.com/product/modern-x86-assembly-language-
programming-covers-x86-64-bit-avx-avx2-and-avx-512-2nd-edition-daniel-
kusswurm/
ebookmeta.com

Assembly Language for x86 Processors 6th edition Kip


Irvine

https://ebookmeta.com/product/assembly-language-
for-x86-processors-6th-edition-kip-irvine/

ebookmeta.com

Axonal Transport Alessio Vagnoni

https://ebookmeta.com/product/axonal-transport-alessio-vagnoni/

ebookmeta.com
Pick Your Poison 1st Edition Emma Nichole

https://ebookmeta.com/product/pick-your-poison-1st-edition-emma-
nichole/

ebookmeta.com

M A Entrance Exams in English Literature Precise Answers


Accurate Sources and Online Exams 1st Edition Mahdi
Javidshad Amirhossein Nemati
https://ebookmeta.com/product/m-a-entrance-exams-in-english-
literature-precise-answers-accurate-sources-and-online-exams-1st-
edition-mahdi-javidshad-amirhossein-nemati/
ebookmeta.com

Introduction to Electrochemical Science and Engineering


2nd Edition Lvov Serguei N

https://ebookmeta.com/product/introduction-to-electrochemical-science-
and-engineering-2nd-edition-lvov-serguei-n/

ebookmeta.com

Cambridge International AS A Level Chemistry Study and


Revision Guide Third Edition David Bevan

https://ebookmeta.com/product/cambridge-international-as-a-level-
chemistry-study-and-revision-guide-third-edition-david-bevan/

ebookmeta.com

Street Culture 50 Years of Subculture Style 1st Edition


Gavin Baddeley

https://ebookmeta.com/product/street-culture-50-years-of-subculture-
style-1st-edition-gavin-baddeley/

ebookmeta.com
Evangelicals on the Canterbury Trail Why Evangelicals Are
Attracted to the Liturgical Church Revised Edition Robert
Webber Lester Ruth
https://ebookmeta.com/product/evangelicals-on-the-canterbury-trail-
why-evangelicals-are-attracted-to-the-liturgical-church-revised-
edition-robert-webber-lester-ruth/
ebookmeta.com
x86-64
Assembly Language
Programming
with
Ubuntu

Ed Jorgensen, Ph.D.
Version 1.1.40
January 2020
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.jpg

Copyright © 2015, 2016, 2017, 2018, 2019 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.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.......................................................................................27
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....................................................63
6.2.5 Displaying Stack Contents........................................................................65
6.2.6 Debugger Commands File (interactive)....................................................65
6.2.6.1 Debugger Commands File (non-interactive).....................................66
6.2.6.2 Debugger Commands File (non-interactive).....................................66
6.3 Exercises............................................................................................................67
6.3.1 Quiz Questions..........................................................................................67
6.3.2 Suggested Projects.....................................................................................68
7.0 Instruction Set Overview....................................................................................69
7.1 Notational Conventions.....................................................................................69
7.1.1 Operand Notation......................................................................................70
7.2 Data Movement.................................................................................................71

Page v
Table of Contents

7.3 Addresses and Values........................................................................................73


7.4 Conversion Instructions.....................................................................................74
7.4.1 Narrowing Conversions.............................................................................74
7.4.2 Widening Conversions..............................................................................74
7.4.2.1 Unsigned Conversions.......................................................................74
7.4.2.2 Signed Conversions...........................................................................76
7.5 Integer Arithmetic Instructions.........................................................................78
7.5.1 Addition.....................................................................................................78
7.5.1.1 Addition with Carry...........................................................................81
7.5.2 Subtraction.................................................................................................83
7.5.3 Integer Multiplication................................................................................87
7.5.3.1 Unsigned Multiplication....................................................................87
7.5.3.2 Signed Multiplication........................................................................91
7.5.4 Integer Division.........................................................................................94
7.6 Logical Instructions.........................................................................................101
7.6.1 Logical Operations..................................................................................102
7.6.2 Shift Operations.......................................................................................103
7.6.2.1 Logical Shift....................................................................................103
7.6.2.2 Arithmetic Shift...............................................................................105
7.6.3 Rotate Operations....................................................................................107
7.7 Control Instructions.........................................................................................108
7.7.1 Labels......................................................................................................109
7.7.2 Unconditional Control Instructions.........................................................109
7.7.3 Conditional Control Instructions.............................................................109
7.7.3.1 Jump Out of Range..........................................................................112
7.7.4 Iteration....................................................................................................115
7.8 Example Program, Sum of Squares.................................................................117
7.9 Exercises..........................................................................................................118
7.9.1 Quiz Questions........................................................................................118
7.9.2 Suggested Projects...................................................................................122
8.0 Addressing Modes.............................................................................................125
8.1 Addresses and Values......................................................................................125
8.1.1 Register Mode Addressing......................................................................126
8.1.2 Immediate Mode Addressing..................................................................126
8.1.3 Memory Mode Addressing......................................................................126
8.2 Example Program, List Summation................................................................129
8.3 Example Program, Pyramid Areas and Volumes............................................131
8.4 Exercises..........................................................................................................136

Page vi
Table of Contents

8.4.1 Quiz Questions........................................................................................136


8.4.2 Suggested Projects...................................................................................138
9.0 Process Stack.....................................................................................................141
9.1 Stack Example.................................................................................................141
9.2 Stack Instructions............................................................................................142
9.3 Stack Implementation......................................................................................143
9.3.1 Stack Layout............................................................................................143
9.3.2 Stack Operations......................................................................................145
9.4 Stack Example.................................................................................................147
9.5 Exercises..........................................................................................................148
9.5.1 Quiz Questions........................................................................................148
9.5.2 Suggested Projects...................................................................................149
10.0 Program Development....................................................................................151
10.1 Understand the Problem................................................................................151
10.2 Create the Algorithm.....................................................................................152
10.3 Implement the Program.................................................................................154
10.4 Test/Debug the Program................................................................................156
10.5 Error Terminology.........................................................................................157
10.5.1 Assembler Error.....................................................................................157
10.5.2 Run-time Error.......................................................................................157
10.5.3 Logic Error............................................................................................157
10.6 Exercises........................................................................................................158
10.6.1 Quiz Questions......................................................................................158
10.6.2 Suggested Projects.................................................................................158
11.0 Macros..............................................................................................................161
11.1 Single-Line Macros.......................................................................................161
11.2 Multi-Line Macros........................................................................................162
11.2.1 Macro Definition...................................................................................162
11.2.2 Using a Macro.......................................................................................162
11.3 Macro Example.............................................................................................163
11.4 Debugging Macros........................................................................................165
11.5 Exercises........................................................................................................165
11.5.1 Quiz Questions......................................................................................165
11.5.2 Suggested Projects.................................................................................166
12.0 Functions..........................................................................................................167
12.1 Updated Linking Instructions........................................................................167

Page vii
Table of Contents

12.2 Debugger Commands....................................................................................168


12.2.1 Debugger Command, next.....................................................................168
12.2.2 Debugger Command, step.....................................................................168
12.3 Stack Dynamic Local Variables....................................................................168
12.4 Function Declaration.....................................................................................169
12.5 Standard Calling Convention........................................................................169
12.6 Linkage..........................................................................................................170
12.7 Argument Transmission................................................................................171
12.8 Calling Convention.......................................................................................171
12.8.1 Parameter Passing..................................................................................172
12.8.2 Register Usage.......................................................................................173
12.8.3 Call Frame.............................................................................................174
12.8.3.1 Red Zone........................................................................................176
12.9 Example, Statistical Function 1 (leaf)...........................................................176
12.9.1 Caller.....................................................................................................177
12.9.2 Callee.....................................................................................................177
12.10 Example, Statistical Function2 (non-leaf)...................................................178
12.10.1 Caller...................................................................................................179
12.10.2 Callee...................................................................................................180
12.11 Stack-Based Local Variables......................................................................183
12.12 Summary.....................................................................................................186
12.13 Exercises......................................................................................................187
12.13.1 Quiz Questions....................................................................................187
12.13.2 Suggested Projects...............................................................................188
13.0 System Services...............................................................................................191
13.1 Calling System Services................................................................................191
13.2 Newline Character.........................................................................................192
13.3 Console Output..............................................................................................193
13.3.1 Example, Console Output......................................................................194
13.4 Console Input................................................................................................197
13.4.1 Example, Console Input........................................................................198
13.5 File Open Operations....................................................................................202
13.5.1 File Open...............................................................................................202
13.5.2 File Open/Create....................................................................................203
13.6 File Read.......................................................................................................204
13.7 File Write.......................................................................................................205
13.8 File Operations Examples.............................................................................205
13.8.1 Example, File Write...............................................................................205

Page viii
Table of Contents

13.8.2 Example, File Read...............................................................................211


13.9 Exercises........................................................................................................216
13.9.1 Quiz Questions......................................................................................216
13.9.2 Suggested Projects.................................................................................217
14.0 Multiple Source Files......................................................................................219
14.1 Extern Statement...........................................................................................219
14.2 Example, Sum and Average..........................................................................220
14.2.1 Assembly Main......................................................................................220
14.2.2 Function Source.....................................................................................222
14.2.3 Assemble and Link................................................................................223
14.3 Interfacing with a High-Level Language......................................................224
14.3.1 Example, C++ Main / Assembly Function............................................224
14.3.2 Compile, Assemble, and Link...............................................................225
14.4 Exercises........................................................................................................226
14.4.1 Quiz Questions......................................................................................226
14.4.2 Suggested Projects.................................................................................227
15.0 Stack Buffer Overflow....................................................................................229
15.1 Understanding a Stack Buffer Overflow.......................................................230
15.2 Code to Inject................................................................................................231
15.3 Code Injection...............................................................................................234
15.4 Code Injection Protections............................................................................235
15.4.1 Data Stack Smashing Protector (or Canaries).......................................235
15.4.2 Data Execution Prevention....................................................................236
15.4.3 Data Address Space Layout Randomization.........................................236
15.5 Exercises........................................................................................................236
15.5.1 Quiz Questions......................................................................................236
15.5.2 Suggested Projects.................................................................................237
16.0 Command Line Arguments............................................................................239
16.1 Parsing Command Line Arguments..............................................................239
16.2 High-Level Language Example....................................................................240
16.3 Argument Count and Argument Vector Table..............................................241
16.4 Assembly Language Example.......................................................................242
16.5 Exercises........................................................................................................246
16.5.1 Quiz Questions......................................................................................246
16.5.2 Suggested Projects.................................................................................246
17.0 Input/Output Buffering..................................................................................249

Page ix
Table of Contents

17.1 Why Buffer?..................................................................................................249


17.2 Buffering Algorithm......................................................................................251
17.3 Exercises........................................................................................................254
17.3.1 Quiz Questions......................................................................................254
17.3.2 Suggested Projects.................................................................................255
18.0 Floating-Point Instructions............................................................................257
18.1 Floating-Point Values....................................................................................257
18.2 Floating-Point Registers................................................................................258
18.3 Data Movement.............................................................................................258
18.4 Integer / Floating-Point Conversion Instructions..........................................260
18.5 Floating-Point Arithmetic Instructions..........................................................262
18.5.1 Floating-Point Addition.........................................................................262
18.5.2 Floating-Point Subtraction.....................................................................263
18.5.3 Floating-Point Multiplication................................................................265
18.5.4 Floating-Point Division.........................................................................267
18.5.5 Floating-Point Square Root...................................................................269
18.6 Floating-Point Control Instructions...............................................................271
18.6.1 Floating-Point Comparison...................................................................271
18.7 Floating-Point Calling Conventions..............................................................274
18.8 Example Program, Sum and Average...........................................................275
18.9 Example Program, Absolute Value...............................................................276
18.10 Exercises......................................................................................................277
18.10.1 Quiz Questions....................................................................................278
18.10.2 Suggested Projects...............................................................................278
19.0 Parallel Processing..........................................................................................279
19.1 Distributed Computing..................................................................................280
19.2 Multiprocessing.............................................................................................280
19.2.1 POSIX Threads......................................................................................281
19.2.2 Race Conditions.....................................................................................282
19.3 Exercises........................................................................................................285
19.3.1 Quiz Questions......................................................................................285
19.3.2 Suggested Projects.................................................................................286
20.0 Interrupts.........................................................................................................287
20.1 Multi-user Operating System........................................................................287
20.1.1 Interrupt Classification..........................................................................288
20.1.2 Interrupt Timing....................................................................................288
20.1.2.1 Asynchronous Interrupts................................................................288

Page x
Table of Contents

20.1.2.2 Synchronous Interrupts..................................................................288


20.1.3 Interrupt Categories...............................................................................289
20.1.3.1 Hardware Interrupt.........................................................................289
20.1.3.1.1 Exceptions..............................................................................289
20.1.3.2 Software Interrupts........................................................................290
20.2 Interrupt Types and Levels............................................................................290
20.2.1 Interrupt Types......................................................................................290
20.2.2 Privilege Levels.....................................................................................290
20.3 Interrupt Processing.......................................................................................292
20.3.1 Interrupt Service Routine (ISR).............................................................292
20.3.2 Processing Steps....................................................................................292
20.3.2.1 Suspension.....................................................................................292
20.3.2.2 Obtaining ISR Address..................................................................292
20.3.2.3 Jump to ISR...................................................................................293
20.3.2.4 Suspension Execute ISR................................................................293
20.3.2.5 Resumption....................................................................................294
20.4 Suspension Interrupt Processing Summary...................................................294
20.5 Exercises........................................................................................................295
20.5.1 Quiz Questions......................................................................................295
20.5.2 Suggested Projects.................................................................................296
21.0 Appendix A – ASCII Table............................................................................297
22.0 Appendix B – Instruction Set Summary.......................................................299
22.1 Notation.........................................................................................................299
22.2 Data Movement Instructions.........................................................................300
22.3 Data Conversion instructions........................................................................300
22.4 Integer Arithmetic Instructions.....................................................................301
22.5 Logical, Shift, and Rotate Instructions..........................................................303
22.6 Control Instructions.......................................................................................305
22.7 Stack Instructions..........................................................................................307
22.8 Function Instructions.....................................................................................307
22.9 Floating-Point Data Movement Instructions.................................................307
22.10 Floating-Point Data Conversion Instructions..............................................308
22.11 Floating-Point Arithmetic Instructions........................................................309
22.12 Floating-Point Control Instructions.............................................................313
23.0 Appendix C – System Services.......................................................................315
23.1 Return Codes.................................................................................................315
23.2 Basic System Services...................................................................................315

Page xi
Table of Contents

23.3 File Modes.....................................................................................................317


23.4 Error Codes...................................................................................................318
24.0 Appendix D – Quiz Question Answers..........................................................321
24.1 Quiz Question Answers, Chapter 1...............................................................321
24.2 Quiz Question Answers, Chapter 2...............................................................321
24.3 Quiz Question Answers, Chapter 3...............................................................322
24.4 Quiz Question Answers, Chapter 4...............................................................324
24.5 Quiz Question Answers, Chapter 5...............................................................325
24.6 Quiz Question Answers, Chapter 6...............................................................326
24.7 Quiz Question Answers, Chapter 7...............................................................327
24.8 Quiz Question Answers, Chapter 8...............................................................330
24.9 Quiz Question Answers, Chapter 9...............................................................331
24.10 Quiz Question Answers, Chapter 10...........................................................331
24.11 Quiz Question Answers, Chapter 11...........................................................332
24.12 Quiz Question Answers, Chapter 12...........................................................332
24.13 Quiz Question Answers, Chapter 13...........................................................333
24.14 Quiz Question Answers, Chapter 14...........................................................333
24.15 Quiz Question Answers, Chapter 15...........................................................334
24.16 Quiz Question Answers, Chapter 16...........................................................334
24.17 Quiz Question Answers, Chapter 17...........................................................335
24.18 Quiz Question Answers, Chapter 18...........................................................335
24.19 Quiz Question Answers, Chapter 19...........................................................336
24.20 Quiz Question Answers, Chapter 20...........................................................336
25.0 Alphabetical Index..........................................................................................339

Page xii
Table of Contents

Illustration Index
Illustration 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....................................................................71
Illustration 15: Integer Multiplication Overview.............................................................88
Illustration 16: Integer Division Overview......................................................................96
Illustration 17: Logical Operations................................................................................102
Illustration 18: Logical Shift Overview.........................................................................104
Illustration 19: Logical Shift Operations.......................................................................104
Illustration 20: Arithmetic Left Shift.............................................................................106
Illustration 21: Arithmetic Right Shift...........................................................................106
Illustration 22: Process Memory Layout........................................................................144
Illustration 23: Process Memory Layout Example........................................................145
Illustration 24: Stack Frame Layout..............................................................................175
Illustration 25: Stack Frame Layout with Red Zone......................................................176
Illustration 26: Stack Call Frame Example....................................................................230
Illustration 27: Stack Call Frame Corruption.................................................................235
Illustration 28: Argument Vector Layout......................................................................242
Illustration 29: Privilege Levels.....................................................................................291
Illustration 30: Interrupt Processing Overview..............................................................294

Page xiii
Table of Contents

Page xiv
If you give someone a program, you will Chapter
frustrate them for a day; if you teach them 1
to program, you will frustrate them for a
lifetime.

1.0 Introduction
The purpose of this text is to provide a reference for University level assembly language
and systems programming courses. Specifically, this text addresses the x86-64 1
instruction set for the popular x86-64 class of processors using the Ubuntu 64-bit
Operating System (OS). While the provided code and various examples should work
under any Linux-based 64-bit OS, they have only been tested under Ubuntu 14.04 LTS
(64-bit).
The x86-64 is a Complex Instruction Set Computing (CISC2) CPU design. This refers
to the internal processor design philosophy. CISC processors typically include a wide
variety of instructions (sometimes overlapping), varying instructions sizes, and a wide
range of addressing modes. The term was retroactively coined in contrast to Reduced
Instruction Set Computer (RISC3).

1.1 Prerequisites
It must be noted that the text is not geared toward learning how to program. It is
assumed that the reader has already become proficient in a high-level programming
language. Specifically, the text is generally geared toward a compiled, C-based high-
level language such as C, C++, or Java. Many of the explanations and examples assume
the reader is already familiar with programming concepts such as declarations,
arithmetic operations, control structures, iteration, function calls, functions, indirection
(i.e., pointers), and variable scoping issues.
Additionally, the reader should be comfortable using a Linux-based operating system
including using the command line. If the reader is new to Linux, the Additional
References section has links to some useful documentation.

1 For more information, refer to: http://en.wikipedia.org/wiki/X86-64


2 For more information, refer to: http://en.wikipedia.org/wiki/Complex_instruction_set_computing
3 For more information, refer to: http://en.wikipedia.org/wiki/Reduced_instruction_set_computing

Page 1
Chapter 1.0 ◄ Introduction

1.2 What is Assembly Language


The typical question asked by students is 'why learn assembly?'. Before addressing that
question, let's clarify what exactly assembly language is.
Assembly language is machine specific. For example, code written for an x86-64
processor will not run on a different processor such as a RISC processor (popular in
tablets and smart-phones).
Assembly language is a “low-level” language and provides the basic instructional
interface to the computer processor. Assembly language is as close to the processor as
you can get as a programmer. Programs written in a high-level language are translated
into assembly language in order for the processor to execute the program. The high-
level language is an abstraction between the language and the actual processor
instructions. As such, the idea that “assembly is dead” is nonsense.
Assembly language gives you direct control of the system's resources. This involves
setting processor registers, accessing memory locations, and interfacing with other
hardware elements. This requires a significantly deeper understanding of exactly how
the processor and memory work.

1.3 Why Learn Assembly Language


The goal of this text is to provide a comprehensive introduction to programming in
assembly language. The reasons for learning assembly language are more about
understanding how a computer works instead of developing large programs. Since
assembly language is machine specific, the lack of portability is very limiting for
programming projects.
The process of actually learning assembly language involves writing non-trivial
programs to perform specific low-level actions including arithmetic operations, function
calls, using stack-dynamic local variables, and operating system interaction for activities
such as input/output. Just looking at small assembly language programs will not be
enough.
In the long run, learning the underlying principles, including assembly language, is what
makes the difference between a coding technician unable to cope with changing
languages and a computer scientist who is able to adapt to the ever-changing
technologies.
The following sections provide some detail on the various, more specific reasons for
learning assembly language.

Page 2
1.3.1 Gain a Better Understanding of Architecture Issues
Learning and spending some time working at the assembly language level provides a
richer understanding of the underlying computer architecture. This includes the basic
instruction set, processor registers, memory addressing, hardware interfacing, and Input/
Output. Since ultimately all programs execute at this level, knowing the capabilities of
assembly language provides useful insights into what is possible, what is easy, and what
might be more difficult or slower.

1.3.2 Understanding the Tool Chain


The tool chain is the name for the process of taking code written by a human and
converting it into something that the computer can directly execute. This includes the
compiler, or assembler in our case, the linker, the loader, and the debugger. In reference
to compiling, beginning programmers are told “just do this” with little explanation of
the complexity involved in the process. Working at the low-level can help provide the
basis for understanding and appreciating the details of the tool chain.

1.3.3 Improve Algorithm Development Skills


Working with assembly language and writing low-level programs helps programmers
improve algorithm development skills by practicing with a language that requires more
thought and more attention to detail. In the highly unlikely event that a program does
not work the first time, debugging assembly language also provides practice debugging
and requires a more nuanced approach since just adding a bunch of output statements is
more difficult at the assembly language level. This typically involves a more
comprehensive use of a debugger which is a useful skill for any programmer.

1.3.4 Improve Understanding of Functions/Procedures


Working with assembly language provides a greatly improved understanding of how
function/procedure calls work. This includes the contents and structure of the function
call frame, also referred to as the activation record. Depending on the specific instance,
the activation record might include stack-based arguments, preserved registers, and/or
stack dynamic local variables. There are some significant implementation and security
implications regarding stack dynamic local variables that are best understood working at
a low-level. Due to the security implications, it would be appropriate to remind readers
to always use their powers for good. Additionally, use of the stack and the associated
call frame is the basis for recursion and understanding the fairly straightforward
implementation of recursive functions.

Page 3
Chapter 1.0 ◄ Introduction

1.3.5 Gain an Understanding of I/O Buffering


In a high-level language, input/output instructions and the associated buffering
operations can appear magical. Working at the assembly language level and performing
some low-level input/output operations provides a more detailed understanding of how
input/output and buffering really works. This includes the differences between
interactive input/output, file input/output, and the associated operating system services.

1.3.6 Understand Compiler Scope


Programming with assembly language, after having already learned a high-level
language, helps ensure programmers understand the scope and capabilities of a
compiler. Specifically, this means learning what the compiler does and does not do in
relation to the computer architecture.

1.3.7 Introduction Multi-processing Concepts


This text will also provide a brief introduction to multi-processing concepts. The
general concepts of distributed and multi-core programming are presented with the
focus being placed on shared memory, threaded processing. It is the author’s belief that
truly understanding the subtle issues associated with threading such as shared memory
and race conditions is most easily understood at the low-level.

1.3.8 Introduction Interrupt Processing Concepts


The underlying fundamental mechanism in which modern multi-user computers work is
based on interrupts. Working at a low-level is the best place to provide an introduction
to the basic concepts associated with interrupt handling, interrupt service handles, and
vector interrupts.

1.4 Additional References


Some key references for additional information are noted in the following sections.
These references provide much more extensive and detailed information.
If any of these locations change, a web search will be able to find the new location.

Page 4
1.4.1 Ubuntu References
There is significant documentation available for the Ubuntu OS. The principal user
guide is as follows:
◦ Ubuntu Community Wiki
◦ Getting Started with Ubuntu 16.04
In addition, there are many other sites dedicated to providing help using Ubuntu (or
other Linux-based OS's).

1.4.2 BASH Command Line References


BASH is the default shell for Ubuntu. The reader should be familiar with basic
command line operations. Some additional references are as follows:
◦ Linux Command Line (on-line Tutorial and text)
◦ An Introduction to the Linux Command Shell For Beginners (pdf)
In addition, there are many other sites dedicated to providing information regarding the
BASH command shell.

1.4.3 Architecture References


Some key references published by Intel provide a detailed technical description of the
architecture and programming environment of Intel processors supporting IA-32 and
Intel 64 Architectures.
◦ Intel® 64 and IA-32 Architectures Software Developer's Manual: Basic
Architecture.
◦ Intel 64 and IA-32 Architectures Software Developer's Manual: Instruction
Set Reference.
◦ Intel 64 and IA-32 Architectures Software Developer's Manual: System
Programming Guide.
If the embedded links do not work, an Internet search can help find the new location.

1.4.4 Tool Chain References


The tool chain includes the assembler, linker, loader, and debugger. Chapter 5, Tool
Chain, provides an overview of the tool chain being used in this text. The following
references provide more detailed information and documentation.

Page 5
Chapter 1.0 ◄ Introduction

1.4.4.1 YASM References


The YASM assembler is an open source assembler commonly available on Linux-based
systems. The YASM references are as follows:
◦ Yasm Web Site
◦ Yasm Documentation
Additional information regarding YASM may be available a number of assembly
language sites and can be found through an Internet search.

1.4.4.2 DDD Debugger References


The DDD debugger is an open source debugger capable of supporting assembly
language.
◦ DDD Web Site
◦ DDD Documentation
Additional information regarding DDD may be at a number of assembly language sites
and can be found through an Internet search.

Page 6
Warning, keyboard not found. Press enter Chapter
to continue. 2

2.0 Architecture Overview


This chapter presents a basic, general overview of the x86-64 architecture. For a more
detailed explanation, refer to the additional references noted in Chapter 1, Introduction.

2.1 Architecture Overview


The basic components of a computer include a Central Processing Unit (CPU), Primary
Storage or Random Access Memory (RAM), Secondary Storage, Input/Output devices
(e.g., screen, keyboard, mouse), and an interconnection referred to as the Bus.
A very basic diagram of the computer architecture is as follows:

Primary Storage
CPU Random Access
Memory (RAM)

BUS
(Interconnection)

Screen / Keyboard / Secondary Storage


Mouse (i.e., SSD / Disk Drive /
Other Storage Media)

Illustration 1: Computer Architecture

Page 7
Chapter 2.0 ◄ Architecture Overview

The architecture is typically referred to as the Von Neumann Architecture 4, or the


Princeton architecture, and was described in 1945 by the mathematician and physicist
John von Neumann.
Programs and data are typically stored on secondary storage (e.g., disk drive or solid
state drive). When a program is executed, it must be copied from secondary storage into
the primary storage or main memory (RAM). The CPU executes the program from
primary storage or RAM.
Primary storage or main memory is also referred to as volatile memory since when
power is removed, the information is not retained and thus lost. Secondary storage is
referred to as non-volatile memory since the information is retained when powered off.
For example, consider storing a term paper on secondary storage (i.e., disk). When the
user starts to write or edit the term paper, it is copied from the secondary storage
medium into primary storage (i.e., RAM or main memory). When done, the updated
version is typically stored back to the secondary storage (i.e., disk). If you have ever
lost power while editing a document (assuming no battery or uninterruptible power
supply), losing the unsaved work will certainly clarify the difference between volatile
and non-volatile memory.

2.2 Data Storage Sizes


The x86-64 architecture supports a specific set of data storage size elements, all based
on powers of two. The supported storage sizes are as follows:
Storage Size (bits) Size (bytes)
Byte 8-bits 1 byte
Word 16-bits 2 bytes
Double-word 32-bits 4 bytes
Quadword 64-bits 8 bytes
Double quadword 128-bits 16 bytes

Lists or arrays (sets of memory) can be reserved in any of these types.


These storage sizes have a direct correlation to variable declarations in high-level
languages (e.g., C, C++, Java, etc.).

4 For more information, refer to: http://en.wikipedia.org/wiki/Von_Neumann_architecture

Page 8
For example, C/C++ declarations are mapped as follows:
C/C++ Declaration Storage Size (bits) Size (bytes)
char Byte 8-bits 1 byte
short Word 16-bits 2 bytes
int Double-word 32-bits 4 bytes
unsigned int Double-word 32-bits 4 bytes
long5 Quadword 64-bits 8 bytes
long long Quadword 64-bits 8 bytes
char * Quadword 64-bits 8 bytes
int * Quadword 64-bits 8 bytes
float Double-word 32-bits 4 bytes
double Quadword 64-bits 8 bytes

The asterisk indicates an address variable. For example, int * means the address of
an integer. Other high-level languages typically have similar mappings.

2.3 Central Processing Unit


The Central Processing Unit6 (CPU) is typically referred to as the “brains” of the
computer since that is where the actual calculations are performed. The CPU is housed
in a single chip, sometimes called a processor, chip, or die7. The cover image shows one
such CPU.
The CPU chip includes a number of functional units, including the Arithmetic Logic
Unit8 (ALU) which is the part of the chip that actually performs the arithmetic and
logical calculations. In order to support the ALU, processor registers9 and cache10
memory are also included “on the die” (term for inside the chip). The CPU registers and
cache memory are described in subsequent sections.
It should be noted that the internal design of a modern processor is quite complex. This
section provides a very simplified, high-level view of some key functional units within a
CPU. Refer to the footnotes or additional references for more information.

5 Note, the 'long' type declaration is compiler dependent. Type shown is for gcc and g++ compilers.
6 For more information, refer to: http://en.wikipedia.org/wiki/Central_processing_unit
7 For more information, refer to: http://en.wikipedia.org/wiki/Die_(integrated_circuit)
8 For more information, refer to: http://en.wikipedia.org/wiki/Arithmetic_logic_unit
9 For more information, refer to: http://en.wikipedia.org/wiki/Processor_register
10 For more information, refer to: http://en.wikipedia.org/wiki/Cache_(computing)

Page 9
Chapter 2.0 ◄ Architecture Overview

2.3.1 CPU Registers


A CPU register, or just register, is a temporary storage or working location built into the
CPU itself (separate from memory). Computations are typically performed by the CPU
using registers.

2.3.1.1 General Purpose Registers (GPRs)


There are sixteen, 64-bit General Purpose Registers (GPRs). The GPRs are described in
the following table. A GPR register can be accessed with all 64-bits or some portion or
subset accessed.

64-bit register Lowest Lowest Lowest


32-bits 16-bits 8-bits
rax eax ax al
rbx ebx bx bl
rcx ecx cx cl
rdx edx dx dl
rsi esi si sil
rdi edi di dil
rbp ebp bp bpl
rsp esp sp spl
r8 r8d r8w r8b
r9 r9d r9w r9b
r10 r10d r10w r10b
r11 r11d r11w r11b
r12 r12d r12w r12b
r13 r13d r13w r13b
r14 r14d r14w r14b
r15 r15d r15w r15b

Additionally, some of the GPR registers are used for dedicated purposes as described in
the later sections.

Page 10
When using data element sizes less than 64-bits (i.e., 32-bit, 16-bit, or 8-bit), the lower
portion of the register can be accessed by using a different register name as shown in the
table.
For example, when accessing the lower portions of the 64-bit rax register, the layout is
as follows:
← eax →
← ax →
rax = ah al

As shown in the diagram, the first four registers, rax, rbx, rcx, and rdx also allow the
bits 8-15 to be accessed with the ah, bh, ch, and dh register names. With the exception
of ah, these are provided for legacy support and will not be used in this text.
The ability to access portions of the register means that, if the quadword rax register is
set to 50,000,000,00010 (fifty billion), the rax register would contain the following value
in hex.
rax = 0000 000B A43B 7400

If a subsequent operation sets the word ax register to 50,00010 (fifty thousand, which is
C35016), the rax register would contain the following value in hex.
rax = 0000 000B A43B C350

In this case, when the lower 16-bit ax portion of the 64-bit rax register is set, the upper
48-bits are unaffected. Note the change in AX (from 740016 to C35016).
If a subsequent operation sets the byte sized al register to 5010 (fifty, which is 3216), the
rax register would contain the following value in hex.
rax = 0000 000B A43B C332

When the lower 8-bit al portion of the 64-bit rax register is set, the upper 56-bits are
unaffected. Note the change in AL (from 5016 to 3216).
For 32-bit register operations, the upper 32-bits is cleared (set to zero). Generally, this
is not an issue since operations on 32-bit registers do not use the upper 32-bits of the
register. For unsigned values, this can be useful to convert from 32-bits to 64-bits.
However, this will not work for signed conversions from 32-bit to 64-bit values.
Specifically, it will potentially provide incorrect results for negative values. Refer to
Chapter 3, Data Representation for additional information regarding the representation
of signed values.

Page 11
Chapter 2.0 ◄ Architecture Overview

2.3.1.2 Stack Pointer Register (RSP)


One of the CPU registers, rsp, is used to point to the current top of the stack. The rsp
register should not be used for data or other uses. Additional information regarding the
stack and stack operations is provided in Chapter 9, Process Stack.

2.3.1.3 Base Pointer Register (RBP)


One of the CPU registers, rbp, is used as a base pointer during function calls. The rbp
register should not be used for data or other uses. Additional information regarding the
functions and function calls is provided in Chapter 12, Functions.

2.3.1.4 Instruction Pointer Register (RIP)


In addition to the GPRs, there is a special register, rip, which is used by the CPU to
point to the next instruction to be executed. Specifically, since the rip points to the
next instruction, that means the instruction being pointed to by rip, and shown in the
debugger, has not yet been executed. This is an important distinction which can be
confusing when reviewing code in a debugger.

2.3.1.5 Flag Register (rFlags)


The flag register, rFlags, is used for status and CPU control information. The rFlag
register is updated by the CPU after each instruction and not directly accessible by
programs. This register stores status information about the instruction that was just
executed. Of the 64-bits in the rFlag register, many are reserved for future use.
The following table shows some of the status bits in the flag register.
Name Symbol Bit Use
Carry CF 0 Used to indicate if the previous operation
resulted in a carry.
Parity PF 2 Used to indicate if the last byte has an even
number of 1's (i.e., even parity).
Adjust AF 4 Used to support Binary Coded Decimal
operations.
Zero ZF 6 Used to indicate if the previous operation
resulted in a zero result.

Page 12
Sign SF 7 Used to indicate if the result of the
previous operation resulted in a 1 in the
most significant bit (indicating negative in
the context of signed data).
Direction DF 10 Used to specify the direction (increment or
decrement) for some string operations.
Overflow OF 11 Used to indicate if the previous operation
resulted in an overflow.

There are a number of additional bits not specified in this text. More information can be
obtained from the additional references noted in Chapter 1, Introduction.

2.3.1.6 XMM Registers


There are a set of dedicated registers used to support 64-bit and 32-bit floating-point
operations and Single Instruction Multiple Data (SIMD) instructions. The SIMD
instructions allow a single instruction to be applied simultaneously to multiple data
items. Used effectively, this can result in a significant performance increase. Typical
applications include some graphics processing and digital signal processing.
The XMM registers as follows:
128-bit Registers
xmm0
xmm1
xmm2
xmm3
xmm4
xmm5
xmm6
xmm7
xmm8
xmm9
xmm10
xmm11
xmm12

Page 13
Chapter 2.0 ◄ Architecture Overview

xmm13
xmm14
xmm15

Note, some of the more recent X86-64 processors support 256-bit XMM registers. This
will not be an issue for the programs in this text.
Additionally, the XMM registers are used to support the Streaming SIMD Extensions
(SSE). The SSE instructions are out of the scope of this text. More information can be
obtained from the Intel references (as noted in Chapter 1, Introduction).

2.3.2 Cache Memory


Cache memory is a small subset of the primary storage or RAM located in the CPU
chip. If a memory location is accessed, a copy of the value is placed in the cache.
Subsequent accesses to that memory location that occur in quick succession are
retrieved from the cache location (internal to the CPU chip). A memory read involves
sending the address via the bus to the memory controller, which will obtain the value at
the requested memory location, and send it back through the bus. Comparatively, if a
value is in cache, it would be much faster to access that value.
A cache hit occurs when the requested data can be found in a cache, while a cache miss
occurs when it cannot. Cache hits are served by reading data from the cache, which is
faster than reading from main memory. The more requests that can be served from
cache, the faster the system will typically perform. Successive generations of CPU
chips have increased cache memory and improved cache mapping strategies in order to
improve overall performance.

Page 14
A block diagram of a typical CPU chip configuration is as follows:

CPU Chip

Core 0 Core 1

L1 Cache L1 Cache

L2 Cache

BUS

Illustration 2: CPU Block Diagram

Current chip designs typically include an L1 cache per core and a shared L2 cache.
Many of the newer CPU chips will have an additional L3 cache.
As can be noted from the diagram, all memory accesses travel through each level of
cache. As such, there is a potential for multiple, duplicate copies of the value (CPU
register, L1 cache, L2 cache, and main memory). This complication is managed by the
CPU and is not something the programmer can change. Understanding the cache and
associated performance gain is useful in understanding how a computer works.

2.4 Main Memory


Memory can be viewed as a series of bytes, one after another. That is, memory is byte
addressable. This means each memory address holds one byte of information. To store
a double-word, four bytes are required which use four memory addresses.
Additionally, architecture is little-endian. This means that the Least Significant Byte
(LSB) is stored in the lowest memory address. The Most Significant Byte (MSB) is
stored in the highest memory location.

Page 15
Chapter 2.0 ◄ Architecture Overview

For a double-word (32-bits), the MSB and LSB are allocated as shown below.
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

MSB LSB

For example, assuming the value of, 5,000,00010 (004C4B4016), is to be placed in a


double-word variable named var1.
For a little-endian architecture, the memory picture would be as follows:

variable value Address


name (in hex)
? 0100100C
00 0100100B
4C 0100100A
4B 01001009

var1 → 40 01001008
? 01001007

Illustration 3: Little-Endian Data Layout

Based on the little-endian architecture, the LSB is stored in the lowest memory address
and the MSB is stored in the highest memory location.

Page 16
2.5 Memory Layout
The general memory layout for a program is as shown:

high memory stack


.
.
.
heap
BSS – uninitialized data
data
text (code)
low memory reserved

Illustration 4: General Memory Layout

The reserved section is not available to user programs. The text (or code) section is
where the machine language11 (i.e., the 1's and 0's that represent the code) is stored. The
data section is where the initialized data is stored. This includes declared variables that
have been provided an initial value at assemble-time. The uninitialized data section,
typically called BSS section, is where declared variables that have not been provided an
initial value are stored. If accessed before being set, the value will not be meaningful.
The heap is where dynamically allocated data will be stored (if requested). The stack
starts in high memory and grows downward.
Later sections will provide additional detail for the text and data sections.

2.6 Memory Hierarchy


In order to fully understand the various different memory levels and associated usage, it
is useful to review the memory hierarchy12. In general terms, faster memory is more
expensive and slower memory blocks are less expensive. The CPU registers are small,
fast, and expensive. Secondary storage devices such as disk drives and Solid State
11 For more information, refer to: http://en.wikipedia.org/wiki/Machine_code
12 For more information, refer to: http://en.wikipedia.org/wiki/Memory_hierarchy

Page 17
Chapter 2.0 ◄ Architecture Overview

Drives (SSD's) are larger, slower, and less expensive. The overall goal is to balance
performance with cost.
An overview of the memory hierarchy is as follows:

Smaller, faster, and


more expensive

CPU
Registers

Cache

Primary Storage
Main Memory (RAM)

Secondary Storage
(disk drives, SSD's, etc.)

Tertiary Storage
(remote storage, optical, backups, etc.)

Larger, slower, and


less expensive
Illustration 5: Memory Hierarchy

Where the top of the triangle represents the fastest, smallest, and most expensive
memory. As we move down levels, the memory becomes slower, larger, and less
expensive. The goal is to use an effective balance between the small, fast, expensive
memory and the large, slower, and cheaper memory.

Page 18
Some typical performance and size characteristics are as follows:

Memory Unit Example Size Typical Speed


Registers 16, 64-bit registers ~1 nanoseconds13
Cache Memory 4 - 8+ Megabytes14 ~5-60 nanoseconds
(L1 and L2)
Primary Storage 2 – 32+ Gigabytes15 ~100-150 nanoseconds
(i.e., main memory)
Secondary Storage 500 Gigabytes – ~3-15 milliseconds17
(i.e., disk, SSD's, etc.) 4+ Terabytes16

Based on this table, a primary storage access at 100 nanoseconds (100 ´ 10-9) is 30,000
times faster than a secondary storage access, at 3 milliseconds (3 ´ 10-3).
The typical speeds improve over time (and these are already out of date). The key point
is the relative difference between each memory unit is significant. This difference
between the memory units applies even as newer, faster SSDs are being utilized.

2.7 Exercises
Below are some questions based on this chapter.

2.7.1 Quiz Questions


Below are some quiz questions.
1) Draw a picture of the Von Neumann Architecture.
2) What architecture component connects the memory to the CPU?
3) Where are programs stored when the computer is turned off?
4) Where must programs be located when they are executing?
5) How does cache memory help overall performance?
6) How many bytes does a C++ integer declared with the declaration int use?
7) On the Intel X86-64 architecture, how many bytes can be stored at each address?

13 For more information, refer to: http://en.wikipedia.org/wiki/Nanosecond


14 For more information, refer to: http://en.wikipedia.org/wiki/Megabyte
15 For more information, refer to: http://en.wikipedia.org/wiki/Gigabyte
16 For more information, refer to: http://en.wikipedia.org/wiki/Terabyte
17 For more information, refer to: http://en.wikipedia.org/wiki/Millisecond

Page 19
Chapter 2.0 ◄ Architecture Overview

8) Given the 32-bit hex 004C4B4016 what is the:


1. Least Significant Byte (LSB)
2. Most Significant Byte (MSB)
9) Given the 32-bit hex 004C4B4016, show the little-endian memory layout showing
each byte in memory.
10) Draw a picture of the layout for the rax register.
11) How many bits does each of the following represent:
1. al
2. rcx
3. bx
4. edx
5. r11
6. r8b
7. sil
8. r14w
12) Which register points to the next instruction to be executed?
13) Which register points to the current top of the stack?
14) If al is set to 0516 and ax is set to 000716, eax is set to 0000002016, and rax is set
to 000000000000000016, and show the final complete contents of the complete
rax register.
15) If the rax register is set to 81,985,529,216,486,89510 (123456789ABCDEF16),
what are the contents of the following registers in hex?
1. al
2. ax
3. eax
4. rax

Page 20
There are 10 types of people in the world; Chapter
those that understand binary and those that 3
don't.

3.0 Data Representation


Data representation refers to how information is stored within the computer. There is a
specific method for storing integers which is different than storing floating-point values
which is different than storing characters. This chapter presents a brief summary of the
integer, floating-point, and ASCII representation schemes.
It is assumed the reader is already generally familiar with binary, decimal, and hex
numbering systems.
It should be noted that if not specified, a number is in base-10. Additionally, a number
preceded by 0x is a hex value. For example, 19 = 1910 = 1316 = 0x13.

3.1 Integer Representation


Representing integer numbers refers to how the computer stores or represents a number
in memory. The computer represents numbers in binary (1's and 0's). However, the
computer has a limited amount of space that can be used for each number or variable.
This directly impacts the size, or range, of the number that can be represented. For
example, a byte (8-bits) can be used to represent 2 8 or 256 different numbers. Those
256 different numbers can be unsigned (all positive) in which case we can represent any
number between 0 and 255 (inclusive). If we choose signed (positive and negative
values), then we can represent any number between -128 and +127 (inclusive).
If that range is not large enough to handle the intended values, a larger size must be
used. For example, a word (16-bits) can be used to represent 216 or 65,536 different
values, and a double-word (32-bits) can be used to represent 232 or 4,294,967,296
different numbers. So, if you wanted to store a value of 100,000 then a double-word
would be required.

Page 21
Chapter 3.0 ◄ Data Representation

As you may recall from C, C++, or Java, an integer declaration (e.g., int <variable>) is
a single double-word which can be used to represent values between -231
(−2,147,483,648) and +231 - 1 (+2,147,483,647).
The following table shows the ranges associated with typical sizes:

Size Size Unsigned Range Signed Range


Bytes (8-bits) 28 0 to 255 -128 to +127
Words (16-bits) 216
0 to 65,535 −32,768 to +32,767
Double-words (32-bits) 232 0 to 4,294,967,295 −2,147,483,648 to
+2,147,483,647
Quadword 264 0 to 264 - 1 -(263) to 263 - 1
Double quadword 2128 0 to 2128 - 1 -(2127) to 2127 - 1

In order to determine if a value can be represented, you will need to know the size of the
storage element (byte, word, double-word, quadword, etc.) being used and if the values
are signed or unsigned.
• For representing unsigned values within the range of a given storage size,
standard binary is used.
• For representing signed values within the range, two's complement is used.
Specifically, the two's complement encoding process applies to the values in the
negative range. For values within the positive range, standard binary is used.

For example, the unsigned byte range can be represented using a number line as follows:

0 255

For example, the signed byte range can also be represented using a number line as
follows:

-128 0 +127
The same concept applies to halfwords and words which have larger ranges.

Page 22
Since unsigned values have a different, positive only, range than signed values, there is
overlap between the values. This can be very confusing when examining variables in
memory (with the debugger).
For example, when the unsigned and signed values are within the overlapping positive
range (0 to +127):
• A signed byte representation of 1210 is 0x0C16
• An unsigned byte representation of -1210 is also 0x0C16

When the unsigned and signed values are outside the overlapping range:
• A signed byte representation of -1510 is 0xF116
• An unsigned byte representation of 24110 is also 0xF116

This overlap can cause confusion unless the data types are clearly and correctly defined.

3.1.1 Two's Complement


The following describes how to find the two's complement representation for negative
values (not positive values).
To take the two's complement of a number:
1. take the one's complement (negate)
2. add 1 (in binary)
The same process is used to encode a decimal value into two's complement and from
two's complement back to decimal. The following sections provide some examples.

3.1.2 Byte Example


For example, to find the byte size (8-bits), two's complement representation of -9 and -
12.

9 (8+1) = 00001001 12 (8+4) = 00001100


Step 1 11110110 Step 1: 11110011
Step 2 11110111 11110100

-9 (in hex) = F7 -12 (in hex) = F4

Note, all bits for the given size, byte in this example, must be specified.

Page 23
Chapter 3.0 ◄ Data Representation

3.1.3 Word Example


To find the word size (16-bits), two's complement representation of -18 and -40.

18 (16+2) = 0000000000010010 40 (32+8) = 0000000000101000


Step 1 1111111111101101 Step 1 1111111111010111
Step 2 1111111111101110 Step 2 1111111111011000

-18 (hex) = 0xFFEE -40 (hex) = 0xFFD8

Note, all bits for the given size, words in these examples, must be specified.

3.2 Unsigned and Signed Addition


As previously noted, the unsigned and signed representations may provide different
interpretations for the final value being represented. However, the addition and
subtraction operations are the same. For example:

241 11110001 -15 11110001


+ 7 00000111 + 7 00000111

248 11111000 -8 11111000

248 = F8 -8 = F8

The final result of 0xF8 may be interpreted as 248 for unsigned representation and -8 for
a signed representation. Additionally, 0xF816 is the º (degree symbol) in the ASCII
table.
As such, it is very important to have a clear definition of the sizes (byte, halfword, word,
etc.) and types (signed, unsigned) of data for the operations being performed.

3.3 Floating-point Representation


The representation issues for floating-point numbers are more complex. There are a
series of floating-point representations for various ranges of the value. For simplicity,
we will look primarily at the IEEE 754 32-bit floating-point standard.

Page 24
3.3.1 IEEE 32-bit Representation
The IEEE 754 32-bit floating-point standard is defined as follows:
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

s biased exponent fraction

Where s is the sign (0 => positive and 1 => negative). More formally, this can be
written as;
N = (−1) S × 1. F × 2E−127
When representing floating-point values, the first step is to convert floating-point value
into binary. The following table provides a brief reminder of how binary handles
fractional components:

23 22 21 20 2-1 2-2 2-3


... 8 4 2 1 . 1/2 1/4 1/8 ...
0 0 0 0 . 0 0 0

For example, 100.1012 would be 4.62510. For repeating decimals, calculating the binary
value can be time consuming. However, there is a limit since computers have finite
storage sizes (32-bits in this example).
The next step is to show the value in normalized scientific notation in binary. This
means that the number should have a single, non-zero leading digit to the left of the
decimal point. For example, 8.12510 is 1000.0012 (or 1000.0012 x 20) and in binary
normalized scientific notation that would be written as 1.000001 x 2 3 (since the decimal
point was moved three places to the left). Of course, if the number was 0.125 10 the
binary would be 0.0012 (or 0.0012 x 20) and the normalized scientific notation would be
1.0 x 2-3 (since the decimal point was moved three places to the right). The numbers
after the leading 1, not including the leading 1, are stored left-justified in the fraction
portion of the double-word.
The next step is to calculate the biased exponent, which is the exponent from the
normalized scientific notation plus the bias. The bias for the IEEE 754 32-bit floating-
point standard is 12710. The result should be converted to a byte (8-bits) and stored in
the biased exponent portion of the word.
Note, converting from the IEEE 754 32-bit floating-point representation to the decimal
value is done in reverse, however leading 1 must be added back (as it is not stored in the
word). Additionally, the bias is subtracted (instead of added).

Page 25
Chapter 3.0 ◄ Data Representation

3.3.1.1 IEEE 32-bit Representation Examples


This section presents several examples of encoding and decoding floating-point
representation for reference.

3.3.1.1.1 Example → -7.7510


For example, to find the IEEE 754 32-bit floating-point representation for -7.7510:

Example 1: -7.75
• determine sign -7.75 => 1 (since negative)
• convert to binary -7.75 = -0111.112
• normalized scientific notation = 1.1111 x 22
• compute biased exponent 210 + 12710 = 12910
◦ and convert to binary = 100000012
• write components in binary:
sign exponent mantissa
1 10000001 11110000000000000000000
• convert to hex (split into groups of 4)
11000000111110000000000000000000
1100 0000 1111 1000 0000 0000 0000 0000
C 0 F 8 0 0 0 0
• final result: C0F8 000016

3.3.1.1.2 Example → -0.12510


For example, to find the IEEE 754 32-bit floating-point representation for -0.12510:

Example 2: -0.125
• determine sign -0.125 => 1 (since negative)
• convert to binary -0.125 = -0.0012
• normalized scientific notation = 1.0 x 2-3
• compute biased exponent -310 + 12710 = 12410
◦ and convert to binary = 011111002
• write components in binary:
sign exponent mantissa
1 01111100 00000000000000000000000
• convert to hex (split into groups of 4)
10111110000000000000000000000000
1011 1110 0000 0000 0000 0000 0000 0000
B E 0 0 0 0 0 0
• final result: BE00 000016

Page 26
Other documents randomly have
different content
The Project Gutenberg eBook of The Young
Book Agent; or, Frank Hardy's Road to Success
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

Title: The Young Book Agent; or, Frank Hardy's Road to Success

Author: Jr. Horatio Alger

Illustrator: A. B. Shute

Release date: March 16, 2018 [eBook #56756]

Language: English

Credits: E-text prepared by David Edwards, Barry Abrahamsen,


and the Online Distributed Proofreading Team
(http://www.pgdp.net) from page images generously
made available by Internet Archive
(https://archive.org)

*** START OF THE PROJECT GUTENBERG EBOOK THE YOUNG


BOOK AGENT; OR, FRANK HARDY'S ROAD TO SUCCESS ***
E-text prepared by David Edwards, Barry Abrahamsen,
and the Online Distributed Proofreading Team
(http://www.pgdp.net)
from page images generously made available by
Internet Archive
(https://archive.org)

Note: Images of the original pages are available through Internet


Archive. See
https://archive.org/details/youngbookagentor00alge
“BOOKS! YOU GET RIGHT OUT OF THIS DOORWAY!”–P. 112.
THE YOUNG
BOOK AGENT
Or, Frank Hardy’s Road to Success

BY
HORATIO ALGER, JR.
AUTHOR OF “LOST AT SEA,” “NELSON THE NEWSBOY,” “OUT
FOR BUSINESS,” “YOUNG CAPTAIN JACK,” “RAGGED
DICK SERIES,” “TATTERED TOM
SERIES,” ETC.

NEW YORK
STITT PUBLISHING COMPANY
1905
Copyright, 1905

BY

STITT PUBLISHING COMPANY


PREFACE

Many years ago the author of the present volume resolved to write
a long series of books describing various phases of village and city
life, taking up in their turn the struggles of the bootblacks, the
newsboys, the young peddlers, the street musicians—the lives, in
fact, of all those who, though young in years, have to face the bitter
necessity of earning their own living.
In the present story are described the ups and downs of a boy
book agent, who is forced, through the misfortunes of his father, to
help provide for the family to which he belongs. He knows nothing of
selling books, when he starts, but he acquires a valuable experience
rapidly, and in the end gains a modest success which is well
deserved.
It is the custom of many persons in ordinary life to sneer at a
book agent and show him scant courtesy, forgetting that the agent’s
business is a perfectly legitimate one and that he is therefore
entitled to due respect so long as he does that which is proper and
gentlemanly. A kind word costs nothing, and it often cheers up a
heart which would otherwise be all but hopelessly depressed.
After reading this volume it may be thought by some that the
hero, Frank Hardy, is above his class in tact, intelligence, and
perseverance. This, however, is not true. A book agent, or, in fact, an
agent of any kind, must possess all of these qualities in a marked
degree, otherwise he will undoubtedly make a failure of the
undertaking. As in every other calling, to win success one must first
deserve it.
CONTENTS

CHAPTER PAGE
I. Frank at Home 1
II. Down at the Wreck 9
III. Disagreeable News 17
IV. The Hunt for a Missing Man 25
V. Frank at the Store 34
VI. The Rival Merchants 42
VII. A Fourth of July Celebration 50
VIII. Frank Looks for Work 58
IX. Frank Meets a Book Agent 67
X. Frank Goes to New York 76
XI. Frank as an Agent 86
XII. A Bright Beginning 96
XIII. Frank on the Road 108
XIV. A Boy Runaway 118
XV. Caught in a Storm 127
XVI. An Important Sale 136
XVII. A Curious Happening 145
XVIII. The Would-be Actor 153
XIX. Giving an Autograph 162
XX. Frank’s Remarkable Find 171
XXI. Gabe Flecker Shows His Hand 180
XXII. The Rival Book Agent 189
XXIII. News from Home 197
XXIV. Lost in a Coal Mine 205
XXV. Frank Meets Flecker Again 214
XXVI. An Escape 224
XXVII. At Home Once More 232
XXVIII. Frank Starts for the South 242
XXIX. A Scene on the Train 249
XXX. Frank Meets His Brother Mark 257
XXXI. A Clever Capture—Conclusion 264
THE YOUNG BOOK AGENT
CHAPTER I

FRANK AT HOME

Frank Hardy came up the short garden path whistling merrily to


himself. He was a tall, good-natured looking boy of sixteen, with
dark eyes and dark, curly hair.
“One more week of school and then hurrah for a long vacation in
the country!” he murmured to himself as he mounted the piazza
steps. “Oh, but won’t we have a dandy time swimming and fishing
when we get to Cloverdale!”
His little dog Frisky was at the door to greet him with short, sharp
barks of pleasure. Frank caught the animal up and began to coddle
him.
“Glad to see me, eh?” he cried. “Frisky, won’t you be glad when
we get to the country and you can roam all over the fields?”
For answer the dog barked again and wagged his tail vigorously.
Still holding the animal, Frank entered the dining room and passed
into the kitchen, where his mother was assisting the servant in the
preparation of the evening meal.
“Mother, is father back from Philadelphia yet?” he asked, as he
hung up his cap and slipped into the sink pantry to wash his hands.
“Not yet, Frank,” answered Mrs. Hardy.
“He must have quite some business to attend to, to stay away so
late. I thought I was late myself.”
“You are late, Frank—it is quarter after six. I expected your father
in on the half-past five train, but he must have missed that.”
“Then he won’t be here until nearly eight o’clock. Must I wait for
my supper?”
“No; we can have our supper directly. I know you must be
hungry.”
“I am, mother. Baseball gives a fellow an appetite, especially if he
runs bases and plays in the field, as I did. We played the Hopeville
Stars and beat them 12 to 7. I made three runs.”
“You must certainly love the game?”
“I do. Sometimes I wish I could be a professional ball player.”
“I shouldn’t wish you to be that, Frank. I want you to go to college
and be a professional man,” added Mrs. Hardy, with a fond smile.
“Oh, I was only talking, mother. But some professional ball players
are college men.”
Frank entered the dining room and sat down to the table. He was
soon joined by his little brother, Georgie, and his sister, Ruth, who
was twelve years of age.
“How do you get along with your lessons?” he asked of Ruth, who
had been practicing on the piano in the parlor.
“I think I am doing real well,” returned the sister, who was very
fair, with golden hair and bright blue eyes. “Professor Hartman says I
will make a good player if I do plenty of practicing. And, oh, I love it
so!” added the girl, enthusiastically.
“The one who loves it is the one who is bound to make a good
player,” said Frank. “Now, there is Dan Dixon. His folks want him to
learn to play the violin, and he takes lessons. But he doesn’t like it at
all, and I am sure he will never make a player.”
“That is true in all things,” came from Mrs. Hardy, as she sat down
to pour the tea. “If one wants to do well at anything, one’s heart
must be in the work. I once knew a girl whose family wanted her to
learn how to paint. She hadn’t any talent for it, and though she took
lessons for two years she never drew or painted anything really
worth showing.”
“I know what I like real well,” came from little Georgie. “I’m going
to keep a candy store when I grow up. I like that real well.”
“Good for you, Georgie!” laughed Frank. “Only don’t eat up all the
stock yourself.”
“Will you buy from me when I keep the store?” continued the little
fellow.
“To be sure, I will—or, maybe, I’ll be a salesman for you—and
Ruth can be the cashier.”
“What’s a cashier?”
“The one who takes in the money.”
“No, I want to take in the money myself,” came from Georgie,
promptly.
Thus the talking went on, and while it is in progress and the family
are waiting for the return of Mr. Hardy from his business trip, let me
take the opportunity of introducing them more specifically than I
have already done.
The Hardy family were six in number, Mr. Thomas Hardy and his
wife; Mark, who was three years older than Frank, and the children
already introduced.
Mr. Hardy was a flour and feed dealer, and at one time had had
the principal store in that line in Claster, the town in which the family
resided. He had made considerable money, and the family were
counted well to do. But during the past two years two rivals with
capital had come into the field, and trade with the flour and feed
merchant had consequently fallen off greatly.
Mr. Hardy had expected to send his oldest son, Mark, to college,
but the youth had begged to be allowed to take an ocean trip, and
had at last been allowed to ship on a voyage to South America. He
was to return home in seven or eight months, but during the past
three months nothing had been heard of him.
Frank, Ruth, and little Georgie all attended the same school in
Claster, Georgie being in the kindergarten, and Ruth in one of the
grammar grades. Frank was in the graduating class, and after a
vacation in the country, expected to prepare himself for high school.
He was just now deep in his final examinations at the grammar
school, and so far had done well, much to his parents’ satisfaction.
“Mother, what took father to Philadelphia?” asked Frank, after a
spell of silence, during which he had devoted himself to the viands
set before him.
At this question a shade of anxiety crossed Mrs. Hardy’s face.
“He went on very important business, Frank. I cannot explain to
you exactly what it was. He was to see Mr. Garrison, the man he
used to buy flour from.”
“Jabez Garrison?”
“Yes.”
“I never liked that man, mother; did you?”
“I really can’t say, Frank—I never had much to do with him.”
“I saw him at the store several times—doing business with father.
He somehow put me in mind of a snake.”
“Oh, Frank!” burst in Ruth.
“A man don’t look like a snake,” was little Georgie’s sober
comment.
“That is not a very complimentary thing to say, Frank,” said Mrs.
Hardy, somewhat severely.
“I can’t help it, mother. He has such an oily, smooth manner about
him.”
“Your father has spoken of him as a very good friend in business. I
believe he gave your father prices which were better than he could
get elsewhere.”
“Well, he didn’t look it. If I were father, I’d keep my eyes on him.”
“He went to Philadelphia to make inquiries about Mr. Garrison. I
cannot tell you more than that just now.”
“Didn’t father loan him some money?”
“Not exactly that; but he went his security when Mr. Garrison was
made treasurer of a certain benevolent order in Philadelphia.”
“How much security?”
“Ten thousand dollars.”
“That’s a big sum of money.”
“Yes, Frank—but I was told that it was more a matter of form than
anything else.”
“I don’t see it, mother. If Jabez Garrison had a lot of money to
handle, he could steal it if he wanted to.”
“Frank, you are certainly not in love with Mr. Garrison. Did he ever
say anything to you?”
“Not a word. Only I don’t like his looks, that’s all.”
Further talk on this subject was cut off by Ruth, who chanced to
look out of the bay window of the dining room.
“There goes the hospital ambulance,” she cried. “Somebody must
be hurt.”
Frank, filled with curiosity, leaped up and ran to the front door,
and then down to the gate.
“What’s the trouble?” he asked of a boy who was running past.
“Big accident on the railroad, down at Barber’s Cut,” answered the
boy. “Freight train ran into the Philadelphia local, and about a dozen
passengers have been killed or hurt.”
“The Philadelphia local!” echoed Frank, and for the moment his
heart almost stopped beating. “Can father have been on that train?”
He ran back into the house and told his mother the news. Mrs.
Hardy was almost prostrated, but quickly recovered.
“I will go down and see if your father is in that wreck,” she said.
“Frank, you can go along.” And a moment later they set out for the
scene of the disaster.
CHAPTER II

DOWN AT THE WRECK

Claster was a thriving town of four thousand inhabitants, with


several churches and schools, a bank, two weekly newspapers, and
six blocks of stores. There was a neat railroad station at which two
score of trains stopped daily, bound either north or south, for the
line ran from Philadelphia to Jersey City.
Barber’s Cut was a nasty curve on the line, just south of the town.
Here there was a rocky hill, and in one spot the cut was twenty feet
deep. At the end of the cut was a hollow where a railroad bridge
crossed Claster Creek.
Frank and his mother found a great many of the townspeople
hurrying to the scene of the wreck. All sorts of rumors were afloat,
and it was said the passenger cars were on fire, and the helpless
inmates were being roasted alive. The local fire department was
called out, but fortunately the fire was confined to a freight car
loaded with unfinished wagon wheels, so but comparatively little
damage was done through the conflagration.
The rumor that a dozen passengers had been killed or hurt was
false. But four people on the passenger train had been injured, and
only one severely—this man having several ribs crushed in and an
arm broken.
“I don’t see anything of father,” said Frank, after he and his
mother had looked at three of the injured persons. “I guess he
wasn’t on this train after all.”
“It is very fortunate.”
“Your father was on this train,” said a man standing near. “I was
talking to him just a short while before the smash-up occurred.”
“Oh!” ejaculated Mrs. Hardy. “Then where is he now?”
“There he is!” burst out Frank, and pointed to a form which four
men were carrying from a wrecked car. “Mother, he is—is hurt. You
had better go back and I’ll—I’ll tend to him.” Frank found he could
scarcely speak, he was so agitated.
“My husband!” murmured Mrs. Hardy, and ran forward with Frank
at her side. “Oh, tell me, he is not—not dead?”
“No, ma’am, he isn’t dead,” came promptly from one of the men.
“He got his foot crushed, and he’s fainted, that’s all.”
“Thank Heaven it is no worse!” murmured Mrs. Hardy, and when
the men laid her husband on the grass above the cut, she knelt
beside him, and sent Frank down to the creek for some water with
which to wash Mr. Hardy’s face, for it was covered with dust and dirt.
As Frank ran down to the creek for the water he saw something
shiny lying in the grass. He picked the object up, and was surprised
to learn that it was a silver spectacle case, containing a fine pair of
gold-rimmed spectacles.
“Somebody dropped those in the excitement,” he reasoned. “I’ll
have to look for the owner later;” and he shoved the case into his
pocket.
Of the four that had been hurt two were removed to the hospital
and the others were taken to their homes. Mr. Hardy was carried to
his residence, and there his physician and his family did all they
could to make him comfortable.
“The foot is in rather bad shape,” said Doctor Basswood. “Yet I
feel certain I can bring it around so you can walk on it as before. But
it will take time.”
“How much time, doctor?” questioned Mr. Hardy, faintly.
“Four or five months, and perhaps longer. But that is much better
than having your foot amputated.”
“True. But I can’t afford to lay around the house for six months.”
At this the physician shrugged his shoulders.
“It’s the best I can do, Mr. Hardy.”
“Oh, it is not your fault, doctor. But——” Mr. Hardy paused.
“You are thinking of your store?”
“Yes.”
“It is a pity your son, Frank, isn’t older. He might be able to run it
for you.”
“Unfortunately, Frank knows little or nothing about the business. I
have kept him at school.”
“Perhaps you can get a good man to run it for you.”
“Perhaps. I don’t know what I’ll do yet.”
“What do you do when you go away, as you did to-day?”
“I lock the place up, and leave a slate out for orders. Trade is not
as brisk as it used to be.”
“You mean as it was before Benning and Jack Peterson started in
the business?”
“That’s it. The town can’t support three flour and feed stores.”
“Won’t your old customers stick by you?”
“A few of them do; but both Benning and Peterson are doing their
best to get the trade away from me. They offer all sorts of
inducements, and sometimes sell at less than the goods cost, just to
get a customer.”
“Nobody in business can afford to do that very long.”
“They want to drive me out, and each wants to drive out the
other. Then the one who is left will make prices to suit himself;” and
here Mr. Hardy had to stop talking, for he felt very much exhausted.
In the meantime Frank had been sent down to the drug store for
several articles which the doctor had said were needed for the
injured man. While he was waiting for the articles a burly and rather
pleasant-faced man came in and purchased a handful of cigars.
“Is there an optician in town?” questioned the man of the
druggist. “I was in that wreck, and somehow I lost my glasses, and I
want to get another pair.”
“The watchmaker across the way keeps spectacles,” answered the
druggist. “But if he can fit you or not I don’t know.”
“I’ll try him,” said the man, and started for the door.
“Excuse me,” put in Frank, stepping up. “What sort of spectacles
did you drop?”
“Did you find them?”
“Perhaps I did.”
“Mine were in a silver case. They are thick glasses, with a gold
frame.”
“Then these must be yours,” and Frank drew the case from his
pocket and passed it over.
“They are mine!” cried the burly man, and looked well pleased to
have his property returned to him. “Where did you find them?”
“In the grass between the wreck and the creek. I was down at the
creek getting some water for my father, who was hurt. I almost
stepped on the case.”
“I see. So your father was hurt. Which one was he?”
“He had his foot crushed.”
“Oh, yes, I remember. They took him to your home up the street.”
“Yes, sir.”
“I hope the hurt isn’t serious?”
“It’s bad enough. But Doctor Basswood says he can save the foot.”
“Well, that’s a great consolation. It’s no fun to have a foot cut off.
May I ask your name?”
“Frank Hardy.”
“Mine is Philip Vincent. I am very much obliged for returning the
glasses to me.”
“Oh, that’s all right, Mr. Vincent. I was going to hunt up the owner
as soon as everything was all right at our house.”
“These glasses are a very fine pair, and I prize them exceedingly.
Let me reward you for returning them,” and Philip Vincent put his
hand in his pocket.
“I don’t want a reward, sir,” said Frank, promptly.
“But I want to show you that I appreciate having them returned,”
insisted the burly gentleman.
“It’s all right.”
“I’ll tell you what I’ll do. I’m in the book business in New York. I’ll
send you a good boy’s book. How will that suit you?” and the
gentleman smiled blandly.
“I must say I never go back on a good story book,” answered
Frank, honestly.
“Most boys like to read. I suppose you go to school here?”
“Yes, sir.”
“Well, I shan’t forget you,” concluded Philip Vincent, and shaking
hands, he left the drug store.
“What a pleasant kind of a man,” thought Frank. “I’d like to see
more of him.” And then he wondered what sort of a story book Mr.
Vincent would send him.
A little later Frank obtained the articles needed from the druggist,
and then he started for home. He did not dream of the disagreeable
surprise which was in store for him.

You might also like