Arm&Es Module2
Arm&Es Module2
Arm&Es Module2
MODULE 2
ARM Cortex M3 Instruction Sets and Programming: Assembly basics,
Instruction list and description, Useful instructions, Memory mapping, Bus
interfaces and CMSIS, Assembly and C language Programming (Text 1: Ch-
4, Ch-5, Ch-10 (10.1, 10.2, 10.3, 10.5 only)
Assembler: A program for converting instructions written in low level symbolic code into
machine code.
Interpreter: A program that can analyze and execute a program line by line.
Compiler: A program that converts instructions into a machine code or lower level form so
that they can be read and executed by a computer.
Mnemonic: A mnemonic is a term, symbol or name used to define or specify a computing
function.
Opcodes: In computing, an opcode is the portion of a machine language instruction that
specifies the operation to be performed.
Operands: In computers, an operand is the part of a computer instruction that
specifies data that is to be operating on or manipulated and by extension, the data itself.
Basically, a computer instruction describes an operation (add, subtract, and so forth) and the
operand or operands on which the operation is to be performed.
Instructions: An instruction is an order given to a computer processor by a
computer program.
Operating modes
ARM has 7 basic Operating modes
1. User mode: Unprivileged mode under which most task run.
2. FIQ (Fast Interrupt Request): Entered when a high priority interrupt is raised.
3. IRQ (Interrupt Request): Entered when low priority interrupt is raised.
4. Supervisor: Entered on reset and when a software interrupt instruction is executed.
5. Abort: Entered upon memory access violations.
6. Undef: Used to handle undefined instructions.
7. System: It is a privileged mode which uses same registers as user mode.
Bank Registers
There are 37 registers in the register file, which are of 32 bit long.
There are upto 18 active registers:- 16 data registers (R0 – R15), CPSR (Current Program
Status Register), SPSR (Saved Program Status Register).
Out of 37 registers, 20 are hidden from a program at different times. These registers are
called banked registers.
They are available only when processor is in a particular mode.
Ex: Abort mode has registers R13_ab, R14_ab and SPSR_ab.
Every processor mode except user mode can change mode by writing directly to the mode
bits of the CPSR.
All processor modes except system modes have a set of associated banked resisters that are a
subset of main 16 registers.
If processor mode is changed, the banked registers from the new mode will replace an
existing register.
Ex: When the processor is in interrupt request mode, the instructions that are executed still
access registers R13 and R14. However then registers are the banked registers R13_irq and
R14_irq. User mode registers R13 and R14 not affected.
Asembler Language
Basic syntax in assembler code, the following instruction formatting is commonly used:
Label opcode operand1, operand2, .. ;Comments
The label is optional.
Some of the instructions might have a label in front of them so that the address of
the instructions can be determined using the label.
Then, the op-code (the instruction) followed by number of operands. Normally, the first
operand is the destination of the operation.
The text after each semicolon (;) is a comment. These comments do not affect the program
operation, but they can make programs easier for humans to understand.
The difference between thumb instruction format and Unified Assembler Language is as
follows:
Assembler Directives
Assembler directives are instructions that direct the assembler to do something.
1). EQU( Equate) - Constants can be defined using EQU.
- Ex: NVIC_IRQ_SET EQU
0xE000E100
NVIC_IRQ_ENABLE EQU 0x1
2). DCI (Define Constant Instruction) - A number of data definition directives are
available for instruction of constants inside assembly code.
Ex: DCI is used to code an instruction if the assembler cant generate the exact instruction that
the user wants and if the user know the binary code for instruction.
DCI 0xBE00 ; Breakpoint (BKPT 0) – 16 bit instruction
DCB (Define Constant Byte) – Byte size constant values such as characters can be used
to define using DCB.
Ex: LDR R0,
=hellotxt BL
printtext
Hellotxt DCB “hello\n”, 0
DCD (Define Constant Data) – Word size constant values to define binary data in
assembler code is done through DCD.
Ex: LDR R3, = my_num
LDR R4, [R3]
my_num DCD 0x12345678H
Suffixes in instructions
Suffix Descriptions
S Update APSR flags. Ex: ADDS
EQ, NE, NT, GT Conditional execution. Ex: BEQ <label>
Double Reg indirect Register indirect LDR R0, [R1, r2, LSL
#2] with scaling indexed with scaling
1. Register Addressing
Operand is given in the CPU register of ARM (R0-R15). It can be only source, only destination
or both.
Examples Meaning
2. Immediate Addressing
Operand is directly given in the instruction, prefixed with # symbol. Can be provided in decimal
or hexadecimal, internally always stored in hexadecimal system..
Examples Meaning
AND R0, R1, #0xFF000000 ;Logically AND imm FF000000h with R1 and store
;result in R0
CMNS R0, #6400 ; Compare negate imm 6400 with R0 and update
; the N, Z, C and V flags
CMPGT SP, R7, LSL #2 ; Compare R7 data shifted left 4 times with SP if
; GT condition is met and update the N, Z, C and V flags
LDR R0, [R1, #20] R1 + 20 ; loads R0 with the word pointed at by R1+20
LDR R0, [R1, #4]! R1+4 ; loads R0 with the word pointed at by
; R1+4then update the pointer by adding 4 to R1
Execute
Decode
Fetch
Example 1 Example 2
Before Execution Before Execution
R1=0x00000000H, R0 = 0x00000004H R1 = 0x00000000H, R0 = 0x00000004H
MOV R1, R0, LSR #1 MVN R1,R0, LSR #1
After Execution After Execution
R1= 0x00000002H, R0 = 0x00000004H R1 = 0xFFFFFFFDH, R0 = 0x00000004H
MOVT (Move Top)
Syntax: MOVT{cond} Rd, #imm16
Where cond is an optional condition code. Rd is the destination register. imm16 is a 16-
bit immediate constant.
Operation: MOVT writes a 16-bit immediate value imm16 to the top halfword,
Rd[31:16] of its destination register. The write does not affect Rd[15:0].
The MOV, MOVT instruction pair enables the user to generate any 32-bit constant.
Restrictions: Rd must not be SP and must not be PC.
Condition Flags: This instruction does not change the flags.
Example
Before Execution Instruction After Execution
R3=0x12345678H MOVT R3, #0xF8CDH R3= 0xF8CD5678H
B. Memory Access Instructions
i. LDR and STR, LDM and STM, LDRD and STRD
ii. PUSH and POP
iii. MRS and MSR
Address Data
1000002A 0xABCDEF54H
R1
1000002E 0x12345678H
10000032 0x24567893H
10000036 0x88564478H
Example 1 Example 2 Example 3 Example 4
LDRB R2, [R1,#4] LDRH R2, [R1, #4] LDR R2, [R1, #4] LDRD R2, R3, [R1, #4]
Example 1 Example 2
STRB R2, [R1, #4] STRH R2, [R1,#4]
Before Execution: Before Execution:
R2=0x324565CDH R2=0x324565CDH
Operation:
Operation:
mem8[R1+4]=R2
Mem16[R1+4]= R2
After Execution:
After Execution:
Address Data
Address Data
1000002A 0xABCDEF54H
R1 1000002A 0xABCDEF54H
1000002E 0x12345678H R1 1000002E 0x12345678H
10000032 0x245678CDH 10000032 0x245665CDH
10000036 0x88564478H 10000036 0x88564478H
Example 3 Example 4
STR R2, [R1, #4]
STRD R2, R3, [R1, #4]
Before Execution:
Before Execution:
R2=0x324565CDH
R2=0x324565CDH, R3=0x88564478H
Operation: Operation:
Mem32[R1+4]= R2 [R1+4] = R2, [R1+8] = R3
After Execution: After Execution
Address Data
1000002A 0xABCDEF54H Address Data
R1 1000002A 0xABCDEF54H
1000002E 0x12345678H
R1 1000002E 0xDE285349H
10000032 0x324565CDH
10000036 0x88564478H 10000032 0x324565CDH
10000036 0x88564478H
1. Write a Cortex M3 program to move a 32 bit data from a location 2000H(data =
0xFFH) into 3000H and 4000H.
area ex1, code, readonly
entry
start
mov r5,#0xFFH ; r5 = 0xFFH
ldr r0, =0x2000H ; r0 points to the address 2000H
ldr r1, =0x3000H ; r1 points to the address 3000H
ldr r2, =0x4000H ; r2 points to the address 4000H
ldr r5, [r0] ; mem[2000H] = 0xFFH
str r5, [r1] ; mem[3000H] = 0xFFH
str r5, [r2] ; mem[4000H] =
0xFFH end
Example Description
LDMIA Rd!, Read multiple words from memory location specified by Rd. Address
<Reg list> increments(IA) after each transfer(16 bit Thumb instruction)
STMIA Rd!, Store multiple words to memory location specified by Rd. Address
<Reg list> increments(IA) after each transfer(16 bit Thumb instruction)
LDMIA.W Read multiple words from memory location specified by Rd. Address
Rd(!), <Reg list> increments(IA) after each read(.W specifies that it is a 32 bit Thumb2
instruction)
LDMDB.W Read multiple words from memory location specified by Rd. Address
Rd(!), <Reg list> decrements before(DB) each read(.W specifies that it is a 32 bit
Thumb2 instruction)
STMIA.W Rd(!), Write multiple words to memory location specified by Rd. Address
<Reg list> increment after each read (.W specifies that it is a 32 bit Thumb2
instruction)
STMDB.W Write multiple words to memory location specified by Rd. Address
Rd(!), <Reg list> decrement before each read (.W specifies that it is a 32 bit Thumb2
instruction)
Example 1: Write an ALP to move 4 32 bit data from source location to the destination location
using LDMIA and STMIA instructions.
Address Data
10000032 0xABCDEF54H
1000002E 0x12345678H
R1 1000002A 0x24567893H
10000026 0x88564478H
Address Data
10000098 0xABCDEF54H
10000094 0x12345678H
R1 10000090 0x24567893H
1000008C 0x88564478H
POP {R0-R3, PC} ; restore registers and return in this case, instead of
popping the LR register back and then branching to the address in LR, we POP the address
value directly in the program counter.
iii). MSR and MRS Instructions
MSR and MRS instructions are used to access the special registers in Cortex M3. The special
registers are listed as shown in Table 2.
Symbol Description
IPSR Interrupt status register
EPSR Execution status register (read as zero)
APSR Flags from previous operation
IEPSR A composite of IPSR and EPSR
IAPSR A composite of IPSR and APSR
EAPSR A composite of EPSR and APSR
PSR A composite of APSR, EPSR and IPSR
MSP Main Stack Pointer
PSP Process Stack Pointer
PRIMASK Normal exception mask register
BASEPRI Normal exception priority mask register
BASEPRI_MAX Same as normal exception priority mask register, with conditional
write (new priority level must be higher than the old level).
FAULTMASK Fault exception mask register (also disables normal interrupt)
CONTROL Control register
3. Arithmetic instructions
Instruction Operation Description
ADD Rd, Rn, Rm Rd = Rn + Rm ADD operation
ADD Rd, Rd, Rm Rd = Rd + Rm
ADD Rd, #immd Rd = Rd + #immd
ADD Rd, Rn. #immd Rd = Rn + #immd
ADC Rd, Rn, Rm Rd = Rn+ Rm + carry ADD with carry
ADC Rd, Rd, Rm Rd = Rd + Rm + carry
ADC Rd, #immd Rd = Rd + #immd + carry
ADDW Rd, Rn, #immd Rd = Rn + #immd Add register with 12 bit
immediate value
SUB Rd, Rn, Rm Rd = Rn – Rm SUBTRACT
SUB Rd, #immd Rd = Rd - #immd
SUB Rd, Rn, #immd Rd = Rn - #immd
SBC Rd, Rm Rd = Rd – Rm - borrow SUBTRACT with borrow (not
SBC.W Rd, Rm,#immd Rd = Rm - #immd - borrow carry)
SBC.W Rd, Rn, Rm Rd = Rn - Rm - borrow
RSB.W Rd, Rn, #immd Rd = #immd – Rn Reverse subtract
RSB.W Rd, Rn, Rm Rd = Rm – Rn
MUL Rd, Rm Rd = Rd * Rm Multiply
MUL.W Rd, Rn, Rm Rd = Rn * Rm
UDIV Rd, Rn, Rm Rd = Rn/Rm Unsigned and Signed divide
SDIV Rd, Rn, Rm Rd = Rn/Rm
2. ADD R3,R3,R1
Before Execution: R1=0x12345678H, R3=0x23657894H
Operation: R3=R3+R1
After execution: R3=0x3599CF0CH
Example 7
RSB.W R2, R1, R0
Before Execution
R1=0x00000022H, R0=0x00000049H
Operation: R2 = R0 – R1
After Execution
R2=0x00000027H
iii). MULTIPLICATION INSTRUCTIONS
Table: 32 bit multiply instructions
Instruction Operation
SMULL RdLo, RdHi, Rn, Rm 32 bit multiply instructions for
Operation: {RdHi, RdLo} = Rn * Rm signed values
SMLAL RdLo, RdHi, Rn, Rm
Operation: {RdHi, RdLo} + = Rn * Rm
UMULL RdLo, RdHi, Rn, Rm 32 bit multiply instructions for
Operation: {RdHi, RdLo} = Rn * Rm unsigned values
UMLAL RdLo, RdHi, Rn, Rm
Operation: {RdHi, RdLo} + = Rn * Rm
MLA R10,R1, R2, R5 Multiply with Accumulator
Operation: R10 = (R1 * R2) + R5
MLS R4, R5, R6, R7 Multiply with Subtract
Operation: R4 = R7 – (R5 * R6)
Example 3: Write an ALP to perform Multiply with accumulate and multiply with subtract
operations.
area pgm2, code, readonly
data1 dcd 0x00009678H
data2 dcd 0x000016EFH
data3 dcd 0x00000003H
export main
main
ldr r0, =data1 ; r0 points to the address of data 1
ldr r1, =data2 ; r1 points to the address of data 2
ldr r4, =data3 ; r4 points to the address of data 3
ldr r2, [r0] ; r2 = 0x00009678H
ldr r3, [r1] ; r3 = 0x000016EFH
ldr r6, [r4] ; r4 = 0x00000003H
mla r5,r2,r3,r6 ; r5 = 0x0D7ACA0BH
mls r7, r2,r3,r6 ; r7=0xF28535FBH
end
4. Logical instructions
After Execution: R1 = 0011 0100 0010 0101 0011 0011 1011 1101
R1 = 0x342533BDH, R2 = 0xDCBC4533H
3 RBIT Rd, Rn Reverse every bit of source register Reverse Bit
and store it in destination register
Example: RBIT R1, R2
Before Execution: R1 = 0x12345678H , R2 = 0x10018008H
5 UBFX.W <Rd>, <Rn>, Extracts a bit field from a register starting Unsigned Bit
<#LSB>. <#width> from any location (specified by LSB) Field Extraction
with any width (specified by #width),
zero
extends it, puts in destination register
Example: LDR R0, =0x5678ABCDH
UBFX.W R1, R0, #4, #8
R1 = 0x000000BCH
6 SBFX.W <Rd>, <Rn>, SBFX extracts a bit field, but its sign Signed Bit Field
<#LSB>. <#width> extends it before putting it in a Extraction
destination register.
Example: LDR R0,=0x5678ABCDH
SBFX.W R1,R0,#4, #8
R1 = 0xFFFFFFBCHs
Examples
B loop A ; Branch to loop A
BLE down ; Conditionally branch to label down
B.W target ; Branch to target within 16MB
range BEQ target; Conditionally branch to target
BL func ; Branch with link (Call) to function func, return address stored in
LR BX LR ; Return from function call
BXNE R0 ; Conditionally branch to address stored in R0
BLX R0 ; Branch with link and exchange (Call) to a address stored in R0.
Operation
Use the CBZ or CBNZ instructions to avoid changing the condition code flags and to reduce
number of instructions.
i). CBZ Rn, label
---Does not change condition flags but is otherwise equivalent to:
CMP Rn, #0
BEQ label
ii). CBNZ Rn, label
--- Does not change condition flags but is otherwise equivalent to:
CMP Rn, #0
BNE label
Condition flags
These instructions do not change the flags.
Example
CBZ R5, target ; Forward branch if R5 is zero
CBNZ R0,target ; Forward branch if R0 is not
zero.
Example : i = 5;
while (i != 0 )
{
func1(); call a
function i−−;
}
This can be compiled into the following:
MOV R0, #5 ; Set loop counter
loop1 CBZ R0, loop1exit ; if loop counter = 0 then exit the loop
BL func1 ; call a function
SUB R0, #1 ; loop counter decrement
B loop1 ; next loop
loop1exit
The usage of CBNZ is similar to CBZ, apart from the fact that the branch is taken if the
Z flag is not set (result is not zero).
The APSR value is not affected by the CBZ and CBNZ instructions.
10. Memory Barrier Instructions
The Cortex-M3 supports a number of barrier instructions.
These instructions are needed as memory systems get more and more complex.
In some cases, if memory barrier instructions are not used, race conditions could
occur.
The following are the three barrier instructions in the Cortex-M3
• DMB • DSB • ISB.
These instructions are described below.
The memory barrier instructions can be accessed in C using Cortex Microcontroller
Software Interface Standard (CMSIS) compliant device driver library as follows:
void DMB(void); // Data Memory Barrier.
void DSB(void); // Data Synchronization Barrier.
void ISB(void); // Instruction Synchronization Barrier.
DMB
It is very useful for multi-processor systems.
For example, tasks running on separate processors might use shared memory
to communicate with each other.
In these environments, the order of memory accesses to the shared memory can be
very important.
DMB instructions can be inserted between accesses to the shared memory to ensure
that the memory access sequence is exactly the same as expected.
DSB
For example, if the memory map can be switched by a hardware register, after
writing to the memory switching register you should use the DSB instruction.
Otherwise, if the write to the memory switching register is buffered and takes a
few cycles to complete, and the next instruction accesses the switched memory
region immediately, the access could be using the old memory map.
In some cases, this might result in an invalid access if the memory switching
and memory access happen at the same time.
Using DSB in this case will make sure that the write to the memory map
switching register is completed before a new instruction is executed.
The DSB and ISB instructions can be important for self-modifying code.
For example, if a program changes its own program code, the next
executed instruction should be based on the updated program.
However, since the processor is pipelined, the modified instruction location
might have already been fetched.
Using DSB and then ISB can ensure that the modified program code is fetched
again.
ISB
Architecturally, the ISB instruction should be used after updating the value of the
CONTROL register.
In the Cortex-M3 processor, this is not strictly required. But if you want to make
sure your application is portable, you should ensure an ISB instruction is used after
updating to CONTROL register.
The IT (IF-THEN) block is very useful for handling small conditional code.
It avoids branch penalties because there is no change to program flow.
It can provide a maximum of four conditionally executed instructions.
In IT instruction blocks, the first line must be the IT instruction, detailing the
choice of execution, followed by the condition it checks.
The first statement after the IT command must be TRUE-THEN- EXECUTE, which
is always written as ITxyz, where T means THEN and E means ELSE.
The second through fourth statements can be either THEN (true) or ELSE (false).
If a statement is to be executed when <cond> is false, the suffix for the instruction
must be the opposite of the condition.
For example, the opposite of EQ is NE, the opposite of GT is LE, and so on.
Ex 1: if (R1<R2) then
R2=R2−R1
R2=R2/2
else
R1=R1−R2
R1=R1/2
In assembly,
CMP R1, R2 ; If R1 < R2 (less then)
ITTEE LT ; then execute instruction 1 and 2 ; (indicated by T) ;
else execute instruction 3 and 4 ; (indicated by E)
SUBLT.W R2, R1 ; 1st instruction
LSRLT.W R2, #1 ; 2nd instruction
SUBGE.W R1, R2 ; 3rd instruction (notice the GE is ; opposite of
LT) LSRGE.W R1,#1 ; 4th instruction
If an exception occurs during the IT instruction block, the execution status of the
block will be stored in the stacked PSR (in the IT/Interrupt-Continuable Instruction
[ICI] bit field).
So, when the exception handler completes and the IT block resumes, the rest of
the instructions in the block can continue the execution correctly.
In the case of using multicycle instructions (for example, multiple load and
store) inside an IT block, if an exception takes place during the execution, the
whole instruction is abandoned and restarted after the interrupt process is
completed.
The table above gives how the disassembler generates the code from object file.
12. Saturation instructions
The Cortex-M3 supports two instructions that provide signed and unsigned
saturation operations: SSAT and USAT (for signed data type and unsigned data
type, respectively).
Saturation is commonly used in signal processing—for example, in signal
amplification. When an input signal is amplified, there is a chance that the
output will be larger than the allowed output range.
If the value is adjusted simply by removing the unused MSB, an overflowed result
will cause the signal waveform to be completely deformed as shown in figure below.
The saturation operation does not prevent the distortion of the signal, but at least
the amount of distortion is greatly reduced in the signal waveform.
Syntax
Besides the destination register, the Q-bit in the APSR can also be affected by the
result.
The Q flag is set if saturation takes place in the operation, and it can be cleared
by writing to the APSR.
For example, if a 32-bit signed value is to be saturated into a 16-bit signed value,
the following instruction can be used: SSAT.W R1, #16, R0.
This will provide a saturation feature that has the properties shown in above Figure.
For the preceding 16-bit saturation example instruction, the output values shown in the
table below can be observed.
The range of the signal with signed saturation is from 0xFFFF8000H to 0x00007FFFH.
Unsigned Saturation
If a 32-bit unsigned value is to saturate into a 16-bit unsigned value, the
following instruction can be used: USAT.W R1, #16, R0.
The figure below indicates the resultant signal with unsigned saturation.
The range of the signal with unsigned saturation is from 0x00000000H to 0x0000FFFFH.
The table below shows the status of Q bit for different values of input and output.
When the TBB instruction is executed, the current PC value is at the address labeled
as branchtable (because of the pipeline in the processor).
Similarly, for TBH instructions, it can be used as follows:
main
;;;;;;;;;;User Code Start from the next line;;;;;;;;;;;;
Sl Microprocessor Microcontroller
No.
1
Read Read Microcontroller Read Only Read Write
Only Write Memory Memory
Memory Memory (ROM)
Timer I/O Port Serial
System
Interface
Micropr Bus Serial
ocessor Interface
Sl
No RISC CISC
1 RISC stands for Reduced Instruction Set CISC stands for Complex Instruction Set
Computer. Computer.
2 CSIC processor has complex instructions that
RISC processors have simple instructions take up multiple clocks for execution. The
taking about one clock cycle. The average average clock cycle per instruction (CPI) is in
clock cycle per instruction (CPI) is 1.5 the range of 2 and 15.
3 Performance is optimized with more focus Performance is optimized with more focus on
on software hardware.
4 It has no memory unit and uses a separate It has a memory unit to implement complex
hardware to implement instructions.. instructions.
5 It has a hard-wired unit of programming. It has a microprogramming unit.
6 The instruction set is reduced i.e. it has only
a few instructions in the instruction set. The instruction set has a variety of
Many of these instructions are very different instructions that can be used for
primitive. complex operations.
7 CISC has many different addressing modes and
The instruction set has a variety of can thus be used to represent higher-level
different instructions that can be used for programming language statements more
complex operations. efficiently.
8 Complex addressing modes are synthesized CISC already supports complex addressing
using the software. modes
9 Multiple register sets are present Only has a single register set
10 They are normally not pipelined or less
RISC processors are highly pipelined pipelined
11 The complexity of RISC lies with the
compiler that executes the program The complexity lies in the micro program
12 Execution time is very less Execution time is very high
13 Code expansion can be a problem Code expansion is not a problem
Differences between Harvard architecture and Von Neumann architecture
2 It required two memories for their It required only one memory for their
instruction and data. instruction and data.
5 Processor can complete an instruction Processor needs two clock cycles to complete
in one cycle an instruction.
MODULE 2
CHAPTER 5
Text Books: Joseph Yiu, “The Definitive Guide to the ARM Cortex-
M3”, 2nd Edition, Newnes, (Elsevier), 2010.
Memory map of Cortex M3
8.4.1 Memory system features overview
External RAM
1 GB. Program execution is allowed.
External devices:
1 GB. Program execution is not allowed.
Unsigned transfer
example 1
Unsigned transfer
example 2
Unsigned transfer
example 3
UNALIGNED TRANSFER CNTD…
2. Half word size, and the address is not a multiple of 2
Unsigned transfer
example 4
Unsigned transfer
example 5
UNALIGNED TRANSFER CNTD…
Semaphores are commonly used for allocating shared resources to applications.
When a shared resource can only service one client or application processor, we
also call it Mutual Exclusion (MUTEX)
EXCLUSIVE ACCESSES CNTD…
EXCLUSIVE ACCESSES CNTD…
Used in Cortex M3
Syntax
LDREX <Rxf>, [Rn, #offset]
STREX <Rd>, <Rxf>,[Rn, #offset]
Where Rd is the return status of the exclusive write (0 = success and 1 =
failure).
ENDIAN MODE CNTD…
ENDIAN MODE CNTD…
ENDIAN MODE CNTD…
MODULE 2
CHAPTER 10
Text Books: Joseph Yiu, “The Definitive Guide to the ARM Cortex-
M3”, 2nd Edition, Newnes, (Elsevier), 2010.
Cortex M3 programming
Cortex M3 can be programmed using either
assembly language, C language, or other high-level
languages like National Instruments LabVIEW.
For most embedded applications using the Cortex-
M3 processor, the software can be written entirely in
C language.
There are of course some people who prefer to use
assembly language or a combination of C and
assembly language in their projects.
The procedure of building and downloading the
resultant image files to the target device is largely
dependent on the tool chain used.
A typical Development flow
The most basic flow uses, assembler, a C compiler, a
linker, and binary file generation utilities.
RealView Development Suite (RVDS) or RealView
Compiler Tools (RVCT) provide a file generation flow as
shown.
• File system_<device>.h
- contains microcontroller specific interrupt
number definitions
- peripheral register definitions.
• File system_<device>.c
- contains a microcontroller specific function called
SystemInit for system initialization.
Using CMSIS cntd…
• In addition, CMSIS compliant device
drivers also contain start-up code (which
contains the vector table)
Benefits of CMSIS
• Better software portability and reusability.
• Allows software to be quickly ported between
Cortex-M3 and other Cortex-M processors,
reducing time to market.
Benefits of CMSIS cntd…
• For embedded OS vendors and middleware
providers, the advantages of the CMSIS are
significant.
• By using the CMSIS, their software products can
become compatible with device drivers from
multiple microcontroller vendors.
Without the CMSIS, the software vendors either
have to include a small library for Cortex-M3
core functions or develop multiple configurations
of their product so that it can work with device
libraries from different microcontroller vendors.
CMSIS Avoids Overlapping Driver
Code.
Using Assembly
• For small projects, it is possible to develop the
whole application in assembly language.
• Using assembler, best optimization is
possible, but with increase in the development
time.
• Handling complex data structures or function
library management can be extremely
difficult.
Using Assembly cntd…
• Yet even when the C language is used in a project, in
some situations part of the program is implemented
in assembly language as follows:
Functions that cannot be implemented in C, such as
direct manipulation of stack data or special instructions
that cannot be generated by the C compiler in normal
C-code.
Timing-critical routines.
Tight memory requirements, causing part of the
program to be written in assembly to get the smallest
memory size.
The Interface between Assembly and C