Академический Документы
Профессиональный Документы
Культура Документы
UCSD ECE 30
Topics
ARM Data path
ARM Registers
ARM Modes
Programming ARM
Arithmetic Operations
ARM Condition Codes
ECE 30 Lecture 2
Comparison Operations
Move and Logic Operations
Branch Operations
Shift Options
Multiplication Operations
2
UCSD ECE 30
UCSD ECE 30
ARM Datapath
Supervisor
CPU
Address
Register
PC-bus
ALU-bus
MUX
PC
RAM
Memory
Write
Data
Register
Register
Bank
A-bus
ALU
32 x 8
Multiplier
B-bus
Read
Data
Register
Instruction Decode
& Control
IRQ
Undefined
FIQ
r13_undef
r14_undef
r8_fiq
r9_fiq
r10_fiq
r11_fiq
r12_fiq
r13_fiq
r14_fiq
spsr_undef
spsr_fiq
r0
r1
r2
r3
r4
r5
r6
r7
MUX
Address
Incrementer
Abort
Barrel
Shifter
r13 (sp)
r14 (lr)
r13_svc
r14_svc
NA
spsr_svc
r8
r9
r10
r11
r12
r13_abt
r13_irq
r13_abt
r14_irq
r15 (pc)
cpsr
spsr_abt
spsr_irq
UCSD ECE 30
UCSD ECE 30
ARM Modes
User
r0
r1
r2
r3
r4
r5
r6
r7
r8
r9
r10
r11
r12
r13
r14
r15 (pc)
cpsr
Interrupt
UCSD ECE 30
N Z C V
Not Used
FIQ
r0
r1
r2
r3
r4
r5
r6
r7
r8_fiq
r9_fiq
r10_fiq
r11_fiq
r12_fiq
r13_fiq
r14_fiq
r15 (pc)
cpsr
spsr_fiq
Address
of FIQ
routine
UCSD ECE 30
7 6 5 4 3 2 1 0
I F T
Mode
C-language: a = b + c;
Assembly language: ADD r0, r1, r2
UCSD ECE 30
UCSD ECE 30
Arithmetic Operations
; r0 = r1 + r2
; if CPSC=GT then r3 = r3 - 1
; if CPSC=LE then r4 = 5 - r5
;
and set CPSC with result
C-code
ARM Assembly
a = b + c;
a = b - c;
a = -b + c;
c = e - a;
b = b + e;
a = b - 5;
a = 2 - b;
UCSD ECE 30
C-code
ARM Assembly
UCSD ECE 30
Register Assignment
One important task for the compiler is to decide
what variable name in C-code will correspond with
what register in ARM.
If there are more variables than registers (likely),
values can be swapped back and forth from
memory.
A good compiler makes assignments that require as
few memory swaps as possible.
12
UCSD ECE 30
UCSD ECE 30
Code
Meaning
Binary
EQ
NE
HS/CS
LO/CC
MI
PL
VS
VC
HI
LS
GE
LT
GT
LE
AL
NV
ALU result = 0
ALU result 0
C-bit set (Carry)
C-bit clear (No carry)
N-bit set (Negative)
N-bit clear (Non-negative)
V-bit set (Overflow)
V-bit clear (No overflow)
C-bit set and Z-bit clear
C-bit clear or Z-bit set
N-bit = V-bit ( >= )
N-bit V-bit ( < )
Z-bit clear and V-bit set ( > )
Z-bit set or N-bit V-bit ( <= )
Always ( same as no cond )
Never (reserved)
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
13
UCSD ECE 30
Comparisons
UCSD ECE 30
Example:
CMP r0, r1
15
;
;
;
;
;
;
r0
if
r1
r0
r2
if
= r1 AND r2
CPSC=EQ then
= r3 EXOR r0
= r1
= 10 and set
CPSC=EQ then
UCSD ECE 30
UCSD ECE 30
Branch Instructions
Branch: B{<cond>} label
r0=r1
?
Stop
N
Y
Examples:
SUBS r0, r1, r2
BEQ isZero
ADD r0, r0, #1
isZero: AND r3, r0, #255
...
BL sub1
AND r3, r0, #255
...
r0=r0-r1
r0>r1
?
r1=r1-r0
17
UCSD ECE 30
UCSD ECE 30
if - else Statement
C-code
ARM Assembly
Example Code
Normal
gcd: cmp
beq
blt
sub
bal
less:sub
bal
stop:...
Assembler
r0,r1
stop
less
r0, r0, r1
gcd
r1, r1, r0
gcd
if(a==b) {
}
c = c + 2;
; if r0>r1
; subtract r1 from r0
if (a==b) {
; subtract r0 from r1
} else {
}
c = c + 2;
if(a==b) {
if r0>r1
subtract r1 from r0
subtract r0 from r1
reached the end?
}
c = c + 2;
if (a==b) {
} else {
}
c = c + 2;
19
lbl1:
CMP r0, r1
BNE lbl1
MOV r2, r3
ADD r2, r2, #2
lbl2:
lbl1:
CMP r0, r1
BNE lbl2
MOV r2, r3
B lbl1
MOV r2, r4
ADD r2, r2, #2
c = d;
c = d;
c = e;
c = d;
e = f;
c = d;
c = e;
CMP r0, r1
MOVEQ r2, r3
MOVEQ r4, r5
ADD r2, r2, #2
CMP r0, r1
MOVEQ r2, r3
MOVNE r2, r4
ADD r2, r2, #2
UCSD ECE 30
UCSD ECE 30
ARM Assembly
lbl1:
lbl1:
lbl2:
switch (a) {
case 1: b = a;
break;
case 2: c = a;
break;
case 5: f = a;
break;
default: m = a;
}
a = a +2;
CMP r0, r1
BEQ lbl1
; Outer if cant use cond. exec.
CMP r2, r3
SUBLT r0, r0, r1 ; Inner if uses cond. exec.
SUBGT r0, r1, r0 ;
B lbl2
; End of outer true clause
CMP r0, #0
; Outer else, inner if
RSBLT r0, r0, #0 ; Inner if uses cond. exec.
ADD r2, r2, #2
UCSD ECE 30
C-code
while ( a < b ) {
a = a + a;
}
a = a - b;
ARM Assembly
wlp1:
wlp2:
CMP r0, r1
BGE wlp2
ADD r0, r0, r0
B wlp1
SUB r0, r0, r1
lbl2:
lbl3:
lbl4:
lbl5:
CMP r0, #1
BNE lbl2
MOV r1, r0
B lbl5
CMP r0, #2
BNE lbl3
MOV r2, r0
B lbl5
CMP r0, #5
BNE lbl4
MOV r5, r0
B lbl5
MOV r12, r0
ADD r0, r0, #2
UCSD ECE 30
while Loops
while ( a < b ) {
a = a + a;
}
a = a - b;
ARM Assembly
for Loops
C-code
ARM Assembly
flp1:
flp2:
MOV r0, #0
CMP r0, r1
BGE flp2
LDR r5, [r3, r0 LSL #2]
LDR r6, [r4, r0 LSL #2]
ADD r5, r5, r6
STR r5, [r2, r0 LSL #2]
ADD r0, r0, #1
B flp1
SUB r1, r1, #1
UCSD ECE 30
UCSD ECE 30
Shift Instructions
Shift Examples
ADD r0,r1,r1,LSL#2
; r0 = r1*5 = r1 + r1*4
Binary Value
CF
Binary Value
CF
Binary Value
CF
Binary Value
CF
25
UCSD ECE 30
ROR
ROR
ROR
ROR
;
;
;
;
UCSD ECE 30
26
#0
#30 => * 22
#28 => * 24
#26 => * 26
28
UCSD ECE 30
UCSD ECE 30
Multiplication
MUL{<cond>}{S} Rd, Rm, Rs
MLA{<cond>}{S} Rd, Rm, Rs, Rn
Review
ARM Data path
; Rd = Rm * Rs
ARM Registers
; Rd = Rm * Rs + Rn
provide:
RdLo,RdHi,Rm,Rs
RdLo,RdHi,Rm,Rs
RdLo, RdHi, Rm, Rs
RdLo, RdHi, Rm, Rs
Arithmetic Operations
ARM Condition Codes, Conditional Execution
;Unsigned
;Unsigned,Accumulate
;Signed
;Signed,Accumulate
Comparison Operations
Move and Logic Operations
Branch, Branch and Link
Shift Options
Multiplication Operations
29
30