Chapter 3
Chapter 3
Chapter 3
ARM Instruction Set Architecture
CE-475
Real-Time Embedded Systems
Spring 2023
STM32F4
2
ARM Processors
ARM Cortex-A family:
Applications processors
Support OS and high-performance applications
Such as Smartphones, Smart TV
ARM Cortex-R family:
Real-time processors with high performance and
high reliability
Support real-time processing and mission-critical
control
ARM Cortex-M family:
Microcontroller
Cost-sensitive, support SoC
3
ARM Processors
Cortex-A57
Cortex-A53
Cortex-A15
Cortex-A9 Cortex-A
Cortex-A8
Cortex-A7
Cortex-A5
Cortex-R7
Cortex-R5
Cortex-R4
Cortex-R
STM32F4 Discovery Board Cortex-M4
Cortex-M3
Cortex-M1
Cortex-M0+
Cortex-M
Cortex-M0
SC000
SC100
SC300
SecurCore
ARM11
ARM9
ARM7
Classic
4
Instruction Sets
Instructions:
Encoded to binary machine code by
assembler
Executed at runtime by hardware
Early 32-bit ARM vs Thumb/Thumb-2
Early ARM has larger power consumption and
larger program size
16-bit Thumb, first used in ARM7TDMI
processors in 1995
Thumb-2: a mix of 16-bit (high code density)
and 32-bit (high performance) instructions
ARM Cortex-M:
Subset of Thumb-2
5
Instruction Sets
Instructions:
Encoded to binary machine code by
assembler
Executed at runtime by hardware
Early 32-bit ARM vs Thumb/Thumb-2
Early ARM has larger power consumption and
larger program size
16-bit Thumb, first used in ARM7TDMI
processors in 1995
Thumb-2: a mix of 16-bit (high code density)
and 32-bit (high performance) instructions
ARM Cortex-M:
Subset of Thumb-2
6
Instruction Sets
16-bit
32-bit
7
from arm.com
Processor Registers
32 bits
Special registers
Register
R14 (LR) FAULTMASK
R15 (PC) CONTROL
xPSR, BASEPRI, PRIMASK, etc
8
Processor Registers
32 bits
R0
Low Registers (R0 – R7)
R1
R2 Can be accessed by any instruction
Low
Registers
R3 High Register (R8 – R12)
R4
R5 Can only be accessed by some instructions
Stack Pointer (R13)
General
R6 Purpose
Register
R7
R8
Cortex-M4 supports two stacks
R9
32 bits
Main SP (MSP) for privileged access (e.g.
High
Registers R10 exception handler)
R11 xPSR
Process SP (PSP) for application access
R12 BASEPRI
R13 (SP) R13 (MSP) R13 (PSP) PRIMASK
Special
Purpose
Register
Program Counter (R15)
R14 (LR) FAULTMASK Memory address of the current instruction
R15 (PC) CONTROL
9
Processor Registers vs Peripheral Registers
ALU
Registers Registers
Control Unit
Registers GPIO A GPIO B
10
Processor Registers vs Peripheral Registers
Processor can directly access processor registers
ADD r3,r1,r0 ; r3 = r1 + r0
11
Interfacing Peripherals
Memory-mapped I/O
0x48000024
0x48000020
0x4800001C
Core Pin output
STR 0x48000018
0x48000014 GPIO Data Output Register GPIO
Output
0x48000010
Memory Space
12
Memory
Map
13
C vs Assembly
14
Load-Modify-Store
Translating C to assembly
• Load values from memory into registers
• Modify value by applying arithmetic operations
• Store result from register to memory
15
Load-Modify-Store
16
ARM Cortex-M3 Organization (STM32L1)
SW/JTAG LCD SPI2
TIM2 I2C1
TIM4 I2C2
Cortex-M3 Processor Core Instructions TIM6 USB 2.0 FS
Flash TIM7 DAC1
Instruction Bus Memory USART2
Instruction Decoder
Interrupt Controller
Interface
Data
Trace & Debug
Memory
Memory SRAM WWDG
Protection
(NVIC)
ALU
Unit
System-on-a-chip
17
ARM Cortex-M4 Organization (STM32L4)
LCD SPI2
TIM2 SPI3
TIM3 I2C1/SMBUS
SW/JTAG
TIM4 I2C2/SMBUS
TIM6 I2C3/SMBUS
TIM7 USB 2.0 FS
Cortex-M4 Processor Core Instructions USART2 bxCAN
Flash
USART3 SWPMI1
Instruction Bus Memory
Instruction Decoder
Multiple Data (DSP)
Interrupt Controller
Processor Control
Single Instruction
USART5 LPTIM2
Interface
Data
Memory
Trace & Debug
FPU (optional)
Memory SRAM OpAmp
` Protection
(NVIC)
ALU
Unit
19
Instruction Format: Labels
20
Instruction Format: Labels
21
Instruction Format: Mnemonic
22
Instruction Format: Operands
label mnemonic operand1, operand2, operand3 ; comments
Operands
Registers
Constants (called immediate values)
Number of operands varies
No operands: DSB
One operand: BX LR
Two operands: CMP R1, R2
Three operands: ADD R1, R2, R3
Four operands: MLA R1, R2, R3, R4
Normally
operand1 is the destination register, and operand2 and operand3 are source operands.
operand2 is usually a register, and the first source operand
operand3 may be a register, an immediate number, a register shifted to a constant number of bits, or a register
plus an offset (used for memory access).
23
Instruction Format: Comments
24
ARM Instruction Format
25
ARM Instruction Format
Bad comment!
A better example:
26
ARM Instruction Format
27
Example Assembly Program:
Copying a String
28
Assembly Directives
Directives are NOT instructions. Instead, they are used to provide key information for assembly.
29
Directive: AREA
AREA myData, DATA, READWRITE ; Define a data section
Array DCD 1, 2, 3, 4, 5 ; Define an array with five integers
The AREA directive indicates to the assembler the start of a new data or code section.
Areas are the basic independent and indivisible unit processed by the linker.
Each area is identified by a name and areas within the same source file cannot share the same name.
An assembly program must have at least one code area.
By default, a code area can only be read (READONLY) and a data area may be read from and written to (READWRITE).
30
Directive: ENTRY
31
Directive: END
32
Directive: PROC and ENDP (Procedure and EndProcedure
PROC and ENDP are to mark the start and end of a function (also called subroutine or procedure).
A single source file can contain multiple subroutines, with each of them defined by a pair of PROC and ENDP.
PROC and ENDP cannot be nested. We cannot define a function within another function.
33
Directive: EXPORT and IMPORT
The EXPORT declares a symbol and makes this symbol visible to the linker.
The IMPORT gives the assembler a symbol that is not defined locally in the current
assembly file. The symbol must be defined in another file.
The IMPORT is similar to the “extern” keyword in C.
34
Directive: Defining Data
35
Directive: Defining Data
AREA myData, DATA, READWRITE
hello DCB "Hello World!",0 ; Allocate a string that is null-terminated
dollar DCB 2,10,0,200 ; Allocate integers ranging from -128 to 255
scores DCD 2,3.5,-0.8,4.0 ; Allocate 4 words containing decimal values
miles DCW 100,200,50,0 ; Allocate integers between –32768 and 65535
Pi_S DCFS 3.14 ; Allocate a single-precision floating number
Pi_D DCFD 3.14 ; Allocate a double-precision floating number
p SPACE 255 ; Allocate 255 bytes of zeroed memory space
36
Directive: EQU and RN (Equate, RegisterName)
37