Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

EEET2096 Laboratory 1:

Introduction to the Keil Development


Environment and Assembly Program Debugging
Objectives:

1. To familiarize with the Keil Development Tools, MicroVision


2. To learn how to debug an ARM assembly program in MicroVision
3. To understand the basic categories of ARM instructions with aid of the simulator built
in MicroVision,

Students are required to understand, assemble and debug an assembly program provided.
Students learn to
• Build a project
• Configure the compiler/assembler to match the microcontroller resources
• Create and add a program file to the project
• Debug the program
• Execute the program
• With help of the debugger, understand the ARM instructions.

Nominally this laboratory should be performed in two sessions.

References

1. MicroVision V5 Help Manual


2. Chapters 2. 3. and 14 “Embedded Microprocessor Systems” by J.Kneen

Introduction

Microcontrollers consist of a number of registers that store data plus a number of circuit
blocks that will act on any data presented. For example the ARM microcontroller contains 8
low level registers r0 through r7. Data from any two of these registers may be added together
using the arithmetic logic unit (ALU). The result may then be stored in any of the 8 registers.
The operations are orchestrated using control circuitry that enables the data to the required
registers/components.
Consider the circuit shown in the above figure. With the ARM microcontroller, if the control
circuit is given the machine code for add r2,r1,r0 it will generate control signals that will
perform the following operations:

(i) Enable the contents of register 0 onto one of the data buses
(ii) Enable the contents of register 1 onto the other data buse
(iii) Enable the contents of the two data buses onto the ALU inputs
(iv) Configure the arithmetic logic unit (ALU) to add its inputs
(v) Enable the results of the operation onto the result bus
(vi) Command the register 2 to read the data on the result bus.

Instructions of the form add r2,r1,r0 are known as assembly language instructions. Each
assembly instruction has a corresponding binary machine code. In the case of Cortex-M4, it is
either 32 or 16 bits. People are not good on fiddling with binary machine codes directly.
Assembly instructions are much easier to understand. High level languages are even easier.
But assembly languages are directly mapped to machine codes. Many assembly language
instructions can be combined in sequence to form a program to perform the desired actions.
An assembler/compiler is to translate an assembly/high level language program to machine
codes so that the microcontroller can understand and execute. It is important to understand
the operations of a microcontroller at the assembly/machine level. In this laboratory you are
required to explore a small introductory assembly program provided to familiarize with
MicroVision IDE and assembly language debugging.

Preliminary
Each laboratory contains some preliminary questions. These will involve reading part of the
references and answering questions. The objective is to have students prepare some material
that will assist in performing the laboratory procedure. These questions do not involve
copying quantities of information from references but rather a summary to demonstrate your
understanding.

.
1. Draw up the basic memory map or blocks for the ARM Cortem-M4 processor
2. Sketch the basic register map for the ARM processor.
3. Explain the operation of the ARM processor following reset.
4. List at least ten different instructions on data transmission, arithmetic operation and
flow control. Provide explanations to these instructions.

Procedure
1. Download the MicroVision ( MDK-ARM Version 5.29)
from the Keil website https://www.keil.com/demo/eval/arm.htm and install it on your
PC. You can run MicroVision from your own PC or the PC in the lab.

2. Open and configure the Keil development environment as described in Appendix B


The Keil start up code is not required. Save the project as Lab01. Create the sample
program Lab01.s given in Appendix A. This program has six sections which perform
the following operations:

1) Load some registers with memory addresses


2) Write data to memory from registers
3) Read data to registers from memory
4) Copy an array from one location to another in memory
5) Add up all the elements in an array and store the result
6) Loop forever.

3. Build the project and debug the program in the simulator step by step.
4. For the codes in section 1, record the contents of r1~r3 after the codes are executed.
Open three memory windows to observe the contents at these memory locations.
R1=
R2=
R3=
R1-> memory (8 bytes):
R2-> memory ( 8 bytes):
R3-> memory ( 8 bytes):

5. For the codes in section 2, record the content of r0, 4 memory units pointed by r2, 4
memory units pointed by r3.

R0 =
R2-> memory ( 4 bytes):
R3-> memory ( 4 bytes):

Understand how 32 bits data are arranged in memory.


6. For the codes in section 3, record contents of R0, R4 and R5 after the codes are
executed.

R0=

R4=

R5=

Understand the differences the extra ‘b’ makes on data transmission/

7. For the codes in section 4 , with help of the debugger, fill the following table

Assembly Address Machine code Explanation


ldr r1, =Tbe1

mov r0, #0x8

Loop1 Ldrb r3, [r1]

strb r3, [r2]

add r1, r1, #0x1

add r2, r2, #0x1

subs r0, r0, #0x1

bne Loop1

Record the two arrays before the codes are executed

Array 1 ( 8 bytes):

Array 2 ( 8 bytes):

Record the two arrays after the codes are executed

Array 1 ( 8 bytes):

Array 2 ( 8 bytes):

Think about the meaning of ‘s’ in the instruction “subs r0, r0, #0x1”. What will happen if it is
replaced with “sub r0, r0, #0x1”

8. For codes in section 5, record the sum of the array1 after the codes are executed.

R2=
Array 3 ( 4 bytes):

Can “strb r2, [r3]” instead of “str r2, [r3]” be used to save the sum? Why or why not?
Questions

The answers to these questions should be included as part of the report. These questions are
related to your understanding the sample program given, your debugging and observation of
the program execution. As the program can be debugged in the simulator, you can can carry
the debugging in the laboratory and at home.

1. What do the pseudo instructions (directives) EQU, DCB and DCD do?
2. Document how the assembler handled the LDR r3,=Tbe3 instruction. Then explain in
general terms the operation of the LDR instructions.
3. Explain the microcontroller status flags and the operation of the conditional branch
instructions.
4. List at least 6 conditional branch instructions and provide explanations.
5. If Array 1 is treated as a 32 bits data array, it has only two elements with 32 bits(ie 4
bytes). Modify the section 5, to perform a summing up of Array 1 and save the result
at the same location. [Note: we assume that the sum is within the range of 32 bits).
You can use a calculator to verify if your program does the correct job.

Report

The report should be more than a statement “we did it!” As well as summarising all the
findings and answers to all questions students should be prepared to add one or two
additional paragraphs to demonstrate that they have thought over and above just the
material given in the laboratory. Students will not lose marks through elaborating any
difficulties they encounter performing this laboratory. (This is part of quality control where
customers/users provide feedback – well considered and written feedback can be used to
demonstrate a thorough understanding of the material of this laboratory). Note students are
NOT required to demonstrate that they can copy material from references. Any material
copied should be referenced and comments explaining its relevance to the laboratory.
Appendix A: Lab1 Assembly Program.
; "Lab1.s" by J Wang 2020

; This assembly program is designed to let you get familiar with

; 1. MicroVision IDE environment ( view memory, register, CC, machine code, step through)

; 2. Assembly program ( directives, instructions, data areas)

; 3. basic ARM Cortex-M4 instructions ( data transter, arithmetic, memory access)

; 4. How to construct a loop

; 5. Machine cycle

; 6. Data arrangement in memory

Number1 EQU 0x87654321

AREA RESET, DATA, READONLY

EXPORT __Vectors

__Vectors

DCD 0

DCD Reset_Handler

AREA |.text|, CODE, READONLY

EXPORT Reset_Handler

Reset_Handler PROC

; *** Section1 : Set r1,r2 and r3 pointing to three memory locations ***

ldr r1, =Tbe1

ldr r2, =Tbe2

ldr r3, =Tbe3

; Section2: Check how data are stored in memory

ldr r0, =Number1 ; r0 = Number1. verify what is loaded in r0

str r0, [r2] ; [r2]= r0, 32 bits.

strb r0, [r3] ; [r3]=r0 8 bits

; Section3: retrieve data from memory


ldr r0, [r1] ; r0 = [r1] 32 bits

ldr r4, [r2] ; r4= [r2] 32 bits

ldrb r5, [r2] ; r5 = [r2] 8 bits

; Section 4: copy an data array from one section to another in memory

ldr r1, =Tbe1; r1 points to Tbe1 which is the beginning address of Array1

ldr r2, =Tbe2 ; r2 points to Tbe1 which is the beginning address of Array2

mov r0, #0x8; set r0 as a counter

Loop1 ldrb r3, [r1] ; load one byte to r3 from Array1

strb r3, [r2] ; Copy one byte from r3 to Array2

add r1, r1, #0x1 ; Increase r1 by 1 pointing to next byte

add r2, r2, #0x1 ; Increase r2 by 1 pointing to next byte

subs r0, r0, #0x1 ; decrease the counter by 1

bne Loop1 ; if r0 is not zero, loop back to Loop1

; Section 5: Add all the elements in an array

ldr r1, =Tbe1; r1 points to Tbe1 which is the beginning address of Array1

ldr r3, =Tbe3 ; r3 ponits to Tbe3 which is used to store the sum

mov r0, #0x8; set r0 as a counter

mov r2, #0x00; r2 will be used as an accumulator, initialized to zero

Loop2 ldrb r4, [r1]; r4=[r1], 8 bits

add r2, r2, r4 ; r2= r2 + r4

add r1, r1, #0x1 ; increase r1 by 1, pointing to next byte

subs r0, r0, #0x1; decrease r0 by 1

bne Loop2

str r2, [r3] ; store the sum at the memory location pointed by r3

; Section 6 : dead loop

Loop3 b Loop3 ; dead loop

ENDP

AREA Table1, DATA, READONLY


Tbe1 DCB 0x12,0x13,0x14,0x15,0x16,0x17,0x18 ; Array 1

AREA Tables, DATA, READWRITE

Tbe2 SPACE 0x8 ; Array 2

Tbe3 SPACE 0x8 ; Array 3

END
Appendix B: MicroVision 5.29 Configuration for Cortex-M4 Development Board
A new Cortex-M4 Development Board is designed for this course. This boards will be used in the
laboratory practice. Students can purchase the boards to use at home. More detailed information will
be released once the boards are available for purchase.

Figure 2 Cortex-m4 Development Board

The simulator is to be used for Lab1. The configuration is for this development board as shown in the
following.

You might also like