0% found this document useful (0 votes)
99 views13 pages

Lab 1

Uploaded by

shivaspy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views13 pages

Lab 1

Uploaded by

shivaspy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab 1 Rev. 1.

4 Software Setup & Assembly Programs

1
Software Setup & Assembly
Programs

1.1 Objectives:

The ARM Cortex-M4 processor executes a set of 16 and 32 bit instructions, called the
Thumb-2 instruction, yielding a good tradeoff between code density (16 bit instructions)
and performance (32 bit instructions). In ECE 251, assembly language is the
programming language of choice. This lab will introduce the basics of the Thumb-2
Instruction Set and the software development environment for developing and debugging
programs using our Arm Cortex M4 processor and TM4C123GXL (Tiva) Launchpad
development board. When you complete this lab, you should be able to:

• Understand the fundamentals of assembly programming


• Recognize and use a few basic Thumb-2 instructions
• Understand Keil μVision (a tool for embedded software developers who write
software in assembly language or C for microcontrollers).
• Connect to the Tiva board and download a program to it using Keil μVision.
• Run a program on Tiva and examine changes to memory and registers.
• Use breakpoints and stepping to debug a program.
Because you will perform all of these procedures in every lab you do after this, a
complete understanding of the material in this lab is necessary.

1.2 Related material to read:

o Text: Embedded Systems with ARM Cortex-M3 Microcontrollers in


Assembly Language and C, Chapter 3 (ARM Instruction Set Architecture)
(Section 3.3 Optional)

-1-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

1.3 Assembly Programming Fundamentals

An assembly language, like many other computer languages, consists of a series of


instruction statements that tell the CPU what operations to perform. An assembly
language has three types of statements: Assembler Directives, Instructions, and
Comments. Assembler directives are interpreted by the assembler to define program
constants and reserve space for program variables, as simple examples. Assembly
instructions are divided into two sets of fields: Operation (or Mnemonic) and Operand
fields. The Operation field consists of the mnemonic names for the machine instructions.
The Operand field contains the operand(s) and assembler directive arguments (if
necessary). The Comment field is an optional field that allows programmers to document
their code. Comment fields are ignored by the assembler and begin with a semicolon ‘;’.
Finally, there is another optional field called the Label field. A label allows a
programmer to use symbols for memory locations, which make assembly programs easier
to read and write. As it turns assembly code into object code, the assembler “replaces”
each label with its respective memory location. Labels will be used extensively when
you begin programming, but for now, let’s put this idea on hold.
When you begin writing assembly programs, you will follow a format specified for the
class; however, each instruction will use the following structure:

LABEL OPERATION OPERAND COMMENTS

NOTE: The Operation field contains the assembly mnemonic for the corresponding
machine instruction and cannot start in the first column of the program (only labels can
exist in the first column). A common practice is to separate each field with a tab (or
multiple tabs). The operand field contains the arguments required for the machine
instruction or assembler directive, with comments following at the end of the instruction.
Let’s look at the example below and clarify some common nomenclature used in this
course. First, notice that the label field is not used in this example. Second, the
Operation field contains the mnemonic for the machine instruction: mov. The first
operand, r2, is a register name, in this case the name of the register to be loaded. The
second operand, #10, is the decimal number 10. The ‘#’ symbol refers to a type of
addressing mode, which is discussed below, but in layman’s terms, this instruction is
simply telling the CPU to put the value of 10 (base 10) into register r2. The comments
field shows this by using some common nomenclature that will be used in this course.
The second instruction instructs the CPU to add the value 11 (base 10) to the contents of
register r2 and store the result in register r4. The brackets around the register name in its
comment field indicate the contents of register r2. Note that these comments describing
the behavior of the instructions are NOT appropriate when you are writing your
programs, but they will be used extensively for instructional purposes here and in class.
Your comments should describe the higher level operations you are performing.
LABEL OPERATION OPERAND COMMENTS
mov r2, #10 ; move 10 into reg #2
add r4, r2,#11 ; [r2] + 11  r4

-2-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

What do you think the contents of registers r2 and r4, i.e. [r2] and [r4], will be after these
two instructions are executed?
Now that we have some idea of what instructions look like, let’s look at the tool we will
be using to work with programs, which are just an ordered collection of instructions,
directives, and comments.

1.4 Board startup:

CAUTION: Your development board is sensitive to static electricity. Be sure to touch


something grounded before touching the board.

The Texas Instruments Tiva is a single board computer development system for the TI
TM4C123GH6PM microcontroller (one of the TM4C123 family). Support software for
this development board is provided for Windows XP/Vista/Windows7/Windows10
operating systems. The Tiva board is a small development board designed for low cost,
and has several common I/O ports. However, you won’t have to worry about using any
I/O ports or a breadboard for a few more lab sessions.

The Tiva development board uses option select jumpers to configure the board operation.
The jumpers should already be set for default board operation.

A “Jumper” is a plastic encased wire shunt that connects two terminals electrically.

Jumper on, in, or installed means the jumper is installed such that the two terminal pins
are connected together.

Jumper off, out, or idle means the jumper or shunt is installed so that only one pin holds
the shunt (i.e., there is no electrical connection between the two terminal pins through the
jumper), or the jumper is removed.

Now follow these steps to connect and power on the board for debugging operations:

1. Ensure the board options are in the default positions:

VDD Jumper = ON

SELECT switch = DEBUG

2. Connect one end of the supplied USB cable to the development board, and the other
to the USB port on your PC.

1.5 Create a Project Folder

-3-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

You will need to create a project folder in an appropriate place in your computer
file system. On your own computer a good folder path could be something like
…/Documents/ECE251/Projects . Each of your ECE251 lab projects should go in its own
folder within this folder.

Using Keil μVision with development board:

Run the μVision application, making sure you see the 2 sub-windows: the Project
window on the left, the Build Output window along the bottom (see Lab 2). The Build
Output and Project windows can be made visible or invisible using the View menu at the
top of the program.
All files associated with each of your projects, debug, and build settings are
organized and saved using Project files. Each project will contain all of the assembly files
created for an individual lab or project.
To start a new project, go to the Project menu > New μVision Project
Navigate to the Project Folder you just created and select a name for your project.
Next, select the device you are using. In our case, the Tiva development board
uses the TM4C123GH6PM microcontroller. Expand the tree inside of the Target window
to find the correct processor. Texas Instruments > Tiva C Series > TM4C123x Series.
Scroll way down until you find the TM4C123GH6PM and select it, then click OK.

The next window to pop up is the Manage Run-Time Environment window.


However, nothing needs to be configured here, so simply select OK.
In order to use the correct driver for TM4C123GH6PM microcontroller select Project >
Options for target > Debug and choose Stellaris ICDI from drop down menu as shown.

-4-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

You will notice that the Project window now has a


tree structure. Expanding the Target1 folder shows
the Source Group 1 folder. The Source Group 1
folder is where all of your assembly files will be
added.

There are two ways to add assembly files to your project. Right-click on the Source
Group folder and either select Add New Item to Group –or- Add Existing Files to Group.

The Add Existing option is for when you already have an existing assembly file
that you would like to use in your project.
The Add New Item option is used when starting an assembly file from scratch.
Selecting this option walks you through creating a new assembly file and opens the new
file as a new tab in the Editor.
Any new or old program file can be opened in the Editor (like any word
processor) using either the File menu or double clicking it in the Project window. Saving
the files can be done by selecting File > Save, or the save button in the menu.
NOTE: If you want a file to be used in your project it MUST be added to the Source
Group folder. Otherwise, μVision does NOT consider it during build time.
Once an assembly program has been written, it will need to be built (assembled).
This can be done using Project > Build target. When the program is built, other files with

-5-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

the same name, but with other file extensions are created. The .lst file is a common listing
file, which provides physical address information with operation and operand
information. It also gives the error information above the line having the error. Make sure
to edit the main program and not the .lst file.
Upon a successful build, the Build Output window will display something similar
to the following:
Build target 'Target 1'
assembling Lab1.s...
linking...
Program Size: Code=40 RO-data=0 RW-data=0 ZI-data=1024
".\Objects\[Link]" - 0 Error(s), 0 Warning(s).

Having built your program with “0 Error(s)”, the next step in the process is to
download it to your board. From the toolbar, choose Download . The Build Output
window shows messages about the download progress:

If the pop-up window shown below on left appears and/or Build Output window
shows message on right, the driver may incorrect or the device may not be attached.

1.6 Debugging:

After a successful build/download, you can now enter debug mode using Debug ->
Start/Stop Debug Session or the Debug icon in the top menu. When entering the
debug session, the look of μVision will change to display windows helpful to debugging.
• On the left is the Register window that shows the current values of each of the
registers.
• In the middle is the Editor window that contains the files to be debugged.
• At the top is the Disassembly window which shows the .lst file that was created
during the build.
• On the bottom left is the Command window which shows any processes currently
running by μVision . It also shows any errors that may come up along the way.
• The bottom right contains two windows. The Call Stack and Memory windows.
The Call Stack window shows current status of the stack and variables of the
project. The Memory window is used to view and (in some cases) modify the
values at memory addresses.
Other options to Run, Stop, and Step through your program also become available.

-6-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

When first entering the Debug Session, the program will be pointed to the very first
operation of your program. Typically, this is the first line of code under the label
Reset_Handler. The current position of the program will be indicated by a yellow arrow.
Note that the current line of the program has NOT yet been executed. The cyan arrow is
simply the line your cursor is on.

Current Position of program

Position of cursor

Breakpoint

Right after entering the Debug Session, your program does NOT automatically run. It
waits at the first line of code waiting for input from you. You have the option to Run or
Step through your code.

To run the program, select Debug > Run, or the Run icon in the debug toolbar (key
F5). Selecting Run will let the program keep executing operations in real time until it
either comes to a line with a breakpoint or the end of the program.

To Step through your program line by line select Debug > Step or the Step icon in
the debug toolbar (key F11). Selecting Step will execute one line only.
Breakpoints are markers you set at places you would like the program to “pause”. When a
line with a breakpoint has been reached, the program stops on that line. All values in
memory and registers are current allowing you to check if they are the values you expect.
Note that breakpoints do not end the program. They simply “pause” it. You can continue
the program by clicking Run or by Stepping.
You can add breakpoints two different ways. One way is to simply click in the dark gray
area left of the line you wish to stop on. Another way is to place your cursor on the line
you would like the program to stop on, and then select Debug > Insert/Remove
Breakpoint (key F9). If a breakpoint already exists, the same actions will remove the
breakpoint.

1.7 Memory and registers:

μVision allows you to view the contents of any memory address using the
Memory Window. At the top of the Memory Window you can type in the address you
wish to view. For example, typing 0x00000018 will displays the values in memory
starting at address 0x00000018. Note that μVision assumes leading zeros, so
0x00000018, 0x0018, and 0x18 are the same addresses.

-7-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

It can be seen that the values are grouped in twos. This is because the Memory Window
follows the physical memory addressing design of the TM4C. Each 32-bit memory
address is a pointer to an 8-bit space of memory. Each group of two hex numbers are 8-
bits (1 byte), therefore each group of two hex numbers are a different address. For
instance in the example above the memory and values are as follows:
Address Value
0x00000018 0x30
0x00000019 0x31
0x00000020 0x32
0x00000021 0x33

To modify any of the values, simply double click the value and type in a new one. Note
that some memory locations are read only, and cannot be modified.
The Register window functions in a similar manner, but shows the current values of the
registers located on the CPU of the TM4C. Each register is 32-bits wide, so when storing
register values in memory they will take up 4 address spaces in memory. To modify a
register value, double click on the register value and enter a new value.
Note: Modifying memory and register values are done only to assist in debugging your
program. They should not be modified as a regular process, or as input to a program.
Memory and Register windows are most helpful as a way to view the values of specific
memory locations and registers.

Figure 2.1: The Cortex-M4F register model

-8-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

Table 2.1: Memory Map for Tiva Development Board


Address Start Address End Memory application
0x0000.0000 [Link] On-Chip Flash (Where you will program)
0x0004.0000 [Link] Reserved
0x2000.0000 0x2000.7FFF Internal SRAM
0x2000.8000 [Link] Reserved
0x2210.0000 [Link] Reserved

1.8 Running an application using μVision:

Start a new project and download the assembly files named Startup.s and
PracticeLab.s from the class website’s Lab folder Software Downloads and add it to the
project’s Source Group folder. Now build (or assemble) this program by following
Project > Build on the menu bar. This will generate [Link] in the Listings
directory. Connect the board to the PC and download the program to the board.

Figure 1.2: Program PracticeLab.s. Lines starting with ‘;’ are the comments written in
the program.
;***************************************************************
; Program PracticeLab.s
; Clear memory locations 0x2000.0400 - 0x2000.041F,

-9-
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

; then load these locations with consecutive numbers starting


; with 00.
; 'CONST' is the number of locations operated on.
; 'FIRST' is the address of first memory address.
;***************************************************************

;***************************************************************
; EQU Directives
; These directives do not allocate memory
;***************************************************************
;SYMBOL DIRECTIVE VALUE COMMENT

FIRST EQU 0x20000400


CONST EQU 0x20
;***************************************************************
; Program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
AREA main, READONLY, CODE
THUMB
EXPORT __main

__main LDR R1,=FIRST ; Initialize registers


MOV R0,#0x00 ;
LDR R2,=CONST ;
loop1 STRB R0,[R1] ; Clear memory
ADD R1,R1,#1 ; Increment address
SUBS R2,R2,#1 ; Decrement contant
BNE loop1
LDR R1,=FIRST ; Reset address
LDR R2,=CONST ; Reset constant
loop2 STRB R0,[R1] ; Store value in memory
ADD R0,R0,#1 ; Increment value
ADD R1,R1,#1 ; Increment address
SUBS R2,R2,#1 ; Decrement contant
BNE loop2
done WFI ; Infinite loop to
B done ; end program
;***************************************************************
; End of the program section
;***************************************************************
;LABEL DIRECTIVE VALUE COMMENT
END

When the program is downloaded to the board, it is stored starting at address


0x0000.0000. Enter Debug mode and step through the program. In the MemoryWindow,
examine memory locations 0x0000.0000 – 0x0000.003F. These memory locations should
contain the object code of the program. You can compare the Disassembly Window to the
values in memory. Notice in the Disassembly Window the program is pointed to address
0x0000.0280, and the value at that address is 0x490A. Look at address 0x0000.0280,
0x0000.0281 in the Memory Window and you should see 0A 49.

- 10 -
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

Now look at memory locations 0x2000.0400 – 0x2000.041F. This program


modifies these memory locations by first loading 0x00 into all of them, then storing
consecutive numbers starting with 0x00. Step through the program by pressing F11.
Watch as the program clears the memory locations and then populates it with consecutive
numbers. Also pay attention to the values in the registers and how they relate to the
instructions being executed.

1.9 Debugging the program:

Here we will introduce a few more debugging tools. When you start writing larger
programs it won't always be practical to single-step the entire program, but you can stop
at particular places by using breakpoints. After you have stepped through the program
and understand how the program works, reset the program by selecting Debug > Reset
CPU or the Reset icon in the debug menu. This places the Program Counter back at
the beginning of the program. Place a breakpoint at loop2 and Run the program (F5). The
program should have run through the first loop, clearing the memory, and stopped at your
breakpoint. Notice that memory locations 0x2000.0400 – 0x2000.041F are zeros again.
Pressing F5 again allows the program to run through 1 cycle of loop2 because it hits the
breakpoint again. Remember you can still Step after breakpoints as well.

- 11 -
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

1.10 Procedure:
1. Connect the board to the PC.
2. Locate/Assemble your program PracticeLab.s
3. Download (Load) the program to your Tiva board and run without breakpoints.
4. Examine memory to verify the functionality of the program.
5. If your program does not function as expected, see if you can identify the error by
tracing the program. Fix the error and repeat steps 2 – 5 until your program
functions as expected.

6. Now, step through your program and watch registers and memory locations
change as expected.

7. Now, instead of just stepping your program, apply a few breakpoints and
continuing program execution using Stepping and Run commands.

Call the lab TA and repeat steps 6-7 in their presence, without reading the directions.
When you can do this without looking at the instructions, the lab TA will check you off.
Complete understanding of this lab will make future labs easier. Review any parts of this
lab you are not sure about.

1.11 More Practice with μVision: Loading and Running programs

Because you will be using the μVision tools extensively throughout the course and
proficiency in these tools will be key, it is highly recommended that you practice loading
source programs, editing them, assembling them, building them and debugging them.
You will find several sample programs accessible on the ECE 251 Lab Web Page by
clicking on Sample Programs under Lab 1. If you are having difficulty with the
PracticeLab above, your TA may require that you complete one or more of the Sample
Programs.

1.12 Questions:

1. What are the sizes and names of the general registers on the board?

2. How does the reset button on the board affect the contents of RAM? How does it
affect the contents of the registers? How is ROM affected?

3. How are single stepping and breakpoints used to debug a program? Why might
you use breakpoints rather than single stepping?

- 12 -
Lab 1 Rev. 1.4 Software Setup & Assembly Programs

1.13 Lab Report:

The lab report is due at the beginning of the next lab. For the lab write up, include
following,
1. A copy of your working .s files for the program.
2. A brief discussion of the objectives of the lab and the procedures performed in the
lab.
3. Answers to any questions in the discussion, procedure or question sections of the
lab.

- 13 -

You might also like