COMP2121 Introductory Experiment

Similar documents
Lab Objectives. 2. Preparations. 3. Signing in. 4. Examining the Host Environment. 5. Part A: Introduction to AVR Studio. 5.

COMP2121 Experiment 4

Lab 2: Basic Assembly Programming and Debugging using AVR Studio. Due: December 13, 2011

Register-Level Programming

C Programming in Atmel Studio 7 Step by Step Tutorial

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Assembly Programming in Atmel Studio 7 Step by Step Tutorial

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I)

Section 1 AVR Studio User Guide

ECE 375: Computer Organization and Assembly Language Programming

Introduction to Assembly language

Microprocessor Fundamentals. Topic 8 Binary Addition

Microprocessors & Interfacing

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

Getting Started with STK200 Dragon

Computer Organization and Assembly Language. Lab Session 01

Programming Model 2 A. Introduction

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

ET-BASE AVR ATmega64/128

Changing the Embedded World TM. Module 3: Getting Started Debugging

Microprocessor Fundamentals. Topic 7 Timing: the Timer/Counter

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

APPENDIX A FOR SKEE3732 LABORATORY 1 SHEET

Code Composer Studio. MSP Project Setup

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

An Introduction to Komodo

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

Assembly Programming (III)

Microprocessors & Interfacing

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

ATmega128 Assembly Language Programming

Assembly Language programming (1)

Required Setup for 32-bit Applications

ECE 353 Lab 4. General MIDI Explorer. Professor Daniel Holcomb Fall 2015

Module 2: Introduction to AVR ATmega 32 Architecture

Microcontroller VU

ECED 3204 Microprocessor Midterm Reference Solution

Note that FLIP is an Atmel program supplied by Crossware with Atmel s permission.

Development Tools. 8-Bit Development Tools. Development Tools. AVR Development Tools

ECE2049 Embedded Computing in Engineering Design. Lab #0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

CPE 323: Laboratory Assignment #1 Getting Started with the MSP430 IAR Embedded Workbench

Lab 7: Change Calculation Engine

Tools Basics. Getting Started with Renesas Development Tools R8C/3LX Family

Introduction to Microcontrollers

Getting Started with Keil µvision 3 and C51

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

NEWBIE'S GUIDE TO AVR DEVELOPMENT A N IN TR O DU CT I O N I N TE N DE D FO R PEOPL E W I TH NO PRIOR AV R KNOWLE DG E AVRFREAKS.

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

BASIC AVR ARITHMATIC v1.1 ADDITION and SUBTRACTION by

Introduction to AVR-STUDIO

COMP2121: Microprocessors and Interfacing. I/O Devices (II)

ECED3204: Microprocessor Part I--Introduction

COMP2121: Microprocessors and Interfacing. Instruction Formats and Addressing Modes

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

ELEG3924 Microprocessor

LABORATORY 1: EXPLORING THE SOFTWARE ARCHITECTURE OF THE MICROPROCESSOR

AVR MICROCONTROLLER ARCHITECTURTE

ELEG3923 Microprocessor Ch.2 Assembly Language Programming

IAS0430 MICROPROCESSOR SYSTEMS

FAKULTI KEJURUTERAAN ELEKTRIK FAKULTI KEJURUTERAAN ELEKTRIK UNIVERSITI TEKNOLOGI MALAYSIA KAMPUS SKUDAI JOHOR SKEE 3732 MICROPROCESSOR LABORATORY

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

Lab 0 Introduction to the MSP430F5529 Launchpad-based Lab Board and Code Composer Studio

ECE372 CodeWarrior Simulator Andreou/Michaelides

Visual Studio.NET. Although it is possible to program.net using only the command OVERVIEW OF VISUAL STUDIO.NET

LAB1. Get familiar with Tools and Environment

COMP2121: Microprocessors and Interfacing. I/O Devices (I)

Click on the Start Icon. Click on All Programs

Programming Microcontroller Assembly and C

CST8152 Compilers Creating a C Language Console Project with Microsoft Visual Studio.Net 2003

ECE 331: PC Lab 3 Stack and Subroutines

CN310 Microprocessor Systems Design

APPENDIX D FLOWCHARTS AND PSEUDOCODE OVERVIEW

ECOM 2325 Computer Organization and Assembly Language. Instructor: Ruba A.Salamah INTRODUCTION

Writing Code and Programming Microcontrollers

acret Ameya Centre for Robotics & Embedded Technology Syllabus for Diploma in Embedded Systems (Total Eight Modules-4 Months -320 Hrs.

Laboratory. Low-Level. Languages. Objective. References. Study simple machine language and assembly language programs.

CS Prof J.P.Morrison

CHAPTER ASSEMBLY LANGUAGE PROGRAMMING

AVR Subroutine Basics

LAB WORK NO. 3 TURBO DEBUGGER ENVIRONMENT

Project. A project file contains the following information:

AVR Microcontrollers Architecture

COMP3221: Microprocessors and Embedded Systems

Embedded Systems. 2. Software Development. Lothar Thiele. Computer Engineering and Networks Laboratory

EXPERIMENT 1. FAMILIARITY WITH DEBUG, x86 REGISTERS and MACHINE INSTRUCTIONS

Carleton University Department of Systems and Computer Engineering SYSC Foundations of Imperative Programming - Winter 2012

Programmer AVR USBasp

Microprocessors And Microcontrollers (Practical)

ESE 150 Lab 08: Machine Level Language

Lab 8: Debugging Embedded Devices and Software

Introduction. Key features and lab exercises to familiarize new users to the Visual environment

DESIGN NOTE #032. AVR Boot Loader. Introduction. Overview AUTHOR: MARIANO BARRÓN RUIZ KEYWORDS: BOOT LOADER, SPM, SELF-PROGRAMMING

COMP2121: Microprocessors and Interfacing

PX-4000 mini-avr In-System programmer

Lab 03 - x86-64: atoi

LAB 1 INTRODUCTION TO LINUX ENVIRONMENT AND C COMPILER

LAB 7 Writing Assembly Code

Now you have the basic hardware tools, its time to setup the software environment. The main softwares you will need are:

Transcription:

COMP2121 Introductory Experiment Objectives: In this introductory experiment, you will: Learn how to use AVR studio, an Integrated Development Environment (IDE) for developing AVR applications in Windows environments, to debug and run an AVR assembly program. Understand how numbers are represented in a microprocessor. Understand how AVR computes the S flag, the C flag and the N flag. Preparation: Before coming to the laboratory, you should read through the instructions in detail. It is highly recommended that you install AVR Studio 4 on your computer at home. The software is available on the course website. Signing In: You will be working with a partner in all labs. When you arrive at the laboratory for the first time, please sign in with your partner. Examining the Host Environment: The host environment is a standard personal computer running Windows operating system. Take a note of the desktop icons. The icons include AVR Studio 4 which you will be using to edit your program, assemble it into an executable file and debug it. Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64 Reference Manual and the Dot Matrix Character LCD Module User s Manual. You are encouraged to use these resources during your laboratory experiments. Introduction to AVR Studio: Start AVR Studio: To start AVR Studio, double click the AVR Studio icon, or click on Start -->Programs --> ATMEL AVR Tools -->AVR Studio 4.

Create a New Project: To create a new project, go to the Project menu and select New Project. The dialog box shown in Figure 1 will appear. There are two types of projects: Atmel AVR assembler and AVR GCC. Choose Atmel AVR Assembler and enter a project name. We choose the name lab01 here, but this could be an arbitrary name. Next you need to select the project location. This is the location where AVR Studio will store all files associated with the project. It is a good practice to create separate directories for each lab. In this laboratory, you need to create your working directory on the desktop as you don't have write permission anywhere else. After choosing the project type, name and location, press the Next button to continue. Then you will be asked to choose the Debug Platform and Device from the list as shown in Figure 2. Choose AVR Simulator as the Debug Platform and ATmega64 from the Device list, press Finish to continue. Figure 1: New Project

Figure 2: Debug Platform & Device The Project manager will now appear and look like Figure 3. In this view, you will see files associated with your project. At the moment, there is one file associated with this project called lab01.asm. This file is automatically marked as Assembly Entry File. This indicates that the assembler will start with this file when assembling the project. The Assembler folder can only contain one file marked as an entry file. The current entry file is indicated by a red arrow on the file icon, which the other files do not have. When you have more than one file associated with the project, you will have to manually mark the file you want to debug and run as the Assembly Entry File by right click on the file and choose Set as Entry File. We have now an empty file in our project called lab01.asm. The next step is to fill this file with our code. Figure 4 and Figure 5 shows the C program and its hand-compiled AVR assembly language version. Here we assume that the length of each integer variable is ONE byte. You should understand what the C program does. Read the corresponding AVR assembly program (comments start with ;) to figure out how it implements the C program. The hand-compiled assembly program assigns registers r16, r17, r18, r19, r20 to the C variables a, b, c, d, e, respectively. Note that the function of pseudo instruction define is similar to that in C. All pseudo instructions will disappear after an AVR assembly program is assembled.

Figure 3: Project Manager # include <stdio.h> int a = 10, b = -20, c, d, e; int main(void) { c = a + b; d = a - b; e = c*2 + d/2; return 0; } Figure 4: Program add.c

.include "m64def.inc".def a =r16.def b =r17.def c =r18.def d =r19.def e =r20 ; define a to be register r16 main: loop: ldi a, 10 ldi b, -20 mov c, a add c, b mov d, a sub d, b lsl c asr d mov e, c add e, d rjmp loop ; main is a label ; load 10 into r16 ; copy the value of r16 into r18 ; add r18 and r17 and store the result in r18 ; subtract r17 from r19 and store the result in r19 ; refer to AVR Instruction Set for the semantics of ; lsl and asr ; jump to loop Figure 5: Program lab01.asm In lab01.asm, the file m64def.inc is required to be included in the program as it is the definition file for the ATmega64, and contains definitions for the microcontroller needed by the assembler. The next step is to build and run the file i.e. assemble the assembly program into machine instructions. This is done by selecting Build and Run from the Project menu, or by press CTRL+F7, as shown in Figure 6. The Output window will show information from the assembler. From this window we can see that the assembly process was completed with no errors and the executable file is 22 bytes long as shown in Figure 6. Note that the code and constants are stored in FLASH memory and the length of all addressable cells of the FLASH memory is ONE word (two bytes) in AVR. We are now ready to run the code in simulator mode.

Figure 6: Build and run your program Simulating the Code: At this point we have generated the files needed to simulate the code. Now, run the program instruction by instruction. Notice that a yellow arrow is pointing to the first instruction ldi a, 10 in the editor window. This arrow indicates the position of the program counter (PC). PC always points to the next instruction to be executed. Next set up the Register View so that we can see the value of each register during the program execution. Now double click on Register 16-31 in the I/O View window. You will see a map of all registers from r16 to r31 as shown in Figure 7.

Figure 7: Register View The value of each register is 0 at the beginning. These registers will be dynamically updated during the program execution. You may also assign and change values of these registers by double clicking on the value of a particular register of interest and enter the appropriate value at any time during the program execution. To start, there are two commands to single step through the code. They are Step Over F10 and Step Into F11. The difference between these commands is that F10 does not trace into subroutines. Since our example does not contain any subroutines, there is no difference between these two commands here. Now single-step down to the last line of the code rjmp loop by repeatedly pressing the F11 key or by selecting Step Into from the Debug menu. Notice how the color changes from black to red on the registers when their values are changed. This makes it easier to identify which register changes its value when an instruction is executed. Each AVR microcontroller has a Status REGister (SREG) that keeps 8 flags such as C, S and V.

The definitions of all 7 flags in SREG can be found in Mega64 Data Sheet (page 10). These flags are dynamically updated during the program execution. To see the value of SREG, double click on I/O MEGA64 and then on CPU. You will see a window as shown in Figure 8. To get the value of each flag, you need to convert the hex number into a binary number. For example, after the instruction add c, b is finished, the value of SREG is 0x34 i.e. 0b00110100. So C (bit 0) = 0, V (bit 3) =0 and S (bit 4) =1. AVR Studio provides a disassembler which lists the assembly details. Using the disassembler, you can see the corresponding machine code and the memory address of each instruction. To run the disassembler, click View and then Disassembler, as shown in Figure 9. You will see a listing of your disassembled program as shown in figure 10. In the listing of the disassembled program, for each instruction its memory address (in the first column), its machine code (in the second column), its assembly code (in the third column) and its name (in the last column) are given. Take the instruction ldi a, 10 for example. Its memory address is 0x0. Its machine code is 0xE00A (one word long). Its assembly code is DI R16,0x0A. Figure 8: Status register SREG

Figure 9: Disassemble your program Task: Single-step through the program. At each step, look at the value of each register used in the program. What is the value of r17 after the instruction ldi b, -20 is executed? How is the value obtained from -20? At each step, examine the value of the Status Register SREG, How are the C flag, the S flag and the V flag changed? Is there any overflow during the program execution? Disassemble the assembly program and figure out how each instruction is assembled into the machine code. Note that there is no mark for this experiment. However, you are strongly recommended to finish both tasks. If you have any questions, feel free to ask the lab tutor.

Figure 10: Disassembled program