Professional Documents
Culture Documents
Lab 1 - Start - Assembly
Lab 1 - Start - Assembly
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.
References
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.
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):
R0=
R4=
R5=
7. For the codes in section 4 , with help of the debugger, fill the following table
bne Loop1
Array 1 ( 8 bytes):
Array 2 ( 8 bytes):
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
; 1. MicroVision IDE environment ( view memory, register, CC, machine code, step through)
; 5. Machine cycle
EXPORT __Vectors
__Vectors
DCD 0
DCD Reset_Handler
EXPORT Reset_Handler
Reset_Handler PROC
; *** Section1 : Set r1,r2 and r3 pointing to three memory locations ***
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
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
bne Loop2
str r2, [r3] ; store the sum at the memory location pointed by r3
ENDP
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.
The simulator is to be used for Lab1. The configuration is for this development board as shown in the
following.