Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Some assembly required: Assembly language programming with the AVR microcontroller
Nội dung xem thử
Mô tả chi tiết
A family of internationally popular microcontrollers, the Atmel AVR
microcontroller series is a low-cost hardware development platform suitable
for an educational environment. Until now, no text focused on the assembly
language programming of these microcontrollers. Through detailed
coverage of assembly language programming principles and techniques,
Some Assembly Required: Assembly Language Programming with the
AVR Microcontroller teaches the basic system capabilities of 8-bit AVR
microcontrollers.
The text illustrates fundamental computer architecture and programming
structures using AVR assembly language. It employs the core AVR 8-bit
RISC microcontroller architecture and a limited collection of external
devices, such as push buttons, LEDs, and serial communications, to
describe control structures, memory use and allocation, stacks, and I/O.
Each chapter contains numerous examples and exercises, including
programming problems.
Features
• Presents a comprehensive treatment of assembly language
programming and basic computer organization
• Focuses on the popular Atmel AVR microcontroller series
• Covers function call mechanisms and parameter passing techniques
• Describes data representation for integers, floating point, and
character data
• Explains how to use the XPLAIN Evaluation Kit
By studying assembly languages, computer scientists gain an understanding
of the functionality of basic processors and how their capabilities support
high level languages and applications. Exploring this connection between
hardware and software, this book provides a foundation for understanding
compilers, linkers, loaders, and operating systems in addition to the
processors themselves.
K11091
SOME ASSEMBLY REQUIRED
MARGUSH
with the AVR Microcontroller
Assembly Language Programming
Computer Science
K11091_Cover.indd 1 6/27/11 10:40 AM
SOME ASSEMBLY
REQUIRED
Assembly Language Programming
with the AVR Microcontroller
SOME ASSEMBLY
REQUIRED
TIMOTHY S. MARGUSH
Assembly Language Programming
with the AVR Microcontroller
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2012 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 20110818
International Standard Book Number-13: 978-1-4398-9700-3 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable
efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and
publishers have attempted to trace the copyright holders of all material reproduced in this publication
and apologize to copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may rectify in any
future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222
Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are
used only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
My husband, Tim, died before completing the dedication page
so I am writing this for him. To Michael Decker who put in
hours of time proofreading text and code during a very busy
time in his life. He is a credit to his profession and a blessing to
our family. I would also like to dedicate this book to Tim's
brother, Philip Margush, whom Tim loved very much.
—Gail Margush
vii
Contents
Acknowledgments, xxi
Author, xxiii
INTRODUCTION, XXV
Chapter 1 ◾ Computer Systems 1
BASIC PROCESSOR ARCHITECTURE 1
COMPONENTS OF A COMPUTER SYSTEM 2
Central Processing Unit 2
Storage 3
Input and Output 4
CLASSIFICATION OF PROCESSORS 5
CISC versus RISC 5
Functionality 5
Architecture 7
NUMERATION SYSTEMS 7
The Nature of Data 8
Binary, Octal, Hexadecimal, Decimal . . . 10
Conversions 12
Repeated Division Algorithm 14
Polynomial Evaluation Algorithm 14
Horner’s Algorithm 14
BOOLEAN DATA 16
Boolean Operations 17
viii ◾ Contents
Applications of Boolean Operations 18
Mask Out, Clear, Zero 20
Set and Union 20
Toggle 21
Shifting and Rotating Bits 22
EXERCISES 23
Chapter 2 ◾ The Atmel AVR Microcontroller Family 27
THE AVR CORE 27
Instructions 28
Registers 28
Clock 29
MACHINE LANGUAGE FOR THE AVR
MICROCONTROLLER 30
One Plus One Equals? 30
Load Immediate 31
Add 34
Expanding Opcodes 36
Execution Trace 37
Jump 39
AVR STUDIO 42
New Project 42
Editor 43
Assembling 43
Debugger 44
Mnemonics 48
AVR DEVELOPMENT PLATFORMS 50
STK-500 Development Kit 50
The ATAVRXPLAIN Demonstration Kit 52
INTRODUCTION TO AVR ASSEMBLY LANGUAGE 53
Assembly Language Source Files 53
Line Format 54
Additional Features 54
Contents ◾ ix
SAMPLE AVR ASSEMBLY LANGUAGE PROGRAM 55
A Counting Program 56
Explanation of the Statements 57
Assembling the Program 58
Obtaining a Listing File 58
The Map File 59
Simulating Execution 59
Downloading to the AVR Processor 60
Adjusting the Clock Speed 64
EXERCISES 66
PROGRAMMING EXERCISES 68
ALTERNATE PROGRAMS FOR THE XPLAIN
DEMONSTRATION KIT 69
Program 2.2a: Counter 69
Chapter 3 ◾ Assembly Language 73
DIRECTIVES 75
The Assembler’s Location Counters 75
EXPRESSIONS 79
Symbols and Literals 80
DATA DEFINITION DIRECTIVES 82
Operators Used in Expressions 83
Functions Used in Expressions 86
INSTRUCTIONS 87
Zero-Operand Instructions 89
One-Operand Instructions 90
Two-Operand Instructions 90
THE TOGGLER PROGRAM: SAMPLE I/O 90
AVR Digital I/O Ports and STK-500/XPLAIN LEDs 92
AVR Digital I/O Ports and STK-500/XPLAIN Pushbuttons 94
The Toggler Program 95
EXERCISES 99
x ◾ Contents
PROGRAMMING EXERCISES 102
ALTERNATE PROGRAMS FOR THE XPLAIN
DEMONSTRATION KIT 103
Program 3.1a: Toggler 103
Program Notes 106
Chapter 4 ◾ Integer Data Representation 109
UNSIGNED INTEGER DATA 109
CHARACTER DATA 115
SIGNED INTEGER DATA 116
Two’s Complement 116
One’s Complement 121
Sign and Magnitude 121
Excess-N 122
BINARY CODED DECIMAL 123
DATA VALUES IN PROGRAMS 124
Define Byte, Word, and Others 124
Data Encoding 125
ASSIGNMENT STATEMENTS 128
Simple Variables 128
Assignment 130
Register-Based Variables 132
Evaluating Expressions 134
BRANCH INSTRUCTIONS 135
EXERCISES 137
PROGRAMMING EXERCISES 142
Chapter 5 ◾ AVR Core: Startup, Reset, and Memories 145
AVR RESET SIGNAL 147
Interrupt Jump Table 148
BASIC SYSTEM ORGANIZATION 150
Program Memory 150
Contents ◾ xi
General Purpose Registers 150
I/O Registers and Data Space 150
Stack 151
EEPROM Storage 154
PROGRAM MEMORY 154
Program Indirect Addressing 154
DATA SPACE 158
Data Direct Addressing 158
I/O Direct Addressing 160
Data Indirect Addressing 160
Data Indirect Addressing with Postincrement
or Predecrement 161
Data Indirect Addressing with Displacement 162
EEPROM 163
The Standard Include File 166
EEPROM Read 167
Defining EEPROM Data 167
Using EEPROM Data in the AVR Simulator 168
Downloading EEPROM Data to the Microcontroller 171
Writing Data to EEPROM 172
EXERCISES 173
PROGRAMMING EXERCISES 176
ALTERNATE PROGRAMS FOR THE XPLAIN
DEMONSTRATION KIT 177
Program 5.1a: LED Patterns 177
The Standard Include File 180
EEPROM Read 181
Defining EEPROM Data 182
Using EEPROM Data in the AVR Simulator 2 183
Downloading EEPROM Data 184
Writing Data to EEPROM 185
xii ◾ Contents
Chapter 6 ◾ The Stack and Function Calls 189
STACK 189
Stack Pointer 190
Push and Pop 190
FUNCTIONS 191
Defining a Function 192
Function Call Instructions 193
Return Address and RET Instructions 195
ARGUMENTS, PARAMETERS, AND
RETURN VALUES 196
Return Values 196
Arguments and Parameters: Call by Value/Call
by Reference 199
Memory Allocation for Parameters 200
Register Parameters 201
Stack Parameters 204
Inline Parameters 207
LOCAL STORAGE 211
Parameter and Local Storage Access
in Stack Frames 215
Fibonacci Numbers Example 215
Stack Frame Naming Conventions 218
Function Fibonacci Continued 219
Stack Cleanup: Automatic Parameter Removal 221
Preprocessor Macros 222
AUXILIARY DATA STACK (SOFTWARE STACK) 224
Macros for the Data Stack 226
Fibonacci with Data Stack 228
CALL BY REFERENCE EXAMPLE 230
EXERCISES 233
PROGRAMMING EXERCISES 238
Contents ◾ xiii
Chapter 7 ◾ Serial Communications Using the
Universal Synchronous and Asynchronous
Serial Receiver and Transmitter 241
RS-232 241
SERIAL COMMUNICATION BASICS 243
Mark and Space 244
Serial Data Frame 244
AVR USART CONFIGURATION 246
Baud Rate 246
Data, Parity, and Stop Bits 248
Physical Connections 251
Terminal Programs 253
XPLAIN Programming Notes 254
AVR USART TRANSMIT AND RECEIVE 255
Enabling the Transmitter and Receiver 255
Transmitting Data 256
Receiving Data 259
Disabling the Transmitter or Receiver 259
SERIAL TRANSMISSION ERRORS 260
Detecting Errors 260
POLLED I/O 262
SERIAL COMMUNICATIONS EXAMPLE 262
Transmit Time 266
EXERCISES 266
PROGRAMMING EXERCISES 268
ALTERNATE PROGRAMS FOR THE XPLAIN
DEMONSTRATION KIT 270
Program 7.1a: Serial Echo 270
Chapter 8 ◾ Logical Operations 275
BITWISE LOGICAL OPERATIONS 275
Status Flags 276
xiv ◾ Contents
USES OF LOGICAL INSTRUCTIONS 278
Clearing, Setting, and Toggling 280
Masks 281
SHIFT AND ROTATE 282
Arithmetic Shifting 284
Other Applications of Shift and Rotate 285
SPECIAL BITWISE OPERATIONS 287
AVR TIMER/COUNTER 288
Timer/Counter 1 Configuration: ATMEGA16A 289
Timer/Counter 1 Compare Match (ATMEGA16A) 290
Timer/Counter Configuration: ATxmega128A1 292
Timer/Counter Period: ATxmega128A1 293
TIMER/COUNTER EXAMPLE: LED BLINKER 295
EXERCISES 297
PROGRAMMING EXERCISES 299
ALTERNATE PROGRAMS FOR THE XPLAIN
DEMONSTRATION KIT 301
Program 8.1a: LED Blinker 301
Chapter 9 ◾ Control Structures 305
ALTERING THE FLOW OF CONTROL 306
Jump 306
Call and Return 307
Conditional Branch 307
JUMP AND CONDITIONAL BRANCH INSTRUCTIONS 308
Comparisons 310
Conditional Skip Instructions 311
SELECTION 311
If Else 312
Compound Conditions 312
Multiway Selection Structure 318
Computed Goto 319