10
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.

COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

Embed Size (px)

Citation preview

Page 1: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

Page 2: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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

Page 3: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

Page 4: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

Figure 3: Project Manager

Figure 4: Program add.c

# 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; }

Page 5: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

.include "m64def.inc"

.def a =r16 ; define a to be register r16

.def b =r17

.def c =r18

.def d =r19

.def e =r20

main: ; main is a label ldi a, 10 ; load 10 into r16 ldi b, -20 mov c, a ; copy the value of r16 into r18 add c, b ; add r18 and r17 and store the result in r18 mov d, a sub d, b ; subtract r17 from r19 and store the result in r19 lsl c ; refer to AVR Instruction Set for the semantics of asr d ; lsl and asr mov e, c add e, d

loop: rjmp loop ; jump to loop

Page 6: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

Page 7: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

Page 8: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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

Page 9: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

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.

Page 10: COMP2121 Introductory Experimentcs2121/Labs/Introductory_2012.pdf · COMP2121 Introductory Experiment Objectives: In this introductory experiment, ... which contains AVR Instruction

Figure 10: Disassembled program