Professional Documents
Culture Documents
Mark McDermott
Fall 2008
8/22/2008
EE382N-4 Embedded Systems Architecture
8/22/2008 2
EE382N-4 Embedded Systems Architecture
Coprocessors
– Up to 16 coprocessors can be defined
– Expands the ARM instruction set
– Each coprocessor can have up to 16 private registers of any reasonable size
– Load‐store architecture
3
EE382N-4 Embedded Systems Architecture
Thumb
Thumb is a 16‐bit instruction set
– Optimized for code density from C code
– Improved performance form narrow memory
– Subset of the functionality of the ARM instruction set
Core has two execution states – ARM and Thumb
– Switch between them using BX instruction
Thumb has characteristic features:
– Most Thumb instruction are executed unconditionally
– Many Thumb data process instruction use a 2‐address format
– Thumb instruction formats are less regular than ARM instruction formats, as
a result of the dense encoding.
4
EE382N-4 Embedded Systems Architecture
Processor Modes
The ARM has six operating modes:
– User (unprivileged mode under which most tasks run)
8/22/2008 5
EE382N-4 Embedded Systems Architecture
The Registers
ARM has 37 registers in total, all of which are 32‐bits long.
– 1 dedicated program counter
– 1 dedicated current program status register
– 5 dedicated saved program status registers
– 30 general purpose registers
However these are arranged into several banks, with the
accessible bank being governed by the processor mode. Each
mode can access
– a particular set of r0‐r12 registers
– a particular r13 (the stack pointer) and r14 (link register)
– r15 (the program counter)
– cpsr (the current program status register)
And privileged modes can also access
– a particular spsr (saved program status register)
8/22/2008 6
EE382N-4 Embedded Systems Architecture
cpsr
spsr spsr spsr spsr spsr spsr
8/22/2008 7
EE382N-4 Embedded Systems Architecture
cpsr
spsr spsr spsr spsr spsr
8/22/2008 8
EE382N-4 Embedded Systems Architecture
8/22/2008 9
EE382N-4 Embedded Systems Architecture
31 28 8 4 0
N Z CV I F T Mode
8/22/2008 10
EE382N-4 Embedded Systems Architecture
Condition Flags
Flag
8/22/2008 11
EE382N-4 Embedded Systems Architecture
8/22/2008 12
EE382N-4 Embedded Systems Architecture
8/22/2008 13
EE382N-4 Embedded Systems Architecture
8/22/2008 14
EE382N-4 Embedded Systems Architecture
ARM7TDMI
ARM decode
Instruction Thumb→ARM Reg Reg
Shift ALU
Fetch decompress Read Write
Reg Select
ARM9TDMI
ARM or Thumb
Instruction Inst Decode Memory Reg
Shift + ALU Write
Fetch Reg Reg Access
Decode Read
FETCH DECODE EXECUTE MEMORY WRITE
EE382N-4 Embedded Systems Architecture
ARM10
Branch ARM or Memory
Prediction Shift + ALU
Thumb Reg Read Access Reg
Instruction Write
Instruction
Decode Multiply
Fetch Multiply
Add
FETCH ISSUE DECODE EXECUTE MEMORY WRITE
ARM11
Data Data
Address Cache Cache
1 2
EE382N-4 Embedded Systems Architecture
3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
Instruction Type
8/22/2008 17
EE382N-4 Embedded Systems Architecture
Conditional Execution
Most instruction sets only allow branches to be executed
conditionally.
However by reusing the condition evaluation hardware, ARM
effectively increases number of instructions.
– All instructions contain a condition field which determines whether the CPU
will execute them.
– Non‐executed instructions consume 1 cycle.
• Can’t collapse the instruction like a NOP. Still have to complete cycle so as to allow
fetching and decoding of the following instructions.
This removes the need for many branches, which stall the
pipeline (3 cycles to refill).
– Allows very dense in‐line code, without branches.
– The Time penalty of not executing several conditional instructions is
frequently less than overhead of the branch
or subroutine call that would otherwise be needed.
8/22/2008 18
EE382N-4 Embedded Systems Architecture
8/22/2008 19
EE382N-4 Embedded Systems Architecture
By default, data processing operations do not affect the condition flags (apart
from the comparisons where this is the only effect). To cause the condition
flags to be updated, the S bit of the instruction needs to be set by postfixing
the instruction (and any condition code) with an “S”.
– For example to add two numbers and set the condition flags:
• ADDS r0,r1,r2 ; r0 = r1 + r2 ; ...
and set flags
8/22/2008 20
EE382N-4 Embedded Systems Architecture
By default, data processing instructions do not affect the condition code flags but the
flags can be optionally set by using “S”. CMP does not need “S”.
loop
…
SUBS r1,r1,#1 decrement r1 and set flags
BNE loop if Z flag clear then branch
8/22/2008 21
EE382N-4 Embedded Systems Architecture
8/22/2008 22
EE382N-4 Embedded Systems Architecture
8/22/2008 23
EE382N-4 Embedded Systems Architecture
8/22/2008 24
EE382N-4 Embedded Systems Architecture
Conditional Branches
Branch Interpretation Normal uses
B Unconditional Always take this branch
BAL Always Always take this branch
BEQ Equal Comparison equal or zero result
BNE Not equal Comparison not equal or non‐zero result
BPL Plus Result positive or zero
BMI Minus Result minus or negative
BCC Carry clear Arithmetic operation did not give carry‐out
BLO Lower Unsigned comparison gave lower
BCS Carry set Arithmetic operation gave carry‐out
BHS Higher or same Unsigned comparison gave higher or same
BVC Overflow clear Signed integer operation; no overflow occurred
BVS Overflow set Signed integer operation; overflow occurred
BGT Greater than Signed integer comparison gave greater than
BGE Greater or equal Signed integer comparison gave greater or equal
BLT Less than Signed integer comparison gave less than
BLE Less or equal Signed integer comparison gave less than or equal
BHI Higher Unsigned comparison gave higher
BLS Lower or same Unsigned comparison gave lower or same
8/22/2008 25
EE382N-4 Embedded Systems Architecture
8/22/2008 26
EE382N-4 Embedded Systems Architecture
Arithmetic Operations
Operations are:
– ADD operand1 + operand2 ; Add
– ADC operand1 + operand2 + carry ; Add with carry
– SUB operand1 ‐ operand2 ; Subtract
– SBC operand1 ‐ operand2 + carry ‐1 ; Subtract with carry
– RSB operand2 ‐ operand1 ; Reverse subtract
– RSC operand2 ‐ operand1 + carry ‐ 1 ; Reverse subtract with carry
Syntax:
– <Operation>{<cond>}{S} Rd, Rn, Operand2
Examples
– ADD r0, r1, r2
– SUBGT r3, r3, #1
– RSBLES r4, r5, #5
8/22/2008 27
EE382N-4 Embedded Systems Architecture
Comparisons
The only effect of the comparisons is to update the condition
flags. Thus no need to set S bit.
Operations are:
– CMP operand1 ‐ operand2 ; Compare
– CMN operand1 + operand2 ; Compare negative
– TST operand1 AND operand2 ; Test
– TEQ operand1 EOR operand2 ; Test equivalence
Syntax:
– <Operation>{<cond>} Rn, Operand2
Examples:
– CMP r0, r1
– TSTEQ r2, #5
8/22/2008 28
EE382N-4 Embedded Systems Architecture
Logical Operations
Operations are:
AND operand1 AND operand2
EOR operand1 EOR operand2
ORR operand1 OR operand2
ORN operand1 NOR operand2
BIC operand1 AND NOT operand2 [ie bit clear]
Syntax:
– <Operation>{<cond>}{S} Rd, Rn, Operand2
Examples:
AND r0, r1, r2
BICEQ r2, r3, #7
EORS r1,r3,r0
8/22/2008 29
EE382N-4 Embedded Systems Architecture
Data Movement
Operations are:
MOV operand2
MVN NOT operand2
Note that these make no use of operand1.
Syntax:
– <Operation>{<cond>}{S} Rd, Operand2
Examples:
MOV r0, r1
MOVS r2, #10
MVNEQ r1,#0
8/22/2008 30
EE382N-4 Embedded Systems Architecture
8/22/2008 31
EE382N-4 Embedded Systems Architecture
CF Destination 0
8/22/2008 32
EE382N-4 Embedded Systems Architecture
8/22/2008 33
EE382N-4 Embedded Systems Architecture
8/22/2008 34
EE382N-4 Embedded Systems Architecture
8/22/2008 36
EE382N-4 Embedded Systems Architecture
8/22/2008 37
EE382N-4 Embedded Systems Architecture
8/22/2008 38
EE382N-4 Embedded Systems Architecture
8/22/2008 39
EE382N-4 Embedded Systems Architecture
8/22/2008 40
EE382N-4 Embedded Systems Architecture
Multiplication Instructions
The Basic ARM provides two multiplication instructions.
Multiply
– MUL{<cond>}{S} Rd, Rm, Rs ; Rd = Rm * Rs
Multiply Accumulate ‐ does addition for free
– MLA{<cond>}{S} Rd, Rm, Rs,Rn ; Rd = (Rm * Rs) + Rn
Restrictions on use:
– Rd and Rm cannot be the same register
• Can be avoided by swapping Rm and Rs around. This works because multiplication
is commutative.
– Cannot use PC.
These will be picked up by the assembler if overlooked.
Operands can be considered signed or unsigned
– Up to user to interpret correctly.
8/22/2008 41
EE382N-4 Embedded Systems Architecture
Multiplication Implementation
The ARM makes use of Booth’s Algorithm to perform integer
multiplication.
On non‐M ARMs this operates on 2 bits of Rs at a time.
– For each pair of bits this takes 1 cycle (plus 1 cycle to start with).
– However when there are no more 1’s left in Rs, the multiplication will early‐
terminate.
Example: Multiply 18 and ‐1 : Rd = Rm * Rs
Rm 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 18 Rs
Rs ‐1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ‐1 Rm
17 cycles 4 cycles
Note: Compiler does not use early termination criteria to
decide on which order to place operands.
8/22/2008 42
EE382N-4 Embedded Systems Architecture
8/22/2008 43
EE382N-4 Embedded Systems Architecture
8/22/2008 44
EE382N-4 Embedded Systems Architecture
8/22/2008 45
EE382N-4 Embedded Systems Architecture
8/22/2008 46
EE382N-4 Embedded Systems Architecture
r0 Memory
Source
Register 0x5
for STR
r1 r2
Base Destination
Register 0x200 0x200 0x5 0x5 Register
for LDR
8/22/2008 47
EE382N-4 Embedded Systems Architecture
8/22/2008 48
EE382N-4 Embedded Systems Architecture
r1
Base
Register 0x200 0x200
8/22/2008 49
EE382N-4 Embedded Systems Architecture
r1 Offset r0
Updated Source
Base 0x20c 12 0x20c
0x5 Register
Register for STR
0x200 0x5
r1
Original
Base 0x200
Register
8/22/2008 50
EE382N-4 Embedded Systems Architecture
8/22/2008 51
EE382N-4 Embedded Systems Architecture
8/22/2008 52
EE382N-4 Embedded Systems Architecture
8/22/2008 53
EE382N-4 Embedded Systems Architecture
Effect of endianess
The ARM can be set up to access its data in either little or big
endian format.
Little endian:
– Least significant byte of a word is stored in bits 0‐7 of an addressed word.
Big endian:
– Least significant byte of a word is stored in bits 24‐31 of an addressed word.
This has no real relevance unless data is stored as words and then
accessed in smaller sized quantities (halfwords or bytes).
– Which byte / halfword is accessed will depend on the endianess of the
system involved.
8/22/2008 54
EE382N-4 Embedded Systems Architecture
YA Endianess Example
r0 = 0x11223344
31 24 23 16 15 87 0
11 22 33 44
31 24 23 16 15 87 0 31 24 23 16 15 87 0
00 00 00 44 00 00 00 11
r2 = 0x44 r2 = 0x11
8/22/2008 55
EE382N-4 Embedded Systems Architecture
8/22/2008 56
EE382N-4 Embedded Systems Architecture
8/22/2008 57
EE382N-4 Embedded Systems Architecture
Stacks
A stack is an area of memory which grows as new data is
“pushed” onto the “top” of it, and shrinks as data is “popped” off
the top.
Two pointers define the current limits of the stack.
– A base pointer
• used to point to the “bottom” of the stack (the first location).
– A stack pointer
• used to point the current “top” of the stack.
PUSH
{1,2,3} POP
SP 3 Result of
2 SP 2 pop = 3
1 1
SP
BASE BASE
BASE
8/22/2008 58
EE382N-4 Embedded Systems Architecture
Stack Operation
Traditionally, a stack grows down in memory, with the last “pushed” value at
the lowest address. The ARM also supports ascending stacks, where the stack
structure grows up through memory.
The value of the stack pointer can either:
– Point to the last occupied address (Full stack)
• and so needs pre‐decrementing (ie before the push)
– Point to the next occupied address (Empty stack)
• and so needs post‐decrementing (ie after the push)
The stack type to be used is given by the postfix to the instruction:
– STMFD / LDMFD : Full Descending stack
– STMFA / LDMFA : Full Ascending stack.
– STMED / LDMED : Empty Descending stack
– STMEA / LDMEA : Empty Ascending stack
Note: ARM Compiler will always use a Full descending stack.
8/22/2008 59
EE382N-4 Embedded Systems Architecture
Stack Examples
STMFD sp!, STMED sp!, STMFA sp!, STMEA sp!,
{r0,r1,r3-r5} {r0,r1,r3-r5} {r0,r1,r3-r5} {r0,r1,r3-r5}
0x418
SP r5 SP
r4 r5
r3 r4
r1 r3
r0 r1
Old SP Old SP r5 Old SP Old SP r0 0x400
r5 r4
r4 r3
r3 r1
r1 r0
SP r0 SP
0x3e8
8/22/2008 60
EE382N-4 Embedded Systems Architecture
See the chapter on the ARM Procedure Call Standard in the SDT Reference
Manual for further details of register usage within subroutines.
If the pop instruction also had the ‘S’ bit set (using ‘^’) then the transfer of the
PC when in a privileged mode would also cause the SPSR to be copied into the
CPSR (see exception handling module).
8/22/2008 61
EE382N-4 Embedded Systems Architecture
8/22/2008 62
EE382N-4 Embedded Systems Architecture
8/22/2008 63
EE382N-4 Embedded Systems Architecture
1
Rn
temp
2 3
Memory
Rm Rd
8/22/2008 64
EE382N-4 Embedded Systems Architecture
8/22/2008 65
EE382N-4 Embedded Systems Architecture
Backup
8/22/2008
EE382N-4 Embedded Systems Architecture
Assembler: Pseudo‐ops
8/22/2008 67
EE382N-4 Embedded Systems Architecture
Assembler: Pseudo‐ops
8/22/2008 68
EE382N-4 Embedded Systems Architecture
Assembler: Pseudo‐ops
8/22/2008 69
EE382N-4 Embedded Systems Architecture
8/22/2008 70
EE382N-4 Embedded Systems Architecture
Example: C assignments
C:
x = (a + b) - c;
Assembler:
ADR r4,a ; get address for a
LDR r0,[r4] ; get value of a
ADR r4,b ; get address for b, reusing r4
LDR r1,[r4] ; get value of b
ADD r3,r0,r1 ; compute a+b
ADR r4,c ; get address for c
LDR r2,[r4] ; get value of c
SUB r3,r3,r2 ; complete computation of x
ADR r4,x ; get address for x
STR r3,[r4] ; store value of x
© 2008 Wayne Wolf Computers as Components 2nd ed.
8/22/2008 71
EE382N-4 Embedded Systems Architecture
Example: C assignment
C:
y = a*(b+c);
Assembler:
ADR r4,b ; get address for b
LDR r0,[r4] ; get value of b
ADR r4,c ; get address for c
LDR r1,[r4] ; get value of c
ADD r2,r0,r1 ; compute partial result
ADR r4,a ; get address for a
LDR r0,[r4] ; get value of a
MUL r2,r2,r0 ; compute final value for y
ADR r4,y ; get address for y
STR r2,[r4] ; store y
Example: C assignment
C:
z = (a << 2) | (b & 15);
Assembler:
ADR r4,a ; get address for a
LDR r0,[r4] ; get value of a
MOV r0,r0,LSL 2 ; perform shift
ADR r4,b ; get address for b
LDR r1,[r4] ; get value of b
AND r1,r1,#15 ; perform AND
ORR r1,r0,r1 ; perform OR
ADR r4,z ; get address for z
STR r1,[r4] ; store value for z
Example: if statement
C:
if (a > b) { x = 5; y = c + d; } else x = c - d;
Assembler:
; compute and test condition
ADR r4,a ; get address for a
LDR r0,[r4] ; get value of a
ADR r4,b ; get address for b
LDR r1,[r4] ; get value for b
CMP r0,r1 ; compare a < b
BLE fblock ; if a ><= b, branch to false block
if statement, cont’d.
; true block
MOV r0,#5 ; generate value for x
ADR r4,x ; get address for x
STR r0,[r4] ; store x
ADR r4,c ; get address for c
LDR r0,[r4] ; get value of c
ADR r4,d ; get address for d
LDR r1,[r4] ; get value of d
ADD r0,r0,r1 ; compute y
ADR r4,y ; get address for y
STR r0,[r4] ; store y
B after ; branch around false block
if statement, cont’d.
; false block
fblock ADR r4,c ; get address for c
LDR r0,[r4] ; get value of c
ADR r4,d ; get address for d
LDR r1,[r4] ; get value for d
SUB r0,r0,r1 ; compute a-b
ADR r4,x ; get address for x
STR r0,[r4] ; store value of x
after ...
82
EE382N-4 Embedded Systems Architecture
83
EE382N-4 Embedded Systems Architecture
84
EE382N-4 Embedded Systems Architecture
85