Professional Documents
Culture Documents
Programming
Outline:
the ARM instruction set
writing simple programs
examples
Assembly v5 - 1
Assembly v5 - 3
r0, r1, r2
r0, r1, #2
;
;
r0 := r1 + r2
r0 := r1 - 2
Note:
everything after the ; is a comment
it is there solely for the programmers
convenience
Assembly v5 - 4
Arithmetic operations:
ADD r0, r1, r2
; r0 :=
ADC r0, r1, r2
; r0 :=
SUB r0, r1, r2
; r0 :=
SBC r0, r1, r2
; r0 :=
RSB r0, r1, r2
; r0 :=
RSC r0, r1, r2
; r0 :=
r1
r1
r1
r1
r2
r2
+
+
-
r2
r2 + C
r2
r2 + C - 1
r1
r1 + C - 1
Examples:
ADD
SUB
Assembly v5 - 2
Assembly v5 - 5
Assembly v5 - 6
AND
ORR
EOR
BIC
r0,
r0,
r0,
r0,
r1,
r1,
r1,
r1,
r2
r2
r2
r2
;
;
;
;
r0
r0
r0
r0
:=
:=
:=
:=
r1
r1
r1
r1
and r2
or r2
xor r2
and not r2
MOV
MVN
Assembly v5 - 7
r1,
r1,
r1,
r1,
r2
r2
r2
r2
;
;
;
;
set
set
set
set
cc
cc
cc
cc
on
on
on
on
r1
r1
r1
r1
- r2
+ r2
and r2
xor r2
; r0 := r2
; r0 := not r2
Assembly v5 - 8
Comparison operations:
CMP
CMN
TST
TEQ
r0, r2
r0, r2
Immediate operands
the second source operand (r2) may be
replaced by a constant:
ADD
AND
r3, r3, #1
; r3 := r3 + 1
r8, r7, #&ff ; r8 := r7[7:0]
Assembly v5 - 9
Assembly v5 - 10
31
ADD
ADD
Assembly v5 - 11
00000
00000
LSL #5
31
LSR #5
0
31
00000 0
11111 1
31
ROR #5
RRX
Assembly v5 - 12
Assembly v5 - 13
Assembly v5 - 15
Assembly v5 - 16
Addressing memory
LDR
STR
Assembly v5 - 14
; r4 := (r3 x r2)[31:0]
Multiplication
Assembly v5 - 17
ADR
..
TABLE1
Assembly v5 - 18
r0, [r1]
; r0 := mem[r1]
r0, [r1,#4]
Assembly v5 - 19
; r0 := mem[r1]
; r2 := mem[r1+4]
; r5 := mem[r1+8]
Assembly v5 - 21
r9
r9
r5
r1
r0
100c 16
r9
r5
r1
r0
stack addressing:
stacks can Ascend or Descend memory
stacks can be Full or Empty
ARM multiple register transfers support all
forms of stack
101816
100016
Increment
After
101816
Before
101816
Decrement
r9
r5
r1
r0
r9
100c 16
100016
r9
After
100c16
r9
r5
r1
r0
Assembly v5 - 22
Before
100016
Assembly v5 - 20
100c16
r9
; r0 := mem8[r1]
; r0 := mem16[r1](signed)
LDMIA
; r0 := mem[r1+4]
Ascending
Full
Empty
STMIB
STMFA
STMIA
STMEA
LDMDB
LDMEA
LDMDA
LDMFA
Descending
Full
Empty
LDMIB
LDMED
LDMIA
LDMFD
STMDB
STMFD
STMDA
STMED
Standard
stack
100016
Assembly v5 - 23
Assembly v5 - 24
B LABEL
.. ; these instructions are skipped
LABEL ..
Note: data ops and loads can also change the PC!
2001 PEVEIT Unit - ARM System Design
Assembly v5 - 25
Assembly v5 - 26
Branch conditions
Conditional branches
B ran c h
B
BAL
BEQ
BNE
BPL
BMI
BCC
BLO
BCS
BHS
BVC
BVS
BGT
BGE
BLT
BLE
BHI
BLS
In t e rp re t at i o n
Unconditional
Always
Equal
Not equal
Plus
Minus
Carry clear
Lower
Carry set
Higher or same
Overflow clear
Overflow set
Greater than
Greater or equal
Less than
Less or equal
Higher
Lower or same
No rm al us e s
Always take this branch
Always take this branch
Comparison equal or zero result
Comparison not equal or non-zero result
Result positive or zero
Result minus or negative
Arithmetic operation did not give carry-out
Unsigned comparison gave lower
Arithmetic operation gave carry-out
Unsigned comparison gave higher or same
Signed integer operation; no overflow occurred
Signed integer operation; overflow occurred
Signed integer comparison gave greater than
Signed integer comparison gave greater or equal
Signed integer comparison gave less than
Signed integer comparison gave less than or equal
Unsigned comparison gave higher
Unsigned comparison gave lower or same
Assembly v5 - 28
Conditional execution
an unusual ARM feature is that all
instructions may be conditional:
CMP r0, #5
; if (r0 != 5) {
ADDNE r1, r1, r0 ;
r1 := r1 + r0 - r2
SUBNE r1, r1, r2 ; }
Assembly v5 - 29
Assembly v5 - 30
Supervisor calls
Assembly v5 - 31
Examples
SWI_Angel
START
LOOP
TEXT
Assembly v5 - 32
Examples
Assembly v5 - 33
HexOut MOV
ADR
LOOP
MOV
CMP
ADDGT
ADDLE
STR
MOV
SWI
MOV
SUBS
BNE
MOV
ChOut DCD
r3, #8
r1, ChOut
r0, r2, LSR #28
r0, #9
r0, r0, #A-10
r0, r0, #0
r0, [r1]
r0, #0x3
SWI_Angel
r2, r2, LSL #4
r3, r3, #1
LOOP
pc, r14
0
; nibble count = 8
; buffer for write char
; get top nibble
; 0-9 or A-F?
; ASCII alphabetic
; ASCII numeric
; store ASCII in buffer
; Angel write char in [r1]
; print character
; shift left one nibble
; decrement nibble count
; if more do next nibble
; ... else return
Assembly v5 - 34