27
Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 1 APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET Other documents that are referred within this document are located at the link https://www.dropbox.com/sh/s16jri4eol3agl5/AAAzn_W3P7fOdJS-WI-XCeNQa?dl=0 Using AVR Studio t Program in Assembly Language B.1. The AVR Studio 6 IDE Atmel Studio 6 is a software development environment developed by Atmel. It is meant to replace AVR Studio 5 going forward providing a single development platform for Atmel's 8- bits, 32-bits and ARM Cortex-M families of AVR microcontrollers. Some Features of Atmel Studio 6 - It is a full software development environment with an editor, simulator, programmer, etc. - It comes with its own integrated C compiler the AVR GNU C Compiler (GCC). As such you do not need a third party C compiler. - It provides a single environment to develop programs for both the 8-bits, 32-bits and ARM Cortex-M AVR series of microcontrollers. - It also integrates fully QTouch studio. - Provides support for several programmers including the STK500, AVR Dragon, etc B.2. To Create a Project before typing an assembly language Install AVR Studio 6 before continuing the following steps. Step B1: To create a assembly project first start Atmel Studio 6 by going to the start menu on your PC select Atmel AVR Tools then Atmel Studio 6. See the splash screen for Atmel Studio 6, as shown figure below, this indicates that Atmel Studio 6 is starting up. Just wait for a moment. After Atmel Studio 6 starts the Atmel Studio 6 Start Page will appear as shown in the figure below.

APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Embed Size (px)

Citation preview

Page 1: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

1

APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET

Other documents that are referred within this document are located at the link

https://www.dropbox.com/sh/s16jri4eol3agl5/AAAzn_W3P7fOdJS-WI-XCeNQa?dl=0

Using AVR Studio t Program in Assembly Language

B.1. The AVR Studio 6 IDE

Atmel Studio 6 is a software development environment developed by Atmel. It is meant to replace AVR Studio 5 going forward providing a single development platform for Atmel's 8-bits, 32-bits and ARM Cortex-M families of AVR microcontrollers.

Some Features of Atmel Studio 6

- It is a full software development environment with an editor, simulator, programmer,

etc.

- It comes with its own integrated C compiler the AVR GNU C Compiler (GCC). As such you do

not need a third party C compiler.

- It provides a single environment to develop programs for both the 8-bits, 32-bits and

ARM Cortex-M AVR series of microcontrollers.

- It also integrates fully QTouch studio.

- Provides support for several programmers including the STK500, AVR Dragon, etc

B.2. To Create a Project before typing an assembly language

Install AVR Studio 6 before continuing the following steps.

Step B1: To create a assembly project first start Atmel Studio 6 by going to the

start menu on your PC select Atmel AVR Tools then Atmel Studio 6. See the

splash screen for Atmel Studio 6, as shown figure below, this indicates

that Atmel Studio 6 is starting up. Just wait for a moment.

After Atmel Studio 6 starts the Atmel Studio 6 Start Page will appear as shown in the

figure below.

Page 2: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

2

Page 3: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

3

Step B2: Click on New Project pointed to by the green arrow in the diagram above to

start a new project. The following window will appear.

Page 4: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

4

Step B3: To create a new assembly project select C from the panel to the left

pointed to by the green arrow. Also type the file Name and Location pointed

to by the green arrows at the bottom of the window. The following window

will then appear. I n this example the projeck name is “Lab1Pre”.

Step B4: The window above is the device selection screen for Atmel Studio 6. Scroll

down and select the microcontroller you will be using. The following window

will then appear.

Page 5: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

5

This is the Atmel Studio 6 editor where you type your assembly program. The editor starts

your assembly program for you by providing you with the structure (in comment field) shown

in the editor of the figure above. You may optionally delete (or edit) the structure.

Step B5: Type the program given in Figure B.2(B5), on the Atmel Studio 6 editor.

Explanations on the program are given in the comments fields). Please do not cut

and paste. It is objectively targeted that by reading then writing back the

program line by line (and referring to B.6 thru B.15, the writer will develop:

1) The experience on typing a program.

2) The good practice to organize code.

3) The knowledge on the assembler directive used and their argument(s).

4) The knowledge on how assembler directive declare locations for writing program

and data in Program memory of the ATmega32 architecture.

5) The knowledge on how assembler directive declare locations for locations for

variable in Data memory of the ATmega32 architecture.

6) The knowledge on how the instructions and their operand(s) are written.

7) The knowledge on function of addressing modes for each instruction: where data

is taken (source) and where data is stored (destination).

You will pick-up new knowledge as you type in the codes while referring to the

comments.

Do not copy the comments as-is given in Figure B.2(B5). You are required to

develop your own original comments which can be done as you progressively type out

and understand the sequence of instructions.

/* * Lab1Pre.asm * * Created: 14/8/2016 10:59:00 PM * Author: Dell5110 */ /* Note that hexadecimal number can represented with either the prefix "0x" or "$" It is a good practice to just use either "0x" or "$" but in this lab both is used for demonstration purpose. Note also how comments are declared The ATmega32A is a functionally identical, drop-in replacement for the ATmega32. All devices are subject to the same qualification process and same set of production tests, but as the manufacturing process is not the same. Some electrical characteristics differ. In this lab ATmega32A device will be used. */ //.include "m32Adef.inc" ;Register/Bit Definitions for the ATmega32A /* By including this file "m32Adef.inc" in the assembly program file, all I/O register names and I/O register bit names appearing in the data book can be used. In addition, the six registers forming the three data pointers X, Y and Z have been assigned names XL - ZH. Highest RAM address for Internal SRAM (RAMEND) is also defined ($085F). */ .equ ten = 10 .cseg //Declare Program (Flash) Memory Space .org 0x0 reset: // jmp main //RESET vector at address 0x0000 /* When RESET event is invoked, PC <-- 0000. The CPU will execute instruction at pointed by PC. In normal procedure we will force PC to be set to the location of the bootup codes by using the "jmp start" instruction which effectively

Page 6: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

6

causes to jump to the address 0x2A. 0x2A is the address in Program memory just after the interrupt vector table of the ATmega32. We can omit this "jmp main" (and straight away continue with the bootup codes) if we are not going to use any interrupt in the program (which are unlikely in normal application of a CPU or microcontroller). */ //Address 0x0002 to 0x002f is reserved for Interrupt vector table .org 0xA /*In this lab1 since we are not going to use interrupt, we are using the vector table area for our program starting at address 0xA*/ main: /* Boot up codes starts here where STACK, I/O control registers and any other global variables used in the operation of the CPU, is initiallised. */ /* Process No: 1. Refer Section A.3(c) Initialise stack ponter at highest (end) of Data Memory (SRAM). Effectively, SP <-- 0x85F (value of RAMEND which is defined in "m32A.inc") */ ldi R16,low(RAMEND)//r16 <-- low byte of value RAMEND (using Immediate addressing

//mode) out SPL,R16 //SPH <-- R16 (using I/O addressing mode) /*R16 (one of the upper GPRs) is used as the intermediate variable to hold low byte of value RAMEND before being stored to SPL*/ ldi r16,high(RAMEND)//R16 <-- high byte of value RAMEND out SPH,R16 //SPH <-- R16 /*Finally SP<--RAMEND after sequential executions of the four instructions*/ //Refer Section A.7 for process 2 thru 11. /* Process No: 2. Now we are going to initialise num1 with value $2F*/ ldi r17,$2a //r17 <-- $2a using Immediate addressing mode sts num1,r17 //num1 <--r17 using direct addressing modes /*R17 (one of the upper GPRs) is used as the intermediate variable to hold value $2a before being stored to num1 (absolute address is $60) Finally num1<-- $2a after sequential executions of the two instructions*/ /* Process No: 3. Now we are going to copy data in num1 into temp1*/ lds r17,num1 //r17 <-- num1 using Direct addressing mode sts temp1,r17 //temp1<--r17 using direct addressing modes /*R17 (one of the upper GPRs) is used as the intermediate variable to hold value from num1 before being stored to temp1 (absolute address is $110)' Finally temp1 <-- num1 after sequential executions of the two instructions */ /* Process No: 4. Now we are going to do the calculation of sum = num1 + temp1 + 5 */ lds r0,num1 ;r0 <-- num1 lds r1,temp1 ;r1 <-- temp1 add r0,r1 ;r0 <-- r0 + r1 ldi r16,5 ;r16 <-- 5 add r0,r16 ;r0 < r0 + r1 sts sum,r0 ;sum <-- r0 ;Effectively sum <-- num1 + temp1 + 5 ($59) /* Process No: 5. Now we are going to initialise pointer Register Y with address of numbers3*/ ldi YL,low(numbers3) //YL <-- low byte of address of numbers3 ldi YH,high(numbers3) //YH <-- high byte of address of numbers3

Page 7: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

7

//effectively Y <-- address of numbers3 /* Process No: 6. Now we are going to store value $53 at address pointed by Y (value in Y)*/ ldi r16,$53 //r16 <-- $53 using R16 as intermediate (one of valid GPR) st Y,r16 //(Y) <-- R16 Using Register Indirect addressing mode /* Process No: 7. Now we are going to store value $35 at address which is 3 byte after address of numbers3 (displaced +vely) */ ldi r16,$35 //r16 <-- $35 using R16 as intermediate (one of valid GPR) std Y+3,r16 //(Y+3) <-- R16 Using Register Indirect with displacement //addressing mode /* Process No: 8. Now we are going to initialise value: $10, $11, $12, $13 and $14 in sequence in the array pointed by numbers3 (i.e address $80, $81, $82, $83 and $84) consecutively by using address register indirect with post increment method X register must be initialise to point to the first address of array numbers3 Consecutively using Register Indirect with post increment mode and r20 is pre-updated (initially r20<--$10 but subsequently r20 incremented) the operation: (X) <-- r20, X <-- X + 1 is executed 4 times */ /*we will need to initialise pointer Register X with address of numbers3*/ ldi XL,low(numbers3) //XL <-- low byte of address of numbers3 ldi XH,high(numbers3) //XH <-- high byte of address of numbers3 //effectively X <-- address of numbers3 /*Now in four consecutively phase: with r20 pre-updated, X <-- r20, x<--x+1 (initially r20<--$10, subsequently r20 incremented)*/ ldi r20,ten //r20 <-- $10: (r20 initialised value $10) st X+,r20 //(X) <-- r20, X <-- X + 1: (effectively [$080] <-- $10, X <-- $081) inc r20 //r20 <-- r20 +1: (effectively r20 <-- $11) st X+,r20 //(X) <-- r20, X <-- X + 1: (effectively [$081] <-- $11, X <-- $082) inc r20 //r20 <-- r20 +1: (effectively r20 <-- $12) st X+,r20 //(X) <-- r20, X <-- X + 1: (effectively [$082] <-- $12, X <-- $083) inc r20 //r20 <-- r20 +1: (effectively r20 <-- $13) st X+,r20 //(X) <-- r20, X <-- X + 1: (effectively [$083] <-- $13, X <-- $084) inc r20 //r20 <-- r20 +1: (effectively r20 <-- $14) st X+,r20 //(X) <-- r20, X <-- X + 1: (effectively [$084] <-- $14, X <-- $085) /* Process No: 9. Now we are going to copy in sequentialy, the string of 1 byte data in address numbers3+0 ($80), numbers3+1 ($81),numbers3+2 ($82) and numbers3+3 ($83) to array pointed by numbers2+3 in REVERSE ORDER (address $67, $66, $65, $64) Effectively sequentially, the following will happen: (numbers2+3) <-- (numbers3) (numbers2+2) <-- (numbers3+1) (numbers2+1) <-- (numbers3+2) (numbers2) <-- (numbers3+3) */ lds r16,numbers3 sts numbers2+3,r16 lds r16,numbers3+1 sts numbers2+2,r16 lds r16,numbers3+2 sts numbers2+1,r16 lds r16,numbers3+3 sts numbers2,r16

Page 8: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

8

/* Process No: 10. Now we are going to read 4 successives byte of data array from numbers1 which is in Program Memory to array numbers4 which is in Data memory */ ; refer Section A.3(b) Data in Program Memory ldi ZH,high(numbers1<<1);ZH<--high byte of absolute byte address of numbers1($200) ldi ZL,low(numbers1<<1) ;ZL <-- low byte of absolute byte address of numbers1 ldi YH,high(numbers4) ;YH <-- high byte of absolute byte address of numbers4 ldi YL,low(numbers4) ;YL <-- low byte of absolute byte address of numbers4 ldi r17,4 ;Set repeat loop = 4 repeat: lpm r18,Z+ ;R18 <-- (Z),Z <-- Z+1 st Y+,r18 ;Y <-- R18, Y <-- Y+1 dec r17 ;R17 <-- R17-1 brne repeat ;Repeat until (R17==0) /* Process No: 11. Program does an infinite loop (halt at location "here") as it continuously executes "rjmp here" In the loop, the program will continuously read the data from SS_Table one at a time transfer it to Port B each time. The data to be taken will be wrap around to the first data when the last data in the array is taken and sent to Port B. Before you can wirite to Port B, Port B will and must be set as an output port once beforehand. Accessing I/O ports: To Set Port B as Output you must write 0xff to DDRB regiter (I/O Memory addressing mode) To send out Data to Port B you must write to PORTB. (I/O Memory addressing mode) Refer Section A.7(d) Accessing data in Flash Memory (ROM) To read from SS_Tablke which is in Flash Memory, you mus use Programming Memory Addressing Mode Refer Section A.7(e) */ here: rjmp here .org $50 //Address to start at 0x50 in Program Memory SS_table: .db $3f,$06,$5b,$4f,$66,$6d,$7d, $03, $7f, $6f //SS_table declared at

//address $50 numbers1: .db 0x10, 0x1,0x12,0x3,0x14,0x15,0x6,0x7,0x18,0x9 //numbers1 declared at //address $5A .dseg //Data Memory(RAM) Space (by default at address start $60) num1: .byte 1 ;num1 declared as 1 byte storage by default at address $60 num2: .byte 1 ;num2 declared as 1 byte storage at next address=0x61 data1: .byte 1 ;data1 declared as 1 byte storage at next address=0x62 data2: .byte 1 ;data2 declared as 1 byte storage at next address=0x63 numbers2: ; .byte 7 ;numbers2 declared at address $64 as a 7 byte array storage endofnumbers2: .byte 1 .org 0x80 //Address to start at 0x100 numbers3: .byte 6 ;numbers2 declared as a 6 byte array storage numbers4: .byte $10 ;numbers2 declared as a 16 byte array storage at address 0x110 temp1: .byte 1 ;temp1 will be declared at address 0x96 temp2: .byte 1 ;temp1 will be declared at address 0x97 sum: .byte 1 ;temp1 will be declared at address 0x98

Figure B2(B5)

Page 9: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

9

Step B6: Closing/Opening an Existing Project.

Whenever it need be, you can always edit existing project that has been created and closed

and close. How to do this can be self-learned.

B.3. Debugging

Debugging is a process of finding logical errors in a program. However though you can use

debugging to trace the flow of a program and monitor changes of Register, memory location

or variables (which uses register, memory location as their storage), debugging can also

be used to understand of confirm the operation of instructions, subroutines, statement or

functions in a program.

There are no fixed steps on debugging a program. The concept is that we need to know where

and what caused the bug so that we can do the correct rectification on the program.

Conceptually, to find the bug, we need to know what the input to the program is, and/or

what the output is, and/or whether logically (in decision instruction) it is doing as what

it should be. So to do this we may use one of the following methods:

1. Single Step by Step, using Step Over or Step Into operation.

2. Set breakpoint(s) at location where we want to inspect the input or output of

the program. Then we run (or simulate) until the program reach the breakpoint.

3. Step Over an instruction or function (or procedure) and inspect the input or

output of the program’s instruction or function (or procedure).

4. Step Into the function (or procedure) and inspect the input or output of the

program’s function (or procedure).

5. Note: Step Over an instruction is the same operation as Step Into the

instruction.

Step B7: Selecting Target of Program when to Debug

Atmel Studio 6 allows the target to be selected. If no external target is

connected, by default AVR simulator will be selected. To make/confirm selection

select ProjectLab1Pre Properties.... You can click “Selected debugger/programmer”

select available target of which in this lab it will be “Simulator”. Unselect the

“Preserve EEPROM” checkbox.

Page 10: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

10

Figure B2(B7)

a. Starting and Ending a Debug Session in Atmel Studio 6

To start a debug session and halt press Alt+F5 or choose Debug→Start Debugging and

Break from the menu, alternatively, press the toolbar button as illustrated below:

To Continuer a debug session (when in Break) and keep executing press F5 or press the

toolbar button with the continue symbol or choose Debug→Continue from the menu as

illustrated below:

To end the debug session use the Stop Debugging button or keyboard shortcut Shift+F5.

Page 11: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

11

b. What is a Break.

The Break is a situation in debug session when execution of instruction is halted. PC

(Program Counter) will hold (pointing) to the next instruction the CPU will execute.

c. Debug control

Several commands are available for controlling the debugger. They are available from

both the Debug menu and several toolbars. The Atmel Studio Integrated Development

Environment (IDE) has two major operating modes: design mode and debug mode. Design

mode is when you are editing the source code project, while debug mode is when you

debug your project. The IDE adapts to modes, and menus an toolbars changes.

Note

Some debug commands are available in design mode, some in debug mode.

In design mode, the available debug commands are those that will start the debug

session, e.g. Start Debugging and Break, Start Debugging, Start without Debugging.

In debug mode, you will find commands like Break All, Step Out and Reset.

Figure B.3(c) shows the pull down menu of the debugging command when the Debug menu is

selected while in Debug Mode:

Figure B3(c)

The following are explanation on some of the commands.

Start Debugging and Break

Starts the debugger, and breaks the execution on the first statement of the program (RESET vector).

Start Debugging

Starts the debugger, and runs the program. In debug mode and stopped, it resumes execution.

Start Without Debugging

Page 12: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

12

Programs the project without starting debugging. For details, see Debug: Start without debugging.

Break All

Halts (breaks) the debugger. Normally we select this when we want to break a running program.

Stop Debugging

Stops and terminates the debug session, and returns to design mode.

Restart

Restarts the debugger and reloads the program.

Reset

Resets the program to the first statement

Step Into

Executes one instruction. When in disassembly level, one assembly level instruction is executed, otherwise one source level instruction is executed.

Step Over

Similar to Step Into, Step Over executes one instruction. However, if the instruction contains a function call/subroutine call, the function/subroutine is executed as well. If a user breakpoint is encountered during Step Over, execution is halted.

Step Out

Continue execution until the current function has completed. If a user breakpoint is encountered during Step Over, execution is halted. If a Step Out command is issued when the program is on the top level, the program will continue executing until it reaches a breakpoint or it is stopped by the user.

Quick Watch and Watches

Adds a Quick Watch for the variable or expression under the cursor.

Toggle Breakpoint

Toggle the breakpoint status for the instruction where the cursor is placed. Note that this function is only available when the source window or disassembly window is the active view.

New Breakpoint

Create a new breakpoint at the location of the cursor.

A breakpoint tells the debugger to temporarily suspend execution of a program when a specific condition takes place, e.g. when a certain instruction is about to be executed.

Breakpoints provide a powerful tool that enables you to suspend execution where and when you need to. Rather than stepping through your code line by line or instruction by instruction, you can allow your program to run until it hits a breakpoint, and then start to debug. This speeds up the debugging process.

Disable All Breakpoints

This function clears all set program breakpoints, including breakpoints which have been disabled.

d. Breakpoint glyphs (icon)

The source windows and the Disassembly window show breakpoint locations by displaying

symbols called glyphs in the left margin. The following table describes these glyphs.

Page 13: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

13

If you rest the mouse on a breakpoint glyph, a breakpoint tip appears with more

information. This information is especially useful for error and warning breakpoints.

Icon Description

Normal breakpoint. The solid glyph indicates that the breakpoint is enabled. The hollow glyph indicates that it is disabled.

Advanced breakpoint. Active/disabled. The + sign indicates that the breakpoint has at least one advanced feature (such as condition, hit count, or filter) attached to it.

Breakpoint error. The X indicates that the breakpoint could not be set because of an error condition.

Breakpoint warning. The exclamation mark indicates that a breakpoint could not be set because of a temporary condition. Usually, this means that the code at the breakpoint or tracepoint location has not been loaded. It can also be seen if you attach to a process and the symbols for that process are not loaded. When the code or symbols are loaded, the breakpoint will be enabled and the glyph will change.

e. Applying Debugging as a learning aid tool.

By using debugging technique you can:

1. Visualise the architecture of the ATmega32A by using Processor View, Memory Views

and I/O Views to identify the physical components in the processor.

2. You can note the relationship between each view. For instance the data in R27:R26

represent the data in X pointer register (refer Figure A.5 (d)) of Appendix A for

SKEE3732 Laboratory 1 Sheet. Try changing R26:R25 and you will find that X will also change.

3. By single stepping (Step Over or Step Into) an instruction you can find out or

confirm your understanding on the execution of the program i.e. the changes of

Registers or Memory Location as defined in the Instruction Set Reference, the

location of data taken from or send to as defined by the addressing modes of the

instruction.

4. By Stepping Into a subroutine call (CALL or RCALL instruction), you can find out

or confirm your understanding on the execution of the Instruction, i.e. the

changes of PC and STACK and trace (single step) into the subroutine.

5. By Stepping Over a subroutine call (CALL or RCALL instruction), you can find out

or confirm that your understanding on the execution of the whole subroutine

without getting into the subroutine.

6. You can simulate condition before you execute an instruction by setting contents

the register or memory location that is used by the instruction before executing

it so that you can visualise/confirm the effect of the register or memory location

data in towards the execution of the instruction.

7. By tracing through a program you can confirm/examine the program flow of a program

and you can visualise/confirm that PC (Program Counter) directs the flow of the

program. So you can see how Program Flow Instructions (Branch Instructions)

changes the PC in accordance to the Instruction Set reference.

8. You can use Breakpoints to force the program to stop at the any of the Breakpoints

the program will pass through.

9. By using Breakpoints you can execute your program such that it skip a sequence of

code that you do not want to trace and stop at the instruction that you want to

begin you single stepping. This will help you overcome repeating tracing that code

that you have confirmed not needed (to save time and energy doing the tedious

stepping over) especially after you restart the program to test the working of the

program after making changes on code.

Page 14: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

14

Note: Select Debug Stop Debugging to clear all registers and Memory contents to

avoid confusion of old data contents during previous debugging process.

10. Certain instruction can only be executed in the program is running so you cannot

use single step method. By setting breakpoints at the instruction you can stop the

program when the certain event occurs. From that breakpoint you can trace the

program if needed. These event can occur from interrupts INT0, INT1 , INT2, ADC or

Timers or input from switches that is check by conditional branch instructions.

11. When you are debugging a program that reads data from Digital I/O ports, you can

simulate the input by setting the revenant PINxn bit before you trace or run the

program. By doing this you can check the flow of the program that check the status

of the input.

Page 15: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

15

B.4. Viewing the Disassembly of the ATmega32 Program

Step B8: To view the disassembly Display, in Debug mode Select

DebugWindowsDisassembler which will open Disassembler window Tab as

shown in Figure B.4(B8). The Disassembly window is only available when

debugging.

The disassembly window shows your program code disassembled. Program execution and AVR

instructions can be followed in this view. By right clicking inside the Disassembly window

you will be able to set breakpoints, run to the position of the cursor or go to the source

code. You cannot modify the source code from the Disassembly window.

Figure B.4(B8): Disassembler View

In addition to assembly instructions, the Disassembly window can show the following optional information: 1. The next instruction to be executes. Identified in box with circle #1

2. Memory address where each instruction is located. For native applications, this is the actual memory address. Identify labels address from the respective instruction. Identified in box with circle #2

3. Code bytes byte representations of the actual machine. Identified in box with circle #3

4. Source code from which the assembly code derives. Identified in box with circle #4.

5. Symbol names for the memory addresses. Identified in box with circle #5.

3

2

4

4

4

4 3

1

Page 16: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

16

6. Original source code instruction. Not Shown in Figure.

B.5. Editing values and bits in break mode

When the project is in debug break mode, any value can be changed by clicking the value

field and writing a new value. Some values and bits cannot be modified as they are read-

only, and some bits may be write-only. See the documentation for each device for more

information. When a bit or value is set, it is immediately read back from the device,

ensuring that the I/O View only displays actual values from the device. If a new value is

set, but the I/O view does not update as expected, the register might be write-only or

simply not accessible.

When a register has changed since last time it was displayed, it will indicate so with a

red coloured value and bits in the display. If a bit has been set since last time, it will

be solid red. If it has been cleared it will simply have a red border. This feature can be

toggled on or off in the toolbar.

Page 17: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

17

B.6. Viewing the ATmega32 Processor’s Registers

Step B9: Select Debug→Windows→Processor View. The processor view offers a

simulated or direct view of the current target device MPU or MCU. In you

can see a partial list of the simulated device's ATxmega32/ATmega32A

registers.

Figure B.6(B9): Processor View window

The program counter shows the address of the instruction being executed.

The stack pointer shows the application's current stack pointer value.

The X, Y and Z registers are temporary pointers that can be used in indirect passing or

retrieving arguments or objects to and from functions. Cycle counter counts the cycles elapsed from the simulation’s start.

Status register or SREG shows the currently set flags.

Further on you will be able to toggle a setting for displaying the flag names.

The stop watch field allows you to make rudimentary profiling of your application. It is influenced

by the frequency set in Frequency field, which defines target MCU/MPU frequency, in case when

the prototyping board is connected.

Each register can be displayed in hexadecimal, decimal, octal and binary (flag) format by right-

clicking and choosing Display in binary, etc., or Display in... . Each field can also be modified, as

Page 18: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

18

shown in the below image. If a field is a status or flags register, composed of a number of the

one-bit flags, you can toggle individual flags by clicking on them -

The processor view is only active in the debug mode.

B.7. Viewing the ATmega32 Memory Space

The memory view gives you an outline of the memory.

It is possible to select among the attached memories to see all the segments by switching

between them in a Segment drop-down menu on top of the memory view, you can also specify

the starting address for the memory view window in the Address form field on top of the

memory view.

In order to specify the address you can use either a normal hexadecimal entry or an

expression: See the section called “Expression formatting”. The Columns drop-down menu allows

to specify how many byte-aligned memory columns you wish to see at one time, most often

that should be left at Auto setting, but if you have to manually check a fixed-length type

values and you know how many words or bytes those values occupy, you could align the

memory view so that each row will correspond to a desired number of values.

a. Viewing Instruction data in Program Memory

Step B10: Select Debug WindowsMemory viewWindow 1, or Ctrl+Alt+M+n where n is

the memory's number (1).

Figure B.7 (a): Memory 1 window with “prog Flash” (Program memory), address start at 0x00

and Auto column selected.

This is the machine code which is also displayed in a different format shown in

display in Figure B.4(B8). Inspect address 0x0000 and 0x000A the similarity and

difference between the two displays.

b. Viewing .DB data in Program Memory

Step B11: Now change the setting such that address = 0x0050 * 2 = 0x00A0 (Refer

Section B.3 Figure B.3). This is the Address of SS_table declared in

program memory of program “Lab1pre”.

Also set Columns : 2. The display will be in Figure B.4(B8).

Page 19: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

19

Figure B.7(b): Location and contents of SS_table declared in Program Memory of

program “Lab1pre” using “.DB” assembler directive.

You will see the physical address of the “.DB” assembler directive in Program

Memory and the content of the memory locations from the .DB declarations.

B.8. Using Watch window to determine address of label (address may be used as variables)

Step B12: Select Debug WindowsWatch.

Figure B.8(B12): Example of Watch window for some of the variable from Lab1Pre project.

1. The identifier under the “Name” are valid labels declared in the program being

debug that have to be entered manually.

2. Watch window can be used to determine value of labels declared in the program

3. Using this value as address in memory window, you can watch content of the label

(as variable) by watching the address.

B.9. Debugging the program Using Single Step

In Debug mode There are two commands to single step through the code. These 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 commands in this example.

Page 20: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

20

Step B13: Close “Lab1Pre” and reopen the project. You should be able to find the

folder, otherwise search for “Lab3pre.atsln. You should see the following

display. If there is no “Lab1Pre.asm” double-click the “Lab1Pre.asm” icon

in the “Solution Explorer” window.

Page 21: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

21

Step B14: Make sure that the “Selected debugger/programmer” is on “Simulator”.

Step B15: Select “Start Debugging and Break” command and you will see the following

display. If the “Processor” window is not open, open it (Debug

Windowprocessor View)

Figure B.9(B15): Debug and Processor View on ““Start Debugging and Break” command.

The yellow arrow will be pointing to the instruction “jmp start” addressed (pointed) by

Program Counter which is at 0x000000 which is the next instruction to be executed.

Page 22: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

22

B.10. Single Step through your program

Step B16: Noting that PC is at address “0x00000A”, To Single Step over instruction

“jmp start” strike the [F10] key once):

Note that the word in red indicates the register that is affected from the

execution changes from the previous value. If the field is affected from the

execution but the result is the same value as before (no change), the colour of

the word in the fields will be black (as if the register is not affected from

the instruction – so be careful not to assume so).

This command will cause the simulation to execute the instruction “jmp start”

pointed by Program Counter and stop at the next instruction which in this case

will be “ldi R16,low(RAMEND)” at address 0x000A shown by the yellow arrow. The

Program Counter will be updated to point at the next instruction which is 0x000A

which is also the address of “start”.

Figure B.10(B16-1): Display after single-stepping instruction at address 0x000000

(execution of “jmp start”)

Referring to the instruction “JMP” in page 82 the in the file “AVR-Instruction-Set

2002.pdf” as shown below, the PC take the value of the operand that is “start” and the is

no changes in the SREG.

To check the Status Register, you will need to view the Processor window. Grey colour text in

the boxed (eg. C for Carry bit) represent that that the bit is 0. A black font colour

represents that that the bit is 1 (not shown right now).

Page 23: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

23

Figure B.10(B16-2): JMP instruction ” in page 82 the in the file “AVR-Instruction-Set 2002.pdf”

Page 24: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

24

Step B17: Single step again over instruction “ldi R16,low(RAMEND)” and inspect

content of R16 and SREG (Status Register).

This command will cause the simulation to execute the instruction “ldi R16,low(RAMEND)” and stop at the next instruction which in this case will be “out SPL,R16” at address 0x000B shown by the yellow arrow. The Program Counter will be updated to point at the next instruction which is 0x000B. R16 will be loaded with 0x5F. Red color on the word denotes that the value has changed

Figure B.10(B17): Display after single-stepping instruction at address 0x00000A (execution

of “ldi R16,low(RAMEND”) “out SPL,R16”.

Page 25: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

25

Step B18: Single step again and inspect content of R16 and SREG (Status Register).

This command will cause the simulation to execute the instruction “out SPL,R16”

pointed by Program Counter and stop at the next instruction which in this case

will be “ldi R16,high(RAMEND)” at address 0x000C shown by the yellow arrow. The

Program Counter will be updated to point at the next instruction which is 0x000C

which is also the address of “ldi R16,high(RAMEND)”.

Do the single stepping until the instruction “rjmp here”.

If you want to check the memory location, you may check using the memory window (refer

Section B.7).

B.11. Using Breakpoint

Breakpoints are a method of halting execution flow. By adding a breakpoint in the assembly

code we can run the program at full speed, and it will be stopped at the line with the

breakpoint. By now you have noticed that you have to press<F11> three times to go through

the loop once. We will add a breakpoint at the by placing the cursor on the instruction in

the source view window and press <F9> (or the "Toggle Breakpoint" in the "Debug" menu ). A

red circle will appear in the left margin of the source view window as shown. By

pressing <F5> or "Run" from the "Debug" menu the program will start running and break

(stop) at the instruction with the breakpoint.

There may be situations where we have single stepped through far in the program but you

may need to reverse a few steps back to review a result. We will need to Stop Debugging

and start over sequential single stepping again until we reach the instruction we want to

review which can be unnecessarily tedious.

The best method to overcome this is to set a breakpoint at the instruction we want to

stop, RUN the program so that it will stop at the breakpoint it meets. Then you may single

step through from there.

To set a breakpoint, click on any row to put the cursor on the line of the instruction

where you want to break (stop) as show below (in this example “ldi r20,ten” ). Do not

select the whole line. Then press F9.

Page 26: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

26

You will see a red dot on the side of the instruction as shown in the following display.

You may set another (or many) breakpoint(s) that we need so that we may CONTINUE running

the program until it meets the next breakpoint.

You may remove any breakpoint by pressing F9 or clicking the Toggle Breakpoint icon. To

remove all breakpoint, select Debug|Remove all Breakpoints or click the Remove all

Breakpoints icon.

B.12. Stopping Debug

Step B19: Stop Debugging by selecting the menu Debug| Stop Debugging.

Page 27: APPENDIX B FOR SKEE3732 LABORATORY 1 SHEET B For... · Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx 3 Step B2: Click on New Project pointed to by the green arrow in

Appendix B For SKEE3732 Laboratory 1 Sheet (Rev 120717).docx

27