You are on page 1of 18

ARM PROGRAMMING

Bùi Quốc Bảo

om
.c
ng
co
an
th
ng

Assembly Programming
o
du

AREA subrout, CODE, READONLY


; Name this block of code
u

ENTRY ; Mark first instruction to execute


cu

start MOV r0, #10 ; Set up parameters


MOV r1, #3
BL doadd ; Call subroutine
stop MOV r0, #0x18 ; angel_SWIreason_ReportException
LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit
SVC #0x123456 ; ARM semihosting (formerly SWI)
doadd ADD r0, r0, r1 ; Subroutine code
BX lr ; Return from subroutine
END ; Mark end of file

BÙI QUỐC BẢO


1
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Define constant
NVIC_IRQ_SETEN0 EQU 0xE000E100
NVIC_IRQ0_ENABLE EQU 0x1
LDR R0,=NVIC_IRQ_SETEN0
MOV R1,#NVIC_IRQ0_ENABLE
STR R1, [R0]

om
.c
ng
co
an
th
o ng
du

MY_NUMBER
u

DCD 0x12345678
cu

HELLO_TXT
DCB “Hello\n”,0

BÙI QUỐC BẢO


2
CuuDuongThanCong.com https://fb.com/tailieudientucntt
{label} {instruction|directive|pseudo-instruction} {;comment}

Label: identify the location of program counter, can not


start by a number
Comment: any thing that follow a semicolon “;” “@” or C
style comment (/* */)

om
.c
ng
co
an
th
ng

Addressing mode
o
du

 MOV R0,#1234
u

 MOV R0,R1
cu

 LDR R0,[R1]
 LDR R0,[R1,#4]
 LDR R0,[R1,R2]

BÙI QUỐC BẢO


3
CuuDuongThanCong.com https://fb.com/tailieudientucntt
The condition

om
.c
ng
co
an
th
ng

Instruction with condition


o
du

 All instruction contain a


u

condition field which determines


cu

whether the CPU will execute


them
 ADD R0, R1,R2 ;R0 =R1+R2
 ADDEQ R0,R1,R2 ;R0 = R1+R2 if zero flag is set

BÙI QUỐC BẢO


4
CuuDuongThanCong.com https://fb.com/tailieudientucntt
 For ARM Cortex M3, the conditional
execution suffixes are usually used for
branch instructions.
 However, other instructions can also be

om
used with the conditional execution
suffixes if they are inside an IF-THEN

.c
instruction block

ng
co
an
th
o ng
du

By default, data processing operations


u

do not affect the condition flags (apart from


cu

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”.
ADD R0,R1,R2 ;R0=R1+R2, flag will not affected
ADDS R0,R1,R2 ;R0=R1+R2, flag will be affected

BÙI QUỐC BẢO


5
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Thumb2 instruction set

 Thumb-2 is a superset of the Thumb


instruction set
 Thumb-2 introduces 32-bit instructions
that are intermixed with the 16-bit

om
instructions.

.c
ng
co
an
th
ng

Thumb2 instruction set


o
du

 Some of the operations can be handled by


u

either a Thumb instruction or a Thumb-2


cu

instruction
 ADDS R0, #1 ; Use 16-bit Thumb instruction
;by default
 ADDS.N R0, #1 ; Use 16-bit Thumb
;instruction (N=Narrow)
 ADDS.W R0, #1 ; Use 32-bit Thumb-2
;instruction (W=wide)

BÙI QUỐC BẢO


6
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Data Processing Instruction
 All sharing the same instruction format.
 Contains:
 • Arithmetic operations
 • Comparisons (no results - just set condition codes)
 • Logical operations
 • Data movement between registers

om
 Remember, this is a load / store architecture
 These instruction only work on registers, NOT memory.
 They each perform a specific operation on one or two

.c
operands.
 First operand always a register - Rn
Second operand sent to the ALU via barrel shifter.

ng


co
an
th
ng

Arithmetic
o
du

 Operations are:
 ADD operand1 + operand2
u

 ADC operand1 + operand2 + carry


 SUB operand1 - operand2
cu

 SBC operand1 - operand2 + carry -1


 RSB operand2 - operand1
 RSC operand2 - operand1 + carry – 1
 MUL operand1 * operand2
 UDIV operand1 / operand2 (unsigned)
 SDIV operand1 / operand2 (signed)
 Syntax:
<Operation>{<cond>}{S} Rd, Rn, Operand2
 Examples:
ADD r0, r1
SUB.N r3, #1
SUBS.W r3, r3, #1

BÙI QUỐC BẢO


7
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Using the suffix

 When 16-bit Thumb code is used, an ADD


instruction changes the flags in the PSR.
 32-bit Thumb-2 code can either change a flag
or keep it unchanged.
 To separate the two different operations, the S

om
suffix should be used if the following operation
depends on the flags:

.c
ADD.W R0, R1, R2 ; Flag unchanged
ADDS.W R0, R1, R2 ; Flag change

ng
co
an
th
ng

Comparisons
o
du

 The only effect of the comparisons is to UPDATE


THE CONDITION FLAGS. Thus no need to set S bit.
u

 Operations are:
cu

• CMP operand1 - operand2, but result not written


• CMN operand1 + operand2 (signed), but result not written
• TST operand1 AND operand2, but result not written
• TEQ operand1 EOR operand2, but result not written
 Syntax:
 <Operation>{<cond>} Rn, Operand2
 Examples:
• CMP r0, r1
• TSTEQ r2, #5

BÙI QUỐC BẢO


8
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Logical Operations

 Operations are:
AND operand1 AND operand2
EOR operand1 EOR operand2
ORR operand1 OR operand2
BIC operand1 AND NOT operand2 [ie bit clear]
 Syntax:

om
<Operation>{<cond>}{S} Rd, Rn,operand2
 Examples:

.c
AND r0, r1, r2
BICEQ r2, r3, #7

ng
EORS r1,r3,r0
co
an
th
ng

Data Movement
o
du

 Operations are:
u

 MOV{S}{cond} Rd, Operand2


cu

 MOV{cond} Rd, #imm16


 MVN{S}{cond} Rd, Operand2
Examples:
• MOV r0, r1
• MOVS r2, #10
• MVNEQ r1,#0

BÙI QUỐC BẢO


9
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Loading full 32 bit

 Instead of MOV, this instruction is


prefered:
 LDR Rd, =numeric constant

om
Ex:
LDR R0,=0x42

.c
ng
co
an
th
ng

Commonly Used Memory Access


Instructions
o
du

LDRB Rd, [Rn, #offset] Read byte from memory location Rn+ offset
u

LDRH Rd, [Rn, #offset] Read half-word from memory location Rn+
offset
cu

LDR Rd, [Rn, #offset] Read word from memory location Rn+offset
LDRD Rd1,Rd2, [Rn, #offset] Read double word from memory location
Rn+offset
STRB Rd, [Rn, #offset] Store byte to memory location Rn+offset
STRH Rd, [Rn, #offset] Store half word to memory location
Rn+offset
STR Rd, [Rn, #offset] Store word to memory location Rn+offset
STRD Rd1,Rd2, [Rn, #offset] Store double word to memory location
Rn+offset

BÙI QUỐC BẢO


10
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Multiple Memory Access Instructions

om
.c
ng
co
an
th
o ng
du

The exclamation mark (!) in the instruction specifi es


whether the register Rd should be updated after the
u

instruction is completed. For example, if R8 equals


cu

0x8000:

STMIA.W R8!, {R0-R3} ; R8 changed to 0x8010 after store


; (increment by 4 words)
STMIA.W R8 , {R0-R3} ; R8 unchanged after store

BÙI QUỐC BẢO


11
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Pre-index Addressing

om
.c
ng
co
an
th
ng

Post-index addressing
o
du
u
cu

BÙI QUỐC BẢO


12
CuuDuongThanCong.com https://fb.com/tailieudientucntt
om
.c
ng
co
an
th
ng

Pre-index addressing
o
du

R0  [R1+4]
 LDR R0,[R1,#4]! R1 = R1 + 4
u

 LDR R0,[R1,R2]
cu

 LDR R0,[R1,R2,LSL # 2]!

BÙI QUỐC BẢO


13
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Post-index addressing
R0  [R1]
 LDR R0, [R1], #4 R1 = R1 + 4

 LDR R0,[R1],R2
 LDR R0,[R1],R2,LSL # 2

om
.c
ng
co
an
th
ng

Branch instruction
o
du
u
cu

BÙI QUỐC BẢO


14
CuuDuongThanCong.com https://fb.com/tailieudientucntt
 The BL and BLX command will save
return address into the LR of current
bank
 To return from subroutine, restore the

om
PC from LR:
 MOV PC,LR

.c
 Another instruction to return is: BX LR

ng
co
an
th
ng

Nested function call


o
du

main
...
BL functionA
u

...
cu

functionA
PUSH {LR} ; Save LR content to stack
...
BL functionB
...
POP {PC} ; Use stacked LR content to return to main
functionB
PUSH {LR}
...
POP {PC} ; Use stacked LR content to return to functionA

BÙI QUỐC BẢO


15
CuuDuongThanCong.com https://fb.com/tailieudientucntt
If-then instruction (IT block)
IT<x><y><z> <cond> ; IT instruction (<x>, <y>,
; <z> can be T or E)
instr1<cond> <operands> ; 1st instruction (<cond>
; must be same as IT)
instr2<cond or not cond> <operands> ; 2nd instruction (can be
; <cond> or <!cond>
instr3<cond or not cond> <operands> ; 3rd instruction (can be
; <cond> or <!cond>

om
instr4<cond or not cond> <operands> ; 4th instruction (can be
; <cond> or <!cond>

.c
ng
co
an
th
ng

If-then instruction (IT block)


o
du

if (R1<R2) then
R2=R2-R1 and R2=R2/2
else
u

R1=R1-R2 and R1=R1/2


cu

CMP R1, R2 ; If R1 < R2 (less then)


ITTEE LT ; then execute instruction 1 and 2
; (indicated by T)
; else execute instruction 3 and 4
; (indicated by E)
SUBLT.W R2,R1 ; 1st instruction
LSRLT.W R2,#1 ; 2nd instruction
SUBGE.W R1,R2 ; 3rd instruction (notice the GE is
; opposite of LT)
LSRGE.W R1,#1 ; 4th instruction

BÙI QUỐC BẢO


16
CuuDuongThanCong.com https://fb.com/tailieudientucntt
PUSH and POP
PUSH {R0, R4-R7, R9} ; Push R0, R4, R5, R6, R7, R9 into
; stack memory
POP {R2,R3} ; Pop R2 and R3 from stack

Use POP as function return:

om
PUSH {R0-R3, LR} ; Save register contents at beginning of
; subroutine
.... ; Processing

.c
POP {R0-R3, PC} ; restore registers and return

ng
co
an
th
ng

Special register
o
du

 Program Status Registers (PSRs)


u

 Application PSR (APSR)


cu

 Interrupt PSR (IPSR)


 Execution PSR (EPSR)

 Interrupt Mask Registers


 PRIMASK
 FAULTMASK
 BASEPRI)

 Control Register (Control)

BÙI QUỐC BẢO


17
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Access the special registers
 To access the special register, one can use MRS and
MSR command:
MRS r0, APSR ; Read Flag state into R0
MRS r0, IPSR ; Read Exception/Interrupt state
MRS r0, EPSR ; Read Execution state
MSR APSR, r0 ; Write Flag state
MRS r0, PSR ; Read the combined program status word
MSR PSR, r0 ; Write combined program state word

om
MRS r0, CONTROL ; Read CONTROL register into R0
MSR CONTROL, r0 ; Write R0 into CONTROL register
MRS r0, BASEPRI ; Read BASEPRI register into R0

.c
MRS r0, PRIMASK ; Read PRIMASK register into R0
MRS r0, FAULTMASK ; Read FAULTMASK register into R0
MSR BASEPRI, r0 ; Write R0 into BASEPRI register

ng
MSR PRIMASK, r0 ; Write R0 into PRIMASK register
MSR FAULTMASK, r0 ; Write R0 into FAULTMASK register
co
an
th
ng

Block Data Transfer


o
du
u
cu

BÙI QUỐC BẢO


18
CuuDuongThanCong.com https://fb.com/tailieudientucntt

You might also like