Computer Science w AS it ssO h e A family of internationally popular microcontrollers, the Atmel AVR t mM microcontroller series is a low-cost hardware development platform suitable h b e for an educational environment. Until now, no text focused on the assembly lyE A language programming of these microcontrollers. Through detailed V L coverage of assembly language programming principles and techniques, R aA Some Assembly Required: Assembly Language Programming with the n M gS AVR Microcontroller teaches the basic system capabilities of 8-bit AVR i uS microcontrollers. c a r E o g The text illustrates fundamental computer architecture and programming c eM o structures using AVR assembly language. It employs the core AVR 8-bit n P t rB RISC microcontroller architecture and a limited collection of external r o o L devices, such as push buttons, LEDs, and serial communications, to g l Y l r describe control structures, memory use and allocation, stacks, and I/O. e a r Each chapter contains numerous examples and exercises, including mR m programming problems. E i nQ Features g • Presents a comprehensive treatment of assembly language U programming and basic computer organization I • Focuses on the popular Atmel AVR microcontroller series R • Covers function call mechanisms and parameter passing techniques E • Describes data representation for integers, floating point, and D 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 M hardware and software, this book provides a foundation for understanding A compilers, linkers, loaders, and operating systems in addition to the R G processors themselves. U S H K11091 K11091_Cover.indd 1 6/27/11 10:40 AM SOME ASSEMBLY REQUIRED Assembly Language Programming with the AVR Microcontroller SOME ASSEMBLY REQUIRED Assembly Language Programming with the AVR Microcontroller TIMOTHY S. MARGUSH 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 stor- age or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copy- right.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 pro- vides licenses and registration for a variety of users. For organizations that have been granted a pho- tocopy 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 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 vii 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