Академический Документы
Профессиональный Документы
Культура Документы
Thomas Schmid
thomas.schmid@utah.edu
Adapted from Prabal Dutta (prabal@umich.edu)
Announcements
You wont use the HW1 Board for this class
It was an exercise in building boards
You might have to make a board for your project
Conditional Execution
Solution to last weeks Minute Quiz
Saleae Logic 16
Sampling increases if you reduce channels
Oscilloscope
2
Announcements
Books
The Definitive Guide to the ARM Cortex-M3
Second Edition
Graduate Students
You will make presentations in second part of class
Your project should have a research component
Outline
Minute quiz
Announcements
ARM Cortex-M3 ISA
Assembly tool flow
C/Assembly mixed tool flow
32-bits
32-bits
!mov!r0,!#1
!ld!!r1,![r0,#5]
!!!!!!!!!!mem((r0)+5)
!bne!loop
!subs!r2,!#1
Endianness
Endianness
ranch instructions
Branch
ble A4-1 summarizes the branch instructions in the Thumb instruction set. In addition to providing for
anges in the flow of execution, some branch instructions can change instruction set.
Table A4-1 Branch instructions
Instruction
Usage
Range
B on page A6-40
+/1 MB
0-126 B
BL on page A6-49
Call a subroutine
+/16 MB
Any
BX on page A6-51
Any
0-510 B
0-131070 B
R and LDM instructions can also cause a branch. See Load and store instructions on page A4-16 and
Branch Encoding
B<c>
<label>
Encoding
T2
Encoding T2
Not allowed
in IT block.
All versions of the Thumb instruction
set.
B<c> <label>
Outside or last in IT block
B<c> <label>
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 0 1
cond
imm8
Out
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 1 0 0
imm11
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Not
15 14 13 12 Not
11 10allowed
9 8 7in6IT5block.
4 3 2 1 0 15 14 13 12 11 10
1 1 1 1 0 S
cond
imm6
1 0 J1 0 J2
B<c>.W <label>
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 1 0 0
imm11
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
ARMv7-M
1 0Encoding
J1 0 J2T4
imm11
Encoding T3
Encoding
B<c>.W
<label>
T4
Out
B<c>.W <label>
ARMv7-M
ARMv7-M
Not 15
allowed
14 13in12IT11block.
10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10
1 1 1 1 Outside
0 S
B<c>.W <label>
or last inimm10
IT block
1 0 J1 1 J2
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
1 1 1 1 0 S
cond
imm6
1 0 J1 0 J2
imm11
I1 13
= NOT(J1
EOR 3S);2 imm32
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 15 14
12 11 EOR
10 S);
9 8 I27= NOT(J2
6 5 4
1 0 = SignExtend(S:I1
if InITBlock() && !LastInITBlock() then UNPREDICTABLE;
1 1 1 1 0 S
Encoding T4
ARMv7-M
imm10
1 0 J1 1 J2
imm11
B<c>.W <label>
1 1 1 1 0 S
8
imm10
1 0 J1 1 J2
imm11
In Thumb instructions, the condition (if it is not AL) is normally encoded in a preceding IT instruction, see
Conditional instructions on page A4-4, ITSTATE on page A6-10 and IT on page A6-78 for details. Some
conditional branch instructions do not require a preceding IT instruction, and include a condition code in
their encoding.
Conditional execution:
Append to many instructions for conditional execution
cond
Mnemonic
extension
Meaning (integer)
Meaning (floating-point) ab
Condition flags
0000
EQ
Equal
Equal
Z == 1
0001
NE
Not equal
Z == 0
0010
CS c
Carry set
C == 1
0011
CC d
Carry clear
Less than
C == 0
0100
MI
Minus, negative
Less than
N == 1
0101
PL
N == 0
0110
VS
Overflow
Unordered
V == 1
0111
VC
No overflow
Not unordered
V == 0
1000
HI
Unsigned higher
C == 1 and Z == 0
1001
LS
C == 0 or Z == 1
1010
GE
N == V
1011
LT
N != V
1100
GT
Greater than
Z == 0 and N == V
1101
LE
Z == 1 or N != V
1110
None (AL) e
Always (unconditional)
Always (unconditional)
Any
a.
b.
c.
d.
e.
<cond
> specifi
es theesbase
of the instruction
block; the
first instruction
<cond
> specifi
the condition
base condition
of the instruction
block;
the first instru
following
IT executes
if <cond
> is true
Useful
for small
If-Then-Else
following
IT executes
if <cond
>constructs
is true
of <x >, <y >, and <z > can be either T (THEN) or E (ELSE), which refers to the base
Each
of <x >, <y >,<cond>
and <z > can be either T (THEN) or E (ELSE), which refers to t
Each
IT<x><y><z>
condition <cond >, whereas <cond > uses traditional syntax such as EQ, NE, GT, or the like.
condition
<cond
>, whereas
<cond
>Euses
traditional
syntax
such
as EQ, NE, GT, o
<x>,
<y>,
<z>
can
be
T
or
which
refers
to
the
base
Here is an example of IT use:
Here
is an example
of IT use:
condition
<cond>
} be written as:
This can
CMP
R0,
; Compare R0 and R1
This can
be R1
written as:
ITTEE EQ
; If R0 equal R1, Then-Then-Else-Else
CMP R3,R0,
; Compare
ADDEQ
R4, R1
R5 ; Add
if equal R0 and R1
ITTEE
; If R0 equal
ASREQ
R3,EQ
R3, #1 ; Arithmetic
shift R1,
rightThen-Then-Else-Else
if equal
ADDEQ
; Add
if equal
equal
ADDNE
R3,R3,
R6, R4,
R7 ;R5Add
if not
ASREQ
; Arithmetic
equal
ASRNE
R3,R3,
R3, R3,
#1 ;#1Arithmetic
shiftshift
right right
if notif
equal
10
Instruction classes
Branching
Data processing
Load/store
Exceptions
Miscellaneous
11
Addressing Modes
Offset Addressing
Offset is added or subtracted from base register
Result used as effective address for memory access
[<Rn>, <offset>]
Pre-indexed Addressing
Offset is applied to base register
Result used as effective address for memory access
Result written back into base register
[<Rn>, <offset>]!
Post-indexed Addressing
The address from the base register is used as the EA
The offset is applied to the base and then written back
[<Rn>], <offset>
12
<offset> options
An immediate constant
#10
An index register
<Rm>
13
sub r0, r1
r0 ! r0 r1
APSR remain unchanged
subs r0, r1
r0 ! r0 r1
APSR N or Z bits could change
add r0, r1
r0 ! r0 + r1
APSR remain unchanged
adds r0, r1
r0 ! r0 + r1
APSR C or V bits could change
14
15
Modern version
Danny Cohen
IEEE Computer, v14, #10
Published in 1981
Satire on CS religious war
Historical Inspiration
Jonathan Swift
Gullivers Travels
Published in 1726
Satire on Henry-VIIIs split
with the Church
16
Little-Endian
LSB is at lower address
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Memory!!!!!Value
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Offset!!(LSB)!(MSB)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!======!!===========
uint8_t!a!!=!1;!!!!!!!!!!!!!!!0x0000!!01!02!FF!00
uint8_t!b!!=!2;
uint16_t!c!=!255;!//!0x00FF
uint32_t!d!=!0x12345678;!!!!!!0x0004!!78!56!34!12
Big-Endian
MSB is at lower address
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Memory!!!!!Value
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Offset!!(LSB)!(MSB)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!======!!===========
uint8_t!a!!=!1;!!!!!!!!!!!!!!!0x0000!!01!02!00!FF
uint8_t!b!!=!2;
uint16_t!c!=!255;!//!0x00FF
uint32_t!d!=!0x12345678;!!!!!!0x0004!!12!34!56!78
Instruction encoding
Instructions are encoded in machine language opcodes
Sometimes
Necessary to hand generate opcodes
Necessary to verify assembled code is correct
How?
ARMv7 ARM
Register!Value!!!!!!Memory!Value
Instructions
001|00|000|00001010!(LSB)!(MSB)
movs!r0,!#10
A7.7.75
MOV (immediate)
(msb)!!!!!!!!!(lsb)!0a!20!00!21
Move (immediate) writes an immediate value to the destination register. It can op
001|00|001|00000000
movs!r1,!#0
condition flags based on the value.
17
Encoding T1
MOVS <Rd>,#<imm8>
MOV<c> <Rd>,#<imm8>
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 1 0 0
Rd
imm8
Outside IT blo
Inside IT bloc
Execution begins
18
!
!
!
!
!
!
_start:
!
start:
!
!
.equ!
.text
.syntax!
.thumb
.global!
.type!
STACK_TOP,!0x20000800
_start
start,!%function
.word!
STACK_TOP,!start
unified
movs!r0,!#10
...
Outline
Minute quiz
Announcements
ARM Cortex-M3 ISA
Assembly tool flow
C/Assembly mixed tool flow
19
Assembly
files!(.s)
Object
files!(.o)
as
(assembler)
Executable
image!file
ld
(linker)
ob
jc
op
y
Binary!program
file!(.bin)
ob
jd
um
p
Memory
layout
Linker
script!(.ld)
20
Disassembled
code!(.lst)
What are the real GNU executable names for the ARM?
Just add the prefix arm-none-eabi- prefix
Assembler (as)
arm-none-eabi-as
Linker (ld)
arm-none-eabi-ld
C Compiler (gcc)
arm-none-eabi-gcc
all:
!
!
!
!
22
armhnoneheabihas!hmcpu=cortexhm3!hmthumb!example1.s!ho!example1.o
armhnoneheabihld!hTtext!0x0!ho!example1.out!example1.o
armhnoneheabihobjcopy!hObinary!example1.out!example1.bin
armhnoneheabihobjdump!hS!example1.out!>!example1.lst
23
armhnoneheabihas!hmcpu=cortexhm3!hmthumb!example1.s!ho!example1.o
armhnoneheabihld!hTtext!0x0!ho!example1.out!example1.o
armhnoneheabihobjcopy!hObinary!example1.out!example1.bin
armhnoneheabihobjdump!hS!example1.out!>!example1.lst
.equ!
STACK_TOP,!
example1.out:!!!!!file!format!elf32hlittlearm
.text
.syntax!
.thumb
.global!
.type!
unified
Disassembly!of!section!.text:
_start
start,!%function
00000000!<_start>:
!!!0:!
20000800!! .word!
!!!4:!
00000009!! .word!
0x20000800
0x00000009
.word!
STACK_TOP,!start
00000008!<start>:
!!!8:!
200a!!!!!!! movs!
!!!a:!
2100!!!!!!! movs!
r0,!#10
r1,!#0
adds!r1,!r0
subs!r0,!#1
bne!!loop
0000000c!<loop>:
!!!c:!
1809!!!!!!! adds!
!!!e:!
3801!!!!!!! subs!
!!10:!
d1fc!!!!!!! bne.n!
r1,!r1,!r0
r0,!#1
c!<loop>
b!!!!deadloop
.end
00000012!<deadloop>:
!!12:!
e7fe!!!!!!! b.n!
12!<deadloop>
movs!r0,!#10
movs!r1,!#0
24
/*!Equates!symbol!to!value!*/
/*!Tells!AS!to!assemble!region!*/
/*!Means!language!is!ARM!UAL!*/
/*!Means!ARM!ISA!is!Thumb!*/
/*!.global!exposes!symbol!*/
/*!_start!label!is!the!beginning!*/
/*!...of!the!program!region!*/
/*!Specifies!start!is!a!function!*/
/*!start!label!is!reset!handler!*/
/*!Inserts!word!0x20000800!*/
/*!Inserts!word!(start)!*/
/*!Weve!seen!the!rest!...!*/
$!armhnoneheabihas!hmcpu=cortexhm3!hmthumb!example1.s!ho!example1.o
25
ELF headers
Program headers
Section headers
Symbol table
Files attributes
Other options
-s shows symbols
-S shows section headers
26
27
$!armhnoneheabihld!hTtext!0x0!Tbss!0x20000000!ho!example1.out!example1.o
28
29
.bss segment
Static (uninitialized) variables
Zero-filled by CRT or OS
From: Block Started by Symbol
31
32
33
35
36
37
Outline
Minute quiz
Announcements
ARM Cortex-M3 ISA
Assembly tool flow
C/Assembly mixed tool flow
39
Assembly
files!(.s)
Object
files!(.o)
as
(assembler)
Executable
image!file
gcc
(compile
+!link)
ob
jc
op
y
ld
(linker)
Binary!program
file!(.bin)
ob
jd
um
p
Memory
layout
40
Library!object
files!(.o)
Linker
script!(.ld)
Disassembled
Code!(.lst)
$!armhnoneheabihgcc!\
!!hmcpu=cortexhm3!\
!!hmthumb!main.c!\
!!hT!generichhosted.ld!\
!!ho!factorial
$!qemuharm!hcpu!cortexhm3!\
!!./factorial
factorial(0)!=!1
factorial(1)!=!1
factorial(2)!=!2
factorial(3)!=!6
factorial(4)!=!24
factorial(5)!=!120
factorial(6)!=!720
factorial(7)!=!5040
factorial(8)!=!40320
factorial(9)!=!362880
Answer:!40/5!" 8
41
Questions?
Comments?
Discussion?
42