0% found this document useful (0 votes)
4 views15 pages

Lab3_Exercise01-Assembly (2)

Uploaded by

luongoc130603
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views15 pages

Lab3_Exercise01-Assembly (2)

Uploaded by

luongoc130603
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Laboratory Exercise 3: Assembly Language Programming

The objective of this exercise is familiarization with the Keil MDK-ARM programming
environment (creating projects, entering assembly language programs, assembling, simulating, and
debugging). An assembly language program is entered and simulated in Keil MDK-ARM, and
various questions about the environment explore important features of assembly language
programming with Keil MDK-ARM.

Prelab Work

(No prelab work is required for this exercise.)

Procedure

Assembling Code Using Keil MDK-ARM


1. Start Keil MDK-ARM’s µVision® IDE (integraged development environment).
Click the start menu icon at the left of the Windows task bar, then select All
Programs, and finally select Keil µVision5. The Keil µVision IDE will then open,
as shown in Figure 1.








Figure 1. Opening Keil MDK-ARM’s µVision IDE

Page 1 of 15
Lab Exercise 1: Tutorial Page 2 of 15

2. Create a new project.


From the Keil µVision IDE main menu, select ProjectNew µVision Project …
to launch the Create New Project window, shown in Figure 2, which is a standard
Windows Explorer-style file save dialog box.

Figure 2. Create New Project Window


a. Browse to a location where you want to keep project files
 Suggestion: somewhere in your Documents folder.
 Caution:.
b. Use New Folder to create a folder for your project, and name it Exercise01.
c. In the File name field at the bottom, type Exercise01.
d. Press the Save button to create your project. Keil µVision IDE will then launch
the Select Device for Target ‘Target 1' … window, shown in Figure 3.

Figure 3. Select Device for Target Window


e. To set the target as the Freescale MKL46Z256VLL4 microcontroller on the Freescale
KL46Z Freedom Board, in the left pane below the Search field, successively click
next to each of the following categories to select MKL46Z256xxx4, as shown in Figure 3:
Freescale SemiconductorKLxx Series  KL4x  MKL46Z256xxx4.
0306-250 20141 Lab Exercise 1: Tutorial Page 3 of 15

f. Click the OK button to finish creating the Exercise01 project.


g. If Keil µVision IDE launches the Manage Run-Time Environment window, shown
in Figure 4, where Keil ARM-MDK software components can be added to the
project, click the OK button to continue without adding any software components.
h. The project then opens in Keil µVision IDE, as shown in Figure 5.

Figure 4. Manage Run-Time Environment


Lab Exercise 1: Tutorial Page 4 of 15

Figure 5. Adding New Item to Project Open in µVision IDE


3. Add an assembly source file to the project.
a. In the Project pane along the top left side of the Vision IDE window, click
next to Target to show Source Group 1, as shown in Figure 5.
b. Right-click on Source Group 1, and in the resulting drop-down menu select Add
New Item to Group 'Source Group 1' … to open the Add New Item to Group 'Source
Group 1' window, as shown in Figure 6.
0306-250 20141 Lab Exercise 1: Tutorial Page 5 of 15

Figure 6. Adding New Assembly Source File to Project


c. In the top left pane, click Asm File (.s) to change the Type field to ASM File
(.s).
d. In the Name field, type Exercise01.
e. Click the Add button to create Exercise01.s. Exercise01.s is then opened in the
editor pane on the right side of the Vision IDE window, as shown in Figure 7.

Figure 7. Source File in Vision IDE Editor


Lab Exercise 1: Tutorial Page 6 of 15

4. Enter assembly source code.


a. Type the code shown in Figure 8 into the Exercise01.s editor pane. Other than
changing the lines in the comment header block where indicated, type the text
exactly as shown—including line spacing and indentation.

Figure 8. Exercise 1 Assembly Code


0306-250 20141 Lab Exercise 1: Tutorial Page 7 of 15

b. Click the Save icon (or select menu FileSave) to save the program.
5. Edit target options for the project.
 Note: during assembly, a listing file will be produced, which has columns
added to the left of the assembly source. Because the number of characters on a
line increases in the listing file, it is best viewed in landscape mode. The
settings below produce a listing file suitable for printing in landscape mode
from a text editor, such as Notepad++.
a. On the second toolbar row, click the Target Options icon (or select menu
ProjectOptions for Target 'Target1' …) to launch the Options for
Target 'Target1' window, as shown in Figure 9.

Figure 9. Setting Options for Listing File


b. Click on the Listing tab along the top of the window to get the options shown in
Figure 9.
c. Change Page Width to 120.
d. Change Page Length to 49.
e. Click the OK to save the options and close the options window.
6. Assemble Exercise01.s.
a. On the second toolbar row, click the Translate icon (or select menu
ProjectTranslate <path>\Exercise01.s) to assemble the source code
b. In the Build Output pane at the bottom of the window, the text assembling
Exercise01.s will appear.
c. If the next text output is "Exercise01.s" - 0 Error(s), 0 Warning(s), as in Figure 10,
assembly was successful. Otherwise, any errors must be corrected before assembly
can complete. Successful assembly produces an object file and a listing file.
Lab Exercise 1: Tutorial Page 8 of 15

Figure 10. Project after Assembly


7. View the listing file: Exercise01.lst.
a. From the Vision IDE main menu, select FileOpen to launch the Open
File window, as shown in Figure 11.

Figure 11. Opening Listing File


b. Use the pull-down menu to the right of the File name field to select Listing (*.lst;
*.m*; *.cod).
c. Select Exercise01.lst, and then click Open.
0306-250 20141 Lab Exercise 1: Tutorial Page 9 of 15

Understanding Assembler Listing Files


8. Examine the listing file and answer the following questions. Note that listing file has
four columns: 1) the assembly source code line number, 2) the corresponding memory
offset within an AREA, 3) the memory contents in hex for that address, and
4) the original assembly source code.
Q1. What are the four words (in hexadecimal) assembled for ConstData by the
assembler directive DCD?

Q2. In ConstData, why does DCD produce different word values for 0x00000010
and 10?

Q3. How much storage and what memory contents are shown for VarData in the
listing file?

Q4. At what offset within the RAM AREA does VarData begin?

Q5. What opcode (in hexadecimal) is generated for the NOP instruction?
Q6. How many bytes of machine code are generated for each assembly instruction?

Q7. Do assembly language program labels generate any code bytes? Explain why.

Q8. According to the symbol table at the end of the listing file, how many times is
SP_INIT used (not counting its definition).

9. Show your answers to your lab instructor for signature.


Lab Exercise 1: Tutorial Page 10 of 15

Building Projects Code Using Keil MDK-ARM


10. Building a project consists of linking all of the assembled code. In the previous
assembly step, the assembler produced an object file. Now that object file must be
linked using target parameters (specified in the project’s target options) to produce an
executable file. The executable file consists of memory contents, (i.e., images), to be
loaded into specific areas of memory, (as specified in the project’s target options).
a. On the second toolbar row, click the Build icon (or select menu ProjectBuild
target) to build the project.
 Note: if the source file had not already been assembled, the build
operation would first assemble the source.
b. In the Build Output pane at the bottom of the window, the text Build target 'Target
1' will appear followed on the next line by linking . . . .
c. If the final line of text shows 0 Error(s), 0 Warning(s) appears as in Figure 10, the
build was successful. Otherwise, any errors must be corrected before an executable
file is produced.
11. View the project’s target options for mapping the project’s AREAs to memory.
a. On the second toolbar row, click the Target Options icon (or select menu
ProjectOptions for Target 'Target1' …) to launch the Options for
Target 'Target1' window, as shown in Figure 12.

Figure 12. Viewing Options for Target Microcontroller


b. If the Target tab is not selected, click on the Target tab along the top of the window
to get the options shown in Figure 12.
c. Answer the following questions about the target memory address options.
Q9. Where in memory would you expect code and constants?

Q10. Where in memory would you expect variables?

d. Click on the Linker tab along the top of the window to get the options shown in
Figure 13.
0306-250 20141 Lab Exercise 1: Tutorial Page 11 of 15

Figure 13. Viewing Options for Linker


 Note: the linker by default uses the memory specified in the options on the
Target tab.
11. View the listing file: Exercise01.map.
a. From the Vision IDE main menu, select FileOpen to launch the Open
File window, as shown in Figure 14.

Figure 14. Opening Map File


b. Use the pull-down menu to the right of the File name field to select Listing (*.lst;
*.m*; *.cod).
c. Select Exercise01.map, and then click Open.
12. Examine the linker map file and answer the following questions.
Image Symbol Table
Q11. Where do the variables VarData start in memory?
Q12. How large (in bytes) is the MyCode area.?
Q13. What number base is used for Size of the symbols?
Lab Exercise 1: Tutorial Page 12 of 15

Q14. How much of RAM is used?


Memory Map of the image
Q15. What is the starting address of the program?
Q16. What number base is used for Size of the regions?
Q17. How many bytes are used for the constants in MyConst?
Q18. What is the starting address of RAM?
13. Show your answers to your lab instructor for signature.

Simulation Using µVision Debug


14. Select simulation for debug.
a. On the second toolbar row, click the Target Options icon (or select menu
ProjectOptions for Target 'Target1' …) to launch the Options for
Target 'Target1' window, as shown in Figure 15.

Figure 15. Viewing Options for µVision Debug


b. Click on the Debug tab along the top of the window to get the options shown in
Figure 15.
c. At the top left, under the tabs, select Use Simulator.
d. Click the OK button to close the options windws and return to the µVision IDE.
15. To start a debug session, on the top toolbar row toward the right of the window, click
the Start/Stop Debug Session icon (or select menu Debug Start/Stop Debug
Session). window, as shown in Figure 16.
 Note: Whereas the lab workstations have a professional license for the
µVision software, if you download the software for your personal use, you
will not have a license, and you will get a pop-up window at this point, that
warns the debugger will run in evaluation mode with a code size limit of 32
KB. Just click OK in the pop-up window, and then the debug session will
start.
0306-250 20141 Lab Exercise 1: Tutorial Page 13 of 15

Figure 16. Viewing Options for µVision Debug


16. Explore the debug simulator.
a. The Register pane at the top left shows the current contents/status of all registers.
 Expand xPSR to see the condition codes: N, Z, C, and V.
b. The Disassembly pane at the top right shows the machine instructions in code
memory.
 The actual machine instruction(s) used will show for any pseudo-
instructions the programmer used in the assembly source code.
c. The source pane at the center right shows the programmer’s original assembly code.
d. The data pane at the bottom right can be toggled between Call Stack + Locals and
memory. For now select Memory 1 and enter 0x20000100 in the Addresss field to
view the VarData variables.shows the current value of constant and variable data
in the program.
 Right clicking in this window gives a context menu, which can be used
to change how the contents are displayed.
e. The Command pane is at the bottom left
17. Run the program in the simulator.
a. In the source pane, position the mouse pointer over the first nop instruction, right
click, and select Insert/Remove Breakpoint. Setting a breakpoint here will cause
program execution to stop before executing this instruction.
Lab Exercise 1: Tutorial Page 14 of 15

b. On the second toolbar row, either click the Run button, select menu
DebugRun, or press F5.
c. On the second toolbar row, either click the Step button, select menu
DebugStep, or press F11. Look at Register pane to see the effects of executing
the instruction.
18. Start a new debug session and track all changes to the registers in Table 1.
a. On the top toolbar row toward the right of the window, click the Start/Stop
Debug Session icon to stop the debug session and return to the µVision IDE.
b. From the µVision IDE click the Start/Stop Debug Session icon to start a new
debug session.
c. Step through the program instructions to record each instruction’s register
changes in Table 1. Note that xPSR will have to be expanded to show N, Z, C, and
V. If an instruction does not change the value of a register, you may leave its cell in
the table blank rather than rewriting the same value.
 If an instruction does not change the value of a register, you may leave its
celin the table blank rather than rewriting the same value.
 Caution: The double-line border before the last NOP instruction in the
tableindicates that other instructions are executed before this NOP. Step
through the instructions not listed in the table and record the results after this
NOP has executed for the first time.
19. Show your completed table to your lab instructor for signature.

Report

(No report is due for this exercise.)


0306-250 20141 Lab Exercise 1: Tutorial Page 15 of 15

Table 1. Register contents(in hex) after each program instruction


Registers
xPSR
PC
Instruction R0 R1 R2 R3 R15 N Z C V

Initial debug state 00000000 00000000 00000000 00000000 00000008 0 0 0 0

LDR R0,=ICER

MOVS R1,#0

MVNS R1,R1

STR R1,[R0,#0]

LDR R0,=SP_INIT

MOV SP,R0

NOP

LDR R2,=ConstData

MOVS R3,#0x02

LDR R1,[R2,#0]

LDR R0,=VarData

STR R1,[R0,#0]

LDR R1,[R2,#4]

STR R1,[R0,#4]

ADDS R2,#8

SUBS R3,#1

BNE Loop

LDR R1,[R2,#0]

LDR R0,=VarData

STR R1,[R0,#0]

LDR R1,[R2,#4]

STR R1,[R0,#4]

ADDS R2,#8

NOP

You might also like