Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors

Total Page:16

File Type:pdf, Size:1020Kb

Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors Beginners Introduction to the Assembly Language of ATMEL-AVR-Microprocessors by Gerhard Schmidt http://www.avr-asm-tutorial.net September 2021 History: Added chapters on binary floating points and on memory access in September 2021 Added chapter on code structures in April 2009 Additional corrections and updates as of January 2008 Corrected version as of July 2006 Original version of December 2003 Avr-Asm-Tutorial 2 http://www.avr-asm-tutorial.net Content Why learning Assembler?..........................................................................................................................1 Short and easy.......................................................................................................................................1 Fast and quick........................................................................................................................................1 Assembler is easy to learn.....................................................................................................................1 AVRs are ideal for learning assembler..................................................................................................1 Test it!....................................................................................................................................................2 Hardware for AVR-Assembler-Programming...........................................................................................3 The ISP-Interface of the AVR-processor family...................................................................................3 Programmer for the PC-Parallel-Port....................................................................................................3 Experimental boards..............................................................................................................................4 Experimental board with an ATtiny13.............................................................................................4 Experimental board with an AT90S2313/ATmega2313..................................................................5 Ready-to-use commercial programming boards for the AVR-family...................................................6 STK200.............................................................................................................................................6 STK500.............................................................................................................................................6 AVR Dragon.....................................................................................................................................7 Tools for AVR assembly programming.....................................................................................................8 From a text file to instruction words in the flash memory....................................................................8 The editor..........................................................................................................................................8 Structuring assembler code...............................................................................................................9 Comments.........................................................................................................................................9 Things to be written on top...............................................................................................................9 Things that should be done at program start...................................................................................10 Structuring of program code...........................................................................................................10 The assembler.................................................................................................................................13 Programming the chips........................................................................................................................14 Simulation in the studio.......................................................................................................................14 What is a register?...............................................................................................................................18 Different registers................................................................................................................................20 Pointer-registers...................................................................................................................................20 Accessing memory locations with pointers....................................................................................20 Reading program flash memory with the Z pointer........................................................................20 Tables in the program flash memory..............................................................................................21 Accessing registers with pointers...................................................................................................21 Recommendation for the use of registers............................................................................................22 Ports.........................................................................................................................................................23 What is a Port?....................................................................................................................................23 Write access to ports.......................................................................................................................23 Read access to ports........................................................................................................................24 Read-Modify-Write access to ports................................................................................................24 Memory mapped port access..........................................................................................................24 Details of relevant ports in the AVR..............................................................................................25 The status register as the most used port.............................................................................................25 Port details...........................................................................................................................................26 SRAM......................................................................................................................................................27 Using SRAM in AVR assembler language.........................................................................................27 What is SRAM?...................................................................................................................................27 For what purposes can I use SRAM?..................................................................................................27 How to use SRAM?.............................................................................................................................27 Direct addressing............................................................................................................................27 Pointer addressing...........................................................................................................................28 Pointer with offset...........................................................................................................................28 Use of SRAM as stack.........................................................................................................................28 Defining SRAM as stack................................................................................................................29 Use of the stack...............................................................................................................................29 Bugs with the stack operation.........................................................................................................30 Jumping and Branching............................................................................................................................31 Controlling sequential execution of the program................................................................................31 What happens during a reset?.........................................................................................................31 Linear program execution and branches..............................................................................................32 Branching........................................................................................................................................32 Timing during program execution.......................................................................................................33 Macros and program execution...........................................................................................................33 Avr-Asm-Tutorial 3 http://www.avr-asm-tutorial.net Subroutines..........................................................................................................................................34 Interrupts and program execution........................................................................................................35
Recommended publications
  • Lecture Notes in Assembly Language
    Lecture Notes in Assembly Language Short introduction to low-level programming Piotr Fulmański Łódź, 12 czerwca 2015 Spis treści Spis treści iii 1 Before we begin1 1.1 Simple assembler.................................... 1 1.1.1 Excercise 1 ................................... 2 1.1.2 Excercise 2 ................................... 3 1.1.3 Excercise 3 ................................... 3 1.1.4 Excercise 4 ................................... 5 1.1.5 Excercise 5 ................................... 6 1.2 Improvements, part I: addressing........................... 8 1.2.1 Excercise 6 ................................... 11 1.3 Improvements, part II: indirect addressing...................... 11 1.4 Improvements, part III: labels............................. 18 1.4.1 Excercise 7: find substring in a string .................... 19 1.4.2 Excercise 8: improved polynomial....................... 21 1.5 Improvements, part IV: flag register ......................... 23 1.6 Improvements, part V: the stack ........................... 24 1.6.1 Excercise 12................................... 26 1.7 Improvements, part VI – function stack frame.................... 29 1.8 Finall excercises..................................... 34 1.8.1 Excercise 13................................... 34 1.8.2 Excercise 14................................... 34 1.8.3 Excercise 15................................... 34 1.8.4 Excercise 16................................... 34 iii iv SPIS TREŚCI 1.8.5 Excercise 17................................... 34 2 First program 37 2.1 Compiling,
    [Show full text]
  • Converting a Microcontroller Lab from the Freescale S12 to the Atmel Atmega32 Processor
    ASEE-NMWSC2013-0025 Converting a Microcontroller Lab From The Freescale S12 to the Atmel ATmega32 Processor Christopher R. Carroll University of Minnesota Duluth [email protected] Abstract During the summer of 2013, the laboratory supporting the microcontroller course at the University of Minnesota Duluth was completely re-implemented. For the last several years, the processor that has been used was the Freescale S12, a popular 16-bit microcontroller with a long ancestral history 1. The recent popularity of the Atmel AVR series of microcontrollers, as used in the Arduino microcomputers, for example, has prompted a change in the lab to use Atmel’s ATmega32 microcontroller, an 8-bit member of the AVR family of microcontrollers 2,3 . The new processor has a fundamentally different architecture than that used in the past, but the input/output resources available are much the same. This paper addresses issues that will be faced in the conversion when the course is taught with the new lab hardware for the first time in the Fall. At the very fundamental level, the S12 and ATmega32 differ in architecture. The S12 is a Princeton architecture computer (single memory for both program and data), while the ATmega32 is a Harvard architecture computer (separate program and data memories). The S12 is clearly a CISC machine (Complex Instruction Set Computer) while the ATmega32 is clearly a RISC machine (Reduced Instruction Set Computer). These differences will affect how the microcontroller course is taught when it is offered in the Fall using this new lab. Fortunately, however, the collection of input/output devices in the AVR microcontrollers mimics closely what is found in the S12, so that many of the existing lab exercises will be used again with only minor tweaking.
    [Show full text]
  • 8051 Programmer
    T51prog2 MCS51 series and Atmel AVR microcontrollers ISP capable fast programmer Short description: 10692 supported devices from 149 manufacturers by 2.75 version of SW (21. Dec. 2010) small, very fast and powerful portable programmer of MCS51 series and Atmel AVR microcontrollers in-circuit serial programming (ISP) capability included program also serial EEPROMs DIL40 ZIF socket, all MCS51/AVR chips in DIL package up to 40 pins are supported without adapters connection to PC: USB port USB 2.0 full speed and USB 1.1 compatible upgradeable to SmartProg2 programmer. comfortable and easy to use control program, work with all versions of MS Windows from Windows 98 to Window 7 64-bit free SW update, download from Internet power supply, cable and software included approved by CE laboratory to meet CE requirements made in Slovakia Available accessories: Programming Adapters (Socket Converters) Diagnostic POD for ISP connector upgrade kit Xprog2 to SmartProg2 Features GENERAL T51prog2 is the next member of new generation MS Windows (from Windows 98 to Window 7 64-bit) based ELNEC specialized programmers . Programmer is capable to support all currently available microcontrollers of the MCS51 series (up to 40 pins) and the AVR microcontrollers (8-40 pins) using parallel and serial algorithms. T51prog2 has been developed in close cooperation with Atmel W&M. , therefore programmer's hardware is focused to support all current and future microcontrollers of Atmel W&M MCS51 family. T51prog2 is a small, very fast and powerful portable programmer for MCS51 series and Atmel AVR microcontrollers. T51prog also programs serial EEPROM with IIC (24Cxx), Microwire (93Cxx) and SPI (25Cxx) interface types.
    [Show full text]
  • 8-Bit Microcontrollers 32-Bit Microcontrollers and Application
    8-bit Microcontrollers 32-bit Microcontrollers and Application Processors QUICK REFE R ENCE GUIDE February 2009 Everywhere You Are® AVR Introduction Atmel® offers both 8-bit and 32-bit AVR®s. AVR microcontrollers and application processors deliver unmatched flexibility. AVR combines the most code-efficient architecture for C and assembly programming with the ability to tune system parameters throughout the entire life cycle of your key products. Not only do you get to market faster, but once there, you can easily and cost-effectively refine and improve your product offering. The AVR XMEGA gives you 16-bit performance and leading low-power features at 8-bit price. It’s simple: AVR works across the entire range of applications you’re working on... or want to work on. & Introduction QUICK REFERENCE GUIDE AVR Key Benefits AVR32 Key Benefits High performance High CPU performance picoPower™ technology Low power consumption High code density High data throughput High integration and scalability Low system cost Complete tool offering High reliability Atmel’s AVR is addressing the 8-bit and 16-bit market Easy to use Environment Friendly Packages For AVR and AVR32 microcontrollers and application processors, all the lead free packages are RoHS compliant, lead free, halide free and fully green. All parts are offered in fully green packaging only. Product Range Atmel microcontrollers - success through innovation Atmel offers both 8-bit and 32-bit AVR’s, and since day one the AVR philosophy has always been clear: Highest performance with no power penalty. tinyAVR 1-16 KBytes Flash, 8-32 pin packages megaAVR 4-256 KBytes Flash, 28-100 pin packages AVR XMEGA 16-384 KBytes Flash, 44-100 pin packages AVR32 UC3 16-512 KBytes Flash, 48-144 pin packages AVR32 AP7 Up to 32 KBytes On-chip SRAM, 196-256 pin packages & QUICK REFERENCE GUIDE Product Range Product Product Range Range Product Families tinyAVR® General purpose microcontrollers with up to 16K Bytes Flash program memory, 512 Bytes SRAM and EEPROM.
    [Show full text]
  • (PSW). Seven Bits Remain Unused While the Rest Nine Are Used
    8086/8088MP INSTRUCTOR: ABDULMUTTALIB A. H. ALDOURI The Flags Register It is a 16-bit register, also called Program Status Word (PSW). Seven bits remain unused while the rest nine are used. Six are status flags and three are control flags. The control flags can be set/reset by the programmer. 1. DF (Direction Flag) : controls the direction of operation of string instructions. (DF=0 Ascending order DF=1 Descending order) 2. IF (Interrupt Flag): controls the interrupt operation in 8086µP. (IF=0 Disable interrupt IF=1 Enable interrupt) 3. TF (Trap Flag): controls the operation of the microprocessor. (TF=0 Normal operation TF=1 Single Step operation) The status flags are set/reset depending on the results of some arithmetic or logical operations during program execution. 1. CF (Carry Flag) is set (CF=1) if there is a carry out of the MSB position resulting from an addition operation or subtraction. 2. AF (Auxiliary Carry Flag) AF is set if there is a carry out of bit 3 resulting from an addition operation. 3. SF (Sign Flag) set to 1 when result is negative. When result is positive it is set to 0. 4. ZF (Zero Flag) is set (ZF=1) when result of an arithmetic or logical operation is zero. For non-zero result this flag is reset (ZF=0). 5. PF (Parity Flag) this flag is set to 1 when there is even number of one bits in result, and to 0 when there is odd number of one bits. 6. OF (Overflow Flag) set to 1 when there is a signed overflow.
    [Show full text]
  • Overview of IA-32 Assembly Programming
    Overview of IA-32 assembly programming Lars Ailo Bongo University of Tromsø Contents 1 Introduction ...................................................................................................................... 2 2 IA-32 assembly programming.......................................................................................... 3 2.1 Assembly Language Statements................................................................................ 3 2.1 Modes........................................................................................................................4 2.2 Registers....................................................................................................................4 2.2.3 Data Registers .................................................................................................... 4 2.2.4 Pointer and Index Registers................................................................................ 4 2.2.5 Control Registers................................................................................................ 5 2.2.6 Segment registers ............................................................................................... 7 2.3 Addressing................................................................................................................. 7 2.3.1 Bit and Byte Order ............................................................................................. 7 2.3.2 Data Types.........................................................................................................
    [Show full text]
  • Optimizing Subroutines in Assembly Language an Optimization Guide for X86 Platforms
    2. Optimizing subroutines in assembly language An optimization guide for x86 platforms By Agner Fog. Copenhagen University College of Engineering. Copyright © 1996 - 2012. Last updated 2012-02-29. Contents 1 Introduction ....................................................................................................................... 4 1.1 Reasons for using assembly code .............................................................................. 5 1.2 Reasons for not using assembly code ........................................................................ 5 1.3 Microprocessors covered by this manual .................................................................... 6 1.4 Operating systems covered by this manual................................................................. 7 2 Before you start................................................................................................................. 7 2.1 Things to decide before you start programming .......................................................... 7 2.2 Make a test strategy.................................................................................................... 9 2.3 Common coding pitfalls............................................................................................. 10 3 The basics of assembly coding........................................................................................ 12 3.1 Assemblers available ................................................................................................ 12 3.2 Register set
    [Show full text]
  • 2 XII December 2014
    2 XII December 2014 www.ijraset.com Volume 2 Issue XII, December 2014 ISSN: 2321-9653 International Journal for Research in Applied Science & Engineering Technology (IJRASET) Overview and Comparative Study of Different Microcontrollers Rajratna Khadse1, Nitin Gawai2, Bagwan M. Faruk3 1Assist.Professor, Electronics Engineering Department, RCOEM, Nagpur 2,3Assist.Professor, E & Tc Engineering Department, JDIET, Yavatmal Abstract—A microcontroller is a small and low-cost computer built for the purpose of dealing with specific tasks, such as displaying information on seven segment display at railway platform or receiving information from a television’s remote control. Microcontrollers are mainly used in products that require a degree of control to be exerted by the user. Today various types of microcontrollers are available in market with different word lengths such as 8bit, 16bit, 32bit, and microcontrollers. Microcontroller is a compressed microcomputer manufactured to control the functions of embedded systems in office machines, robots, home appliances, motor vehicles, and a number of other gadgets. Therefore in today’s technological world lot of things done with the help of Microcontroller. Depending upon the applications we have to choose particular types of Microcontroller. The aim of this paper to give the basic information of microcontroller and comparative study of 8051 Microcontroller, ARM Microcontroller, PIC Microcontroller and AVR Microcontroller Keywords— Microcontroller, Memory, Instruction, cycle, bit, architecture I. INTRODUCTION Microcontrollers have directly or indirectly impact on our daily life. Usually, But their presence is unnoticed at most of the places like: At supermarkets in Cash Registers, Weighing Scales, Video games ,security system , etc. At home in Ovens, Washing Machines, Alarm Clocks, paging, VCR, LASER Printers, color printers etc.
    [Show full text]
  • An Overview of Atmega AVR Microcontrollers Used in Scientific Research and Industrial Applications
    Pomiary Automatyka Robotyka, R. 19, Nr 1/2015, 15–20, DOI: 10.14313/PAR_215/15 An Overview of ATmega AVR Microcontrollers Used in Scientific Research and Industrial Applications Wojciech Kunikowski, Ernest Czerwiński, Paweł Olejnik, Jan Awrejcewicz Department of Automation, Biomechanics and Mechatronics, Lodz University of Technology, 90–924 Łódź, 1/15 Stefanowski str. Abstract: Nowadays, microcontrollers are commonly used in many fields of industrial applications previously dominated by other devices. Their strengths such as: processing power, low cost, and small sizes enable them to become substitutes for industrial PLC controllers, analog electronic circuits, and many more. In first part of this article an overview of the Atmel AVR microprocessor family can be found, alongside with many scientific and industrial applications. Second part of this article contains a detailed description of two implementations of ATmega644PA microprocessor. First one is a controller with PID regulation that supports a DC motor driver. Second one is a differential equation solver with 4-th order Runge-Kutta method implemented. It is used for solving a torsion pendulum dynamics. Finally, some general conclusions regarding the two presented implementations are made. Keywords: Atmel, AVR, ATmega, microcontroller, torsion pendulum, PID control, DC motor, PWM control 1. Introduction the performance and compact dimensions of control units are the most important requirements. The described micropro- 1.1. Overview of some Atmel microcontrollers cessor characterizes also with very low power consumption. AVR ATmega is a family of 8-bit microprocessors from Atmel. It is the only processor in AVR family that works with 0.7 V Their features vary across models, but mostly, the following power supply.
    [Show full text]
  • Single-Board Microcontrollers
    Embedded Systems Design (0630414) Lecture 15 Single-Board Microcontrollers Prof. Kasim M. Al-Aubidy Philadelphia University Single-Board Microcontrollers: • There is a wide variety of single-board microcontrollers available from different manufacturers and suppliers of microcontrollers. • The most common microcontroller boards are: – Intel Boards: based on Intel microcontrollers. – ARM Boards: based on ARM7 microcontrollers. – Cortex Boards: based on Cortex microcontrollers. – AVR Boards: based on Atmel AVR microcontrollers. – MSP430 Boards: based on Texas Instruments microcontrollers. – PIC Boards: based on the Microchip PIC microcontrollers. – Motorola Boards: based on Motorola microcontrollers. – ARDUNIO Boards: based on Atmel AVR microcontrollers. •It is not easy to decide on which microcontroller to use in a certain application. However, Arduino is becoming one of the most popular microcontrollers used in industrial applications and robotics. •There are different types of Arduino microcontrollers which differ in their design and specifications. The following table shows comparison between the Arduino microcontrollers. Ref: http://www.robotshop.com/arduino-microcontroller-comparison.html The Arduino Uno board: Hardware design of the Arduino Uno board: Single-Board Microcontroller + ZigBee Example: Mobile Robot control using Zigbee Technology Single-Board Microcontroller Selection: The selection guide for using the suitable microcontroller includes: 1. Meeting the hardware needs for the project design; - number of digital and analog i/o lines. - size of flash memory, RAM, and EPROM. - power consumption. - clock speed. - communication with other devices. 2. Availability of software development tools required to design and test the proposed prototype. 3. Availability of the microcontroller..
    [Show full text]
  • X86 Assembly Language Reference Manual
    x86 Assembly Language Reference Manual Part No: 817–5477–11 March 2010 Copyright ©2010 Oracle and/or its affiliates. All rights reserved. This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited. The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing. If this is software or related software documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, the following notice is applicable: U.S. GOVERNMENT RIGHTS Programs, software, databases, and related documentation and technical data delivered to U.S. Government customers are “commercial computer software” or “commercial technical data” pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use, duplication, disclosure, modification, and adaptation shall be subject to the restrictions and license terms setforth in the applicable Government contract, and, to the extent applicable by the terms of the Government contract, the additional rights set forth in FAR 52.227-19, Commercial Computer Software License (December 2007). Oracle USA, Inc., 500 Oracle Parkway, Redwood City, CA 94065.
    [Show full text]
  • 0X03 the Simplest Device Drivers.Pdf
    The Simplest Device Drivers 3.1 How to compile and link the kernel-mode device driver 3.2 The simplest possible kernel-mode device driver 3.2.1 Simplest driver source code 3.2.2 DriverEntry Routine 3.3 Beeper device driver 3.3.1 Beeper driver source code 3.3.2 Controlling the system timer 3.3.3 Starting the driver automatically 3.4 Service Control Program for giveio driver 3.4.1 Giveio driver's SCP source code 3.4.2 Using the registry for passing some info to the driver 3.4.3 Accessing the CMOS 3.5 Giveio device driver 3.5.1 Giveio driver source code 3.5.2 I/O permission bit map 3.5.3 Reading info from the registry 3.5.4 Give user-mode process access to the I/O ports 3.6 A couple of words about driver debugging Source code: KmdKit\examples\simple\Beeper Source code: KmdKit\examples\simple\DateTime 3.1 How to compile and link the kernel-mode device driver I always place driver's source code into a batch file. Such file is a mixture of *.bat and *.asm files, but has "bat" extension. ;@echo off ;goto make .386 ; driver's code start ;:::::::::::::::::::::::::::::::: ; the rest of the driver's code ; ;:::::::::::::::::::::::::::::::: end DriverEntry ; driver's code end :make set drv=drvname \masm32\bin\ml /nologo /c /coff %drv%.bat \masm32\bin\link /nologo /driver /base:0x10000 /align:32 /out:%drv%.sys /subsystem:native %drv%.obj del %drv%.obj echo. pause If you run such "self-compiling" file the following will occur.
    [Show full text]