Front Cover
Modern Assembly Language Programming with the ARM Processor
Copyright
Contents
List of Tables
List of Figures
List of Listings
Preface
Choice of Processor Family
General Approach
Companion Website
Acknowledgments
Part I: Assembly as a Language
Chapter 1: Introduction
1.1 Reasons to Learn Assembly
1.2 The ARM Processor
1.3 Computer Data
1.3.1 Representing Natural Numbers
1.3.2 Base Conversion
Base b to decimal
Decimal to base b
Conversion between arbitrary bases
Bases that are powers-of-two
1.3.3 Representing Integers
Sign-magnitude representation
Excess-(2n-1 -1) representation
Complement representation
1.3.4 Representing Characters
Non-printing characters
Converting character strings to ASCII codes
Interpreting data as ASCII strings
ISO extensions to ASCII
Unicode and UTF-8
1.4 Memory Layout of an Executing Program
1.5 Chapter Summary
Exercises
Chapter 2: GNU Assembly Syntax
2.1 Structure of an Assembly Program
2.1.1 Labels
2.1.2 Comments
2.1.3 Directives
2.1.4 Assembly Instructions
2.2 What the Assembler Does
2.3. GNU Assembly Directives
2.3.1 Selecting the Current Section
2.3.2 Allocating Space for Variables and Constants
2.3.3 Filling and Aligning
2.3.4 Setting and Manipulating Symbols
2.3.5 Conditional Assembly
2.3.6 Including Other Source Files
2.3.7 Macros
Macro example
Recursive macro example
2.4 Chapter Summary
Exercises
Chapter 3: Load/Store and Branch Instructions
3.1 CPU Components and Data Paths
3.2 ARM User Registers
3.3 Instruction Components
3.3.1 Setting and Using Condition Flags
3.3.2 Immediate Values
3.4 Load/Store Instructions
3.4.1 Addressing Modes
3.4.2 Load/Store Single Register
Syntax
Operations
Examples
3.4.3 Load/Store Multiple Registers
Syntax
Operations
Examples
3.4.4 Swap
Syntax
Operations
Example
3.4.5 Exclusive Load/Store
Syntax
Operations
Example
3.5 Branch Instructions
3.5.1 Branch
Syntax
Operations
Examples
3.5.2 Branch and Link
Syntax
Operations
Examples
3.6 Pseudo-Instructions
3.6.1 Load Immediate
Syntax
Operations
Example
3.6.2 Load Address
Syntax
Operations
Examples
3.7 Chapter Summary
Exercises
Chapter 4: Data Processing and Other Instructions
4.1 Data Processing Instructions
4.1.1 Operand2
4.1.2 Comparison Operations
Syntax
Operations
Examples
4.1.3 Arithmetic Operations
Syntax
Operations
Examples
4.1.4 Logical Operations
Syntax
Operations
Examples
4.1.5 Data Movement Operations
Syntax
Operations
Examples
4.1.6 Multiply Operations with 32-bit Results
Syntax
Operations
Examples
4.1.7 Multiply Operations with 64-bit Results
Syntax
Operations
Examples
4.1.8 Division Operations
Syntax
Operations
Examples
4.2 Special Instructions
4.2.1 Count Leading Zeros
Syntax
Operations
Example
4.2.2 Accessing the CPSR and SPSR
Syntax
Operations
Examples
4.2.3 Software Interrupt
Syntax
Operations
Example
4.2.4 Thumb Mode
Syntax
Operations
Example
4.3 Pseudo-Instructions
4.3.1 No Operation
Syntax
Operations
Examples
4.3.2 Shifts
Syntax
Operations
Examples
4.4 Alphabetized List of ARM Instructions
4.5 Chapter Summary
Exercises
Chapter 5: Structured Programming
5.1 Sequencing
5.2 Selection
5.2.1 Using Conditional Execution
5.2.2 Using Branch Instructions
5.2.3 Complex Selection
5.3 Iteration
5.3.1 Pre-Test Loop
5.3.2 Post-Test Loop
5.3.3 For Loop
Pre-test conversion
Post-test conversion
5.4 Subroutines
5.4.1 Advantages of Subroutines
5.4.2 Disadvantages of Subroutines
5.4.3 Standard C Library Functions
5.4.4 Passing Arguments
5.4.5 Calling Subroutines
Passing arguments in registers
Passing arguments on the stack
5.4.6 Writing Subroutines
5.4.7 Automatic Variables
5.4.8 Recursive Functions
5.5 Aggregate Data Types
5.5.1 Arrays
5.5.2 Structured Data
5.5.3 Arrays of Structured Data
5.6 Chapter Summary
Exercises
Chapter 6: Abstract Data Types
6.1 ADTs in Assembly Language
6.2 Word Frequency Counts
6.2.1 Sorting by Word Frequency
6.2.2 Better Performance
6.3 Ethics Case Study: Therac-25
6.3.1 History of the Therac-25
6.3.2 Overview of Design Flaws
6.4 Chapter Summary
Exercises
Part II: Performance Mathematics
Chapter 7: Integer Mathematics
7.1 Subtraction by Addition
7.2 Binary Multiplication
7.2.1 Multiplication by a Power of Two
7.2.2 Multiplication of Two Variables
7.2.3 Multiplication of a Variable by a Constant
7.2.4 Signed Multiplication
7.2.5 Multiplying Large Numbers
7.3 Binary Division
7.3.1 Division by a Power of Two
7.3.2 Division by a Variable
7.3.3 Division by a Constant
7.3.4 Dividing Large Numbers
7.4 Big Integer ADT
7.5 Chapter Summary
Exercises
Chapter 8: Non-Integral Mathematics
8.1 Base Conversion of Fractional Numbers
8.1.1 Arbitrary Base to Decimal
8.1.2 Decimal to Arbitrary Base
Bases that are powers-of-two
8.2 Fractions and Bases
Implications
8.3 Fixed-Point Numbers
8.3.1 Interpreting Fixed-Point Numbers
8.3.2 Q Notation
8.3.3 Properties of Fixed-Point Numbers
8.4 Fixed-Point Operations
8.4.1 Fixed-Point Addition and Subtraction
8.4.2 Fixed Point Multiplication
8.4.3 Fixed Point Division
Results of fixed point division
Maintaining precision
8.4.4 Division by a Constant
Division by constant 23
Division by constant -50
8.5 Floating Point Numbers
8.5.1 IEEE 754 Half-Precision
Examples
8.5.2 IEEE 754 Single-Precision
8.5.3 IEEE 754 Double-Precision
8.5.4 IEEE 754 Quad-Precision
8.6 Floating Point Operations
8.6.1 Floating Point Addition and Subtraction
8.6.2 Floating Point Multiplication and Division
8.7 Computing Sine and Cosine
8.7.1 Formats for the Powers of x
8.7.2 Formats and Constants for the Factorial Terms
8.7.3 Putting it All Together
8.7.4 Performance Comparison
8.8 Ethics Case Study: Patriot Missile Failure
8.9 Chapter Summary
Exercises
Chapter 9: The ARM Vector Floating Point Coprocessor
9.1 Vector Floating Point Overview
9.2 Floating Point Status and Control Register
9.2.1 Performance Versus Compliance
Full-compliance mode
RunFast mode
9.2.2 Vector Mode
9.3 Register Usage Rules
9.4 Load/Store Instructions
9.4.1 Load/Store Single Register
Syntax
Operations
Examples
9.4.2 Load/Store Multiple Registers
Syntax
Operations
Examples
9.5 Data Processing Instructions
9.5.1 Copy, Absolute Value, Negate, and Square Root
Syntax
Operations
Examples
9.5.2 Add, Subtract, Multiply, and Divide
Syntax
Operations
Examples
9.5.3 Compare
Syntax
Operations
Examples
9.6 Data Movement Instructions
9.6.1 Moving Between Two VFP Registers
Syntax
Operations
Examples
9.6.2 Moving Between VFP Register and One Integer Register
Syntax
Operations
Examples
9.6.3 Moving Between VFP Register and Two Integer Registers
Syntax
Operations
Examples
9.6.4 Move Between ARM Register and VFP System Register
Syntax
Operations
Examples
9.7 Data Conversion Instructions
9.7.1 Convert Between Floating Point and Integer
Syntax
Operation
Examples
9.7.2 Convert Between Fixed Point and Single Precision
Syntax
Operations
Examples
9.8 Floating Point Sine Function
9.8.1 Sine Function Using Scalar Mode
9.8.2 Sine Function Using Vector Mode
9.8.3 Performance Comparison
9.9 Alphabetized List of VFP Instructions
9.10 Chapter Summary
Exercises
Chapter 10: The ARM NEON Extensions
10.1 NEON Intrinsics
10.2 Instruction Syntax
10.3 Load and Store Instructions
10.3.1 Load or Store Single Structure Using One Lane
Syntax
Operations
Examples
10.3.2 Load Copies of a Structure to All Lanes
Syntax
Operations
Examples
10.3.3 Load or Store Multiple Structures
Syntax
Operations
Examples
10.4 Data Movement Instructions
10.4.1 Moving Between NEON Scalar and Integer Register
Syntax
Operations
Examples
10.4.2 Move Immediate Data
Syntax
Operations
Examples
10.4.3 Change Size of Elements in a Vector
Syntax
Operations
Examples
10.4.4 Duplicate Scalar
Syntax
Operations
Examples
10.4.5 Extract Elements
Syntax
Operation
Examples
10.4.6 Reverse Elements
Syntax
Operation
Examples
10.4.7 Swap Vectors
Syntax
Operation
Examples
10.4.8 Transpose Matrix
Syntax
Operation
Examples
10.4.9 Table Lookup
Syntax
Operations
Examples
10.4.10 Zip or Unzip Vectors
Syntax
Operations
Examples
10.5 Data Conversion
10.5.1 Convert Between Fixed Point and Single-Precision
Syntax
Operations
Examples
10.5.2 Convert Between Half-Precision and Single-Precision
Syntax
Operations
Examples
10.6 Comparison Operations
10.6.1 Vector Compare
Syntax
Operations
Examples
10.6.2 Vector Absolute Compare
Syntax
Operations
Examples
10.6.3 Vector Test Bits
Syntax
Operations
Examples
10.7 Bitwise Logical Operations
10.7.1 Bitwise Logical Operations
Syntax
Operations
Examples
10.7.2 Bitwise Logical Operations with Immediate Data
Syntax
Operations
Examples
10.7.3 Bitwise Insertion and Selection
Syntax
Operations
Examples
10.8 Shift Instructions
10.8.1 Shift Left by Immediate
Syntax
Operations
Examples
10.8.2 Shift Left or Right by Variable
Syntax
Operations
Examples
10.8.3 Shift Right by Immediate
Syntax
Operations
Examples
10.8.4 Saturating Shift Right by Immediate
Syntax
Operations
Examples
10.8.5 Shift and Insert
Syntax
Operations
Examples
10.9 Arithmetic Instructions
10.9.1 Vector Add and Subtract
Syntax
Operations
Examples
10.9.2 Vector Add and Subtract with Narrowing
Syntax
Operations
Examples
10.9.3 Add or Subtract and Divide by Two
Syntax
Operations
Examples
10.9.4 Add Elements Pairwise
Syntax
Operations
Examples
10.9.5 Absolute Difference
Syntax
Operations
Examples
10.9.6 Absolute Value and Negate
Syntax
Operations
Examples
10.9.7 Get Maximum or Minimum Elements
Syntax
Operations
Examples
10.9.8 Count Bits
Syntax
Operations
Examples
10.10 Multiplication and Division
10.10.1 Multiply
Syntax
Operations
Examples
10.10.2 Multiply by Scalar
Syntax
Operations
Examples
10.10.3 Fused Multiply Accumulate
Syntax
Operations
Examples
10.10.4 Saturating Multiply and Double (Low)
Syntax
Operations
Examples
10.10.5 Saturating Multiply and Double (High)
Syntax
Operations
Examples
10.10.6 Estimate Reciprocals
Syntax
Operations
Examples
10.10.7 Reciprocal Step
Syntax
Operations
Examples
10.11 Pseudo-Instructions
10.11.1 Load Constant
Syntax
Operations
Examples
10.11.2 Bitwise Logical Operations with Immediate Data
Syntax
Operations
Examples
10.11.3 Vector Absolute Compare
Syntax
Operations
Examples
10.12 Performance Mathematics: A Final Look at Sine
10.12.1 Single Precision
10.12.2 Double Precision
10.12.3 Performance Comparison
10.13 Alphabetized List of NEON Instructions
10.14 Chapter Summary
Exercises
Part III: Accessing Devices
Chapter 11: Devices
11.1 Accessing Devices Directly Under Linux
11.2 General Purpose Digital Input/Output
11.2.1 Raspberry Pi GPIO
Setting the GPIO pin function
Setting GPIO output pins
Reading GPIO input pins
Enabling internal pull-up or pull-down
Detecting GPIO events
GPIO pins available on the Raspberry Pi
11.2.2 pcDuino GPIO
Setting the GPIO pin function
Reading and setting GPIO pins
Enabling internal pull-up or pull-down
Detecting GPIO events
GPIO pins available on the pcDuino
11.3 Chapter Summary
Exercises
Chapter 12: Pulse Modulation
12.1 Pulse Density Modulation
12.2 Pulse Width Modulation
12.3 Raspberry Pi PWM Device
12.4 pcDuino PWM Device
12.5 Chapter Summary
Exercises
Chapter 13: Common System Devices
13.1 Clock Management Device
13.1.1 Raspberry Pi Clock Manager
13.1.2 pcDuino Clock Control Unit
13.2 Serial Communications
13.2.1 UART
13.2.2 Raspberry Pi UART0
13.2.3 Basic Programming for the Raspberry Pi UART
13.2.4 pcDuino UART
13.3 Chapter Summary
Exercises
Chapter 14: Running Without an Operating System
14.1 ARM CPU Modes
14.2 Exception Processing
14.2.1 Handling Exceptions
14.3 The Boot Process
14.4 Writing a Bare-Metal Program
14.4.1 Startup Code
14.4.2 Main Program
14.4.3 The Linker Script
14.4.4 Putting it All Together
14.5 Using an Interrupt
14.5.1 Startup Code
14.5.2 Interrupt Controllers
14.5.3 Timers
14.5.4 Exception Handling
14.6 Building the Interrupt-Driven Program
14.6 ARM Processor Profiles
14.7 Chapter Summary
Index
Back Cover