Professional Documents
Culture Documents
Lap Trinh He Thong Nhung Bui Quoc Bao Arm Programming (Cuuduongthancong - Com)
Lap Trinh He Thong Nhung Bui Quoc Bao Arm Programming (Cuuduongthancong - Com)
om
.c
ng
co
an
th
ng
Assembly Programming
o
du
om
.c
ng
co
an
th
o ng
du
MY_NUMBER
u
DCD 0x12345678
cu
HELLO_TXT
DCB “Hello\n”,0
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]
om
.c
ng
co
an
th
ng
om
used with the conditional execution
suffixes if they are inside an IF-THEN
.c
instruction block
ng
co
an
th
o ng
du
om
instructions.
.c
ng
co
an
th
ng
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)
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
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
Operations are:
cu
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
om
Ex:
LDR R0,=0x42
.c
ng
co
an
th
ng
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
om
.c
ng
co
an
th
o ng
du
0x8000:
om
.c
ng
co
an
th
ng
Post-index addressing
o
du
u
cu
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
LDR R0,[R1],R2,LSL # 2
om
.c
ng
co
an
th
ng
Branch instruction
o
du
u
cu
om
PC from LR:
MOV PC,LR
.c
Another instruction to return is: BX LR
ng
co
an
th
ng
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
om
instr4<cond or not cond> <operands> ; 4th instruction (can be
; <cond> or <!cond>
.c
ng
co
an
th
ng
if (R1<R2) then
R2=R2-R1 and R2=R2/2
else
u
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
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