Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC...

10
1 Lab 0 1. Objectives 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 (microcontroller). Understand how AVR computes the S flag, the C flag and the N flag. Understand how AVR instructions are encoded. 2. Preparations 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. You can download it from the course homepage by clicking on “AVR Material”. Note that AVR Studio works in Windows environments only! 3. Signing in As I mentioned in the first lecture, you will be working with a partner in all labs. When you arrive at the laboratory in Week 2, sign in with your partner. 4. Examining the Host Environment Please turn your attention to the host PC. This host PC is simply a standard personal computer running the Windows operating system. You should see a fairly standard Windows desktop every time you log in. Take note of the desktop icons. The icons include AVR Studio 4 you will be using to edit your program, assemble it into the 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 the labs. 5. Part A: Introduction to AVR Studio 5.1 Start AVR Studio Start AVR Studio by clicking on Start-!Programs-!ATMEL AVR Tools-!AVR Studio 4. 5.2 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 lab0 here, but this could of course be an arbitrary name. Next you need to select the

Transcript of Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC...

Page 1: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

1

Lab 0

1. Objectives

• 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 (microcontroller). • Understand how AVR computes the S flag, the C flag and the N flag. • Understand how AVR instructions are encoded.

2. Preparations 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. You can download it from the course homepage by clicking on “AVR Material”. Note that AVR Studio works in Windows environments only! 3. Signing in As I mentioned in the first lecture, you will be working with a partner in all labs. When you arrive at the laboratory in Week 2, sign in with your partner. 4. Examining the Host Environment Please turn your attention to the host PC. This host PC is simply a standard personal computer running the Windows operating system. You should see a fairly standard Windows desktop every time you log in. Take note of the desktop icons. The icons include AVR Studio 4 you will be using to edit your program, assemble it into the 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 the labs. 5. Part A: Introduction to AVR Studio 5.1 Start AVR Studio Start AVR Studio by clicking on Start-!Programs-!ATMEL AVR Tools-!AVR Studio 4. 5.2 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 lab0 here, but this could of course be an arbitrary name. Next you need to select the

Page 2: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

2

project location. This is the location where AVR Studio will store all files associated with the project. It is a good habit to create separate directories for each lab. In this laboratory, you need to create your working directories on the desktop as you do not have write permission anywhere else. To create a new project, click on New Projects. You will be asked for the type and name of the new project as shown in Figure 2. Click on Atmel AVR Assembler and then enter your project name. Here we choose lab0 as the project name. Of course you can choose any name you like. After choosing a project type and name, 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 3. Choose “AVR Simulator” as the “Debug Platform” and “ATmega64” from the Device list, press “Finish” to continue.

Figure 1: Start Screen

Page 3: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

3

Figure 2: New Project

The Project manager will now appear and look like Figure 4. In this view, you will see files associated with your project. At the moment, there is one file associated with this project called lab0.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 lab0.asm. The next step is to fill this file with our code. Figure 5 and Figure 6 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: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

4

Figure 3: Debug Platform & Device

Page 5: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

5

Figure 4: 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 5: Program add.c

Page 6: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

6

.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: inc r21 ; increase the value of r21 by 1 rjmp loop ; jump to loop

Figure 6: Program lab0.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 7. 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 24 bytes long (why?) as shown in Figure 7. Note that as I mentioned in the lecture, 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 runs the code in simulator mode. 5.3 Simulating the Code At this point we have generated the files needed to simulate the code. Now start running 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 “Registers ” in the Processor window. You will see a map of all registers from r0 to r31 as shown in Figure 8.

Page 7: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

7

Figure 7: Build and run your program

Page 8: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

8

Figure 8: 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. The SREG is displayed in the Processor window. 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

Page 9: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

9

instruction. To run the disassembler, click View and then Disassembler. You will see a listing of your disassembled program as shown in figure 9. In the listing of the disassembled program, for each instruction its memory address (in the first column), its assembly code and machine code (in the second column) as well as its name (in the third 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 LDI R16,0x0A. Task 1: 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?

Task 2: Disassemble the assembly program and figure out how each instruction is assembled into the machine code.

Figure 9: Disassemble your program

Page 10: Lab 0 - Computer Science and Engineeringcs2121/Labs/lab0_new09.pdf · Another icon is the AVRDOC folder, which contains AVR Instruction Set Manual, AVR Instruction Set Summary, ATmega64

10

Note that there is no mark for this lab! However, you are strongly recommended to finish both tasks. If you have any questions, feel free to ask a demonstrator.