arm assembly language - gbv

6
ARM Assembly Language Fundamentals and Techniques William Hohl ARM Incorporated, Austin, Texas CRC Press Taylor & Francis Group Boca Raton London NewYork CRC Press is an imprint of the Taylor & Francis Group, an informa business

Upload: others

Post on 25-Jan-2022

10 views

Category:

Documents


0 download

TRANSCRIPT

ARM Assembly Language Fundamentals and Techniques

William Hohl ARM Incorporated, Austin, Texas

CRC Press Taylor & Francis Group

Boca Raton London NewYork

CRC Press is an imprint of the Taylor & Francis Group, an informa business

Contents Preface xiii Acknowledgments xvii Software Notice xix Author xxi

Chapter 1 An Overview of Computing Systems 1

1.1 Introduction 1 1.2 History of RISC 2

1.2.1 ARM Begins 5 1.2.2 The Creation of ARM Ltd 6 1.2.3 ARM Today 9

1.3 The Computing Device 10 1.4 Number Systems 11 1.5 Representations of Numbers and Characters 14

1.5.1 Integer Representations 15 1.5.2 Floating-Point Representations 18 1.5.3 Character Representations 20

1.6 Translating Bits to Commands 21 1.7 The Tools 22 1.8 Exercises 25

Chapter 2 The ARM7TDMI Programmer's Model 29

2.1 Introduction 29 2.2 Data Types 29 2.3 Processor Modes 30 2.4 Registers 31 2.5 Program Status Registers 33

2.5.1 The Control Bits 34 2.5.2 The Mode Bits 34

2.6 The Vector Table 34 2.7 Exercises 35

Chapter 3 First Programs 37

3.1 Introduction 37 3.2 Program 1: Shifting Data 38

3.2.1 Running the Code 39 3.2.2 Examining Register and Memory Contents 40

3.3 Program 2: Factorial Calculation 41 3.4 Program 3: Swapping Register Contents 43

vii

V I I I Contents

3.5 Programming Guidelines 44 3.6 Exercises 45

Chapter 4 Assembler Rules and Directives 47

4.1 Introduction 47 4.2 Structure of Assembly Language Modules 47 4.3 Predefined Register Names 50 4.4 Frequently Used Directives 51

4.4.1 AREA—Define a Block of Data or Code 51 4.4.2 RN—Register Name Definition 52 4.4.3 EQU—Equate a Symbol to a

Numeric Constant 53 4.4.4 ENTRY—Declare an Entry Point 54 4.4.5 DCB, DCW, and DCD—Allocate Memory and

Specify Contents 54 4.4.6 ALIGN—Align Data or Code to Appropriate

Boundaries 55 4.4.7 SPACE—Reserve a Block of Memory 56 4.4.8 LTORG—Assign Literal Pool Origins 56 4.4.9 END—End of a Source File 57

4.5 Macros 57 4.6 Miscellaneous Assembler Features 59

4.6.1 Assembler Operators 59 4.6.2 Literals 61

4.7 Exercises 61

Chapter 5 Loads, Stores, and Addressing 63

5.1 Introduction 63 5.2 Memory 63 5.3 Loads and Stores—The Instructions 66 5.4 Operand Addressing 69

5.4.1 Pre-Indexed Addressing 69 5.4.2 Post-Indexed Addressing 71

5.5 Endianness 72 5.5.1 Changing Endianness 74 5.5.2 Defining Memory Areas 75

5.6 Exercises 76

Chapter 6 Constants and Literal Pools 79

6.1 Introduction 79 6.2 The ARM Rotation Scheme 79 6.3 Loading Constants into Registers 82 6.4 Loading Addresses into Registers 86 6.5 Exercises 91

Contents ix

Chapter 7 Logic and Arithmetic 93

7.1 Introduction 93 7.2 Flags and Their Use 93

7.2.1 The N Flag 93 7.2.2 The V Flag 94 7.2.3 The Z Flag 95 7.2.4 The С Flag 95

7.3 Comparison Instructions 95 7.4 Data Processing Operations 97

7.4.1 Boolean Operations 97 7.4.2 Shifts and Rotates 98 7.4.3 Addition/Subtraction 104 7.4.4 Multiplication 106 7.4.5 Multiplication by a Constant 107 7.4.6 Division 108

7.5 Fractional Notation I l l 7.6 Exercises 115

Chapter 8 Loops and Branches 119

8.1 Introduction 119 8.2 Branching 120 8.3 Looping 123

8.3.1 While Loops 124 8.3.2 For Loops 124 8.3.3 Do ... While Loops 128

8.4 More on Flags 128 8.5 Conditional Execution 128 8.6 Straight-Line Coding 131 8.7 Exercises 132

Chapter 9 Tables 135

9.1 Introduction 135 9.2 Lookup Tables 135 9.3 Jump Tables 139 9.4 Binary Searches 140 9.5 Exercises 143

Chapter 10 Subroutines and Stacks 147

10.1 Introduction 147 10.2 The Stack 147

10.2.1 LDM/STM Instructions 148 10.2.2 Full/Empty Ascending/Descending Stacks 150

10.3 Subroutines 152

x Contents

10.4 Passing Parameters to Subroutines 153 10.4.1 Passing Parameters in Registers 154 10.4.2 Passing Parameters by Reference 156 10.4.3 Passing Parameters on the Stack 158

10.5 The ARM APCS 160 10.6 Exercises 161

Chapter 11 Exception Handling 165

11.1 Introduction 165 11.2 Interrupts 165 11.3 Error Conditions 166 11.4 Processor Exception Sequence 167 11.5 The Vector Table 169 11.6 Exception Handlers 171 11.7 Exception Priorities 172 11.8 Procedures for Handling Exceptions 173

11.8.1 Reset Exceptions 173 11.8.2 Undefined Instructions 174 11.8.3 Interrupts 178 11.8.4 Aborts 187 11.8.5 SWIs 190

11.9 Exercises 191

Chapter 12 Memory-Mapped Peripherals 193

12.1 Introduction 193 12.2 The LPC2104 194

12.2.1 The UART 195 12.2.2 The Memory Map 195 12.2.3 Configuring the UART 197 12.2.4 Writing the Data to the UART 199 12.2.5 Putting the Code Together 199 12.2.6 Running the Code 201

12.3 TheLPC2132 202 12.3.1 The D/A Converter 203 12.3.2 The Memory Map 204 12.3.3 Configuring the D/A Converter 204 12.3.4 Generating a Sine Wave 205 12.3.5 Putting the Code Together 206 12.3.6 Running the Code 208

12.4 Exercises 209

Chapter 13 THUMB 211

13.1 Introduction 211 13.2 THUMB Instructions 212

Contents xi

13.3 Differences Between ARM and THUMB 213 13.4 THUMB Implementation and Use 215

13.4.1 Processor Hardware 215 13.4.2 Switching Between ARM and THUMB States 216

13.5 How to Compile for THUMB 218 13.6 Exercises 220

Chapter 14 Mixing С and Assembly 223

14.1 Introduction 223 14.2 Inline Assembler 223

14.2.1 Inline Assembly Syntax 226 14.2.2 Restrictions on Inline Assembly Operations 228

14.3 Embedded Assembler 228 14.3.1 Embedded Assembly Syntax 230 14.3.2 Restrictions on Embedded Assembly Operations.... 231

14.4 Calling Between С and Assembly 231 14.5 Exercises 233

Appendix A The ARM V4T Instruction Set 235

Appendix В Running Keil Tools 333 B.l Introduction 333 B.2 Creating a Project and Selecting a Device 333 B.3 Creating Application Code 335 B.4 Building the Project and Running Code 336

Appendix С ASCII Character Codes 339

Glossary 341

References 343

Index 345