Академический Документы
Профессиональный Документы
Культура Документы
The ARM processor is easy to program at the assembly level level. (It is a RISC) We will learn ARM assembly programming at the user level l l and d run it on a GBA emulator. l t
Memory system
Memory is a linear array of bytes addressed from 0 to 232-1 Word, W d h half-word, lf d b byte t Little-endian
0x00000000 0x00000001 0x00000002 0x00000003 0x00000004 0x00000005 0x00000006 0 FFFFFFFD 0xFFFFFFFD 0xFFFFFFFE 0xFFFFFFFF
00 10 20 30 FF FF FF 00 00 00
Byte ordering
Big Endian
Least significant byte has highest address Word address 0x00000000 Value: 00102030
0x00000000
R0 R4 R8 R12 R1 R5 R9 R13 R2 R6 R10 R14 R3 R7 R11 PC
00 10 20 30 FF FF FF 00 00 00
0x00000001 0x00000002 0x00000003 0 00000004 0x00000004 0x00000005 0x00000006 0xFFFFFFFD 0xFFFFFFFE 0 0xFFFFFFFF
Little Endian
Least significant byte has lowest address Word address 0x00000000 Value: 30201000
Instruction set
ARM instructions are all 32-bit 32 bit long (except for Thumb mode). mode) There are 232 possible machine instructions. Fortunately they Fortunately, are structured.
Instruction set
Data processing Data D movement Flow control
Data processing
They are move, arithmetic, logical, comparison and multiply instructions. instructions Most data processing instructions can process one of f th their i operands d using i th the b barrel l shifter. hift General rules:
All operands are 32-bit, coming from registers or literals. The result, if any, is 32-bit and placed in a register (with the exception ti for f l long g multiply lti l which produces a 64-bit result) 3-address 3 address format
Instruction set
MOV<cc><S> Rd, <operands>
Conditional execution
Almost all ARM instructions have a condition field which allows it to be executed conditionally. movcs R0, 0 R1 1
MOVCS R0, R1 @ if carry is set @ then R0:=R1 MOVS R0, #0 @ R0:=0 @ Z=1, , N=0 @ C, V unaffected
Register movement
immediate,register,shift
Addressing modes
Register operands
ADD R0 R0, R1 R1, R2
MOV MVN
R0, R2 R0, 0 R2 2
@ R0 = R2 @ R0 0 = ~R2 2
Immediate operands
a literal; most can be represented by (0..255)x22n 0<n<12
move negated t d
ADD AND
register
0
MOV
register
R0, R2, LSL #2 @ R0:=R2<<2 @ R2 unchanged g Example: 00 0011 0000 Before R2=0x00000030 R2 0x00000030 After R0=0x000000C0 R2=0x00000030
R0, R2, LSR #2 @ R0:=R2>>2 @ R2 unchanged g Example: 00 0011 0000 Before R2=0x00000030 R2 0x00000030 After R0=0x0000000C R2=0x00000030
Rotate right C
MOV
register
register
R0, R2, ROR #2 @ R0:=R2 rotate @ R2 unchanged g Example: 00 0011 0001 Before R2=0x00000031 R2 0x00000031 After R0=0x4000000C R2=0x00000031
MOV
R0, R2, ASR #2 @ R0:=R2>>2 @ R2 unchanged g Example: 1010 00 0011 0000 Before R2=0xA0000030 R2 0xA0000030 After R0=0xE800000C R2=0xA0000030
register
R0, R2, RRX
@ R0:=R2 rotate @ R2 unchanged g Example: 00 0011 0001 Before R2=0x00000031, R2 0x00000031, C=1 C 1 After R0=0x80000018, C=1 R2=0x00000031
@ fast multiply R2=35xR0 ADD R0, R0, R0, LSL #2 @ R0=5xR0 RSB R2, R0, R0, LSL #3 @ R2 =7xR0
Multiplication
MOV MUL R1, R2 R2, or ADD R0, 0 RSB R2, #35 R0 R0, R1 R0, 0 R0, 0 LSL #2 R0, R0, LSL #3 @ R0=5xR0 0 5 0 @ R2 =7xR0
Arithmetic
Add and subtraction
cond
00
opcode
Rn
Rd
operand 2
destination register first operand register set condition codes arithmetic/logic function
25 11 8 7 0
1
immediate alignment
11
# t #rot
5 4
#shift
25
Sh
Rm
Rs
register shift length
Sh
Rm
Arithmetic
ADD ADC SUB SBC RSB RSC R0, R0 R0, R0, R0, R0, R0,
-5
Arithmetic
R1, R1 R1, R1, R1, R1, R1, R2 R2 R2 R2 R2 R2 @ @ @ @ @ @ R0 R0 R0 R0 R0 R0 = = = = = = R1+R2 R1+R2+C R1 R2 C R1-R2 R1-R2-!C R2-R1 R2 R1 R2-R1-!C
3 0 0
-1 1 255
3-5=3+(-5) sum<=255 C=0 borrow 5-3=5+(-3) sum > 255 C=1 no borrow
Arithmetic
R0 R2 R2
R3 R3
Logical
Logical
AND ORR EOR BIC R0, R0 R0, R0, R0, R1, R1 R1, R1, R1, R2 R2 R2 R2 @ @ @ @ R0 R0 R0 R0 = = = = R1 R1 R1 R1 and or xor and R2 R2 R2 (~R2)
R2=0x01100101
Logical
Comparison
These instructions do not generate a result, but set condition code bits (N, (N Z Z, C C, V) in CPSR CPSR. Often, a branch operation follows to change the program flow. flow
Comparison
compare CMP R1 R1, R2 compare negated CMN R1 R1, R2 bit test TST R1 R1, R2 test equal TEQ R1 R1, R2 @ set cc on R1-R2 @ set cc on R1+R2 @ set cc on R1 and R2 @ set t cc on R1 xor R2
Comparison
Multiplication
Multiplication
MUL R0, R1, R2 @ R0 = (R1xR2)[31:0]
Features: Second S d operand d cant t b be i immediate di t The result register must be different from the first operand y depends p on core type yp Cycles If S bit is set, C flag is meaningless
Multiplication
Multiply-accumulate (2D array indexing) MLA R4 R4, R3 R3, R2 R2, R1 @ R4 = R3xR2+R1 M Multiply lti l with ith a constant t t can often ft b be more efficiently implemented using shifted register operand MOV R1, #35 MUL R2 R2, R0 R0, R1 or ADD R0, R0, R0, LSL #2 @ R0=5xR0 RSB R2, R0, R0, LSL #3 @ R2 =7xR0
Multiplication
Multiplication
Branch conditions
label:
Conditional branches
MOV loop: R0, #0 ADD R0 R0, R0 R0, #1 CMP R0, #10 BNE loop
Branches
Conditional execution
CMP BEQ ADD SUB R0, #5 b bypass pass @ if (R0! (R0!=5) 5) { R1, R1, R0 @ R1=R1+R0-R2 R1, R1, R2 @ }
sub1:
bypass:
sub2:
smaller and faster CMP R0, , #5 ADDNE R1, R1, R0 SUBNE R1 R1, R1 R1, R2
Rule of thumb: if the conditional sequence q is three instructions or less, it is better to use conditional execution than a branch.
PC PC, LR
Conditional execution
if ((R0==R1) && (R2==R3)) R4++ CMP BNE CMP BNE ADD R0, R1 skip R2, R3 skip R4, R4, #1
skip:
Addressing modes
Memory is addressed by a register and an offset.
LDR R0 R0, [R1] @ mem[R1] [R1]
mem[R1+4]
mem[R1+R2] mem[R1+4*R2] [R1+4*R2] #2]
LDR, , LDRH, , LDRB for 32, , 16, , 8 bits STR, STRH, STRB for 32, 16, 8 bits
Addressing modes
Pre-index addressing (LDR R0, [R1, #4]) without ih a writeback i b k Auto-indexing addressing (LDR R0, [R1, #4]!) Pre-index with writeback calculation before accessing with a writeback Post-index addressing (LDR R0, [R1], #4) calculation l l ti after ft accessing i with ith a writeback it b k
Pre-index addressing
LDR R0, [R1, #4] @ R0=mem[R1+4] @ R1 unchanged nchanged
R1
+ R0
Auto-indexing addressing
LDR R0, [R1, #4]! @ R0=mem[R1+4] @ R1 R1=R1+4 R1+4
No extra time; ; Fast; ;
Post-index addressing
LDR R0, R1, #4 @ R0=mem[R1] @ R1 R1=R1+4 R1+4
]!
LDR R0,[R1],
R1
+ R0
R1 +
R0
Comparisons
Pre-indexed addressing
LDR R0 R0, [R1, [R1 R2] @ R0=mem[R1+R2] @ R1 unchanged
Example
Post-indexed addressing
LDR
Example
Example
Application
loop loop: ADR R1, table table LDR R0 R0, [R1] R1 ADD R1, R1, #4 @ operations on R0 ADR R1, , table LDR R0, [R1], #4 @ operations on R0
loop:
Assembler transfer p pseudo instruction into a sequence of appropriate instructions sub r0 pc r0, pc, #12
LDMIA
Addressing modes
Rn
R1 R2 R3
R1 R2 Rn R3
Rn R1 R2 R3
10 20 30 0x10
R0
10 20 30 0x01C
R0
data 10 20 30 40 50 60
20 30 40 0x01C
R0
data 10 20 30 40 50 60
addr 0x010
data 10 20 30 40 50 60
addr 0x010
data 10 20 30 40 50 60
40 50 60 0x018
R0
30 40 50 0x018
R0
Example
Example
LDMIA
r0!, {r1-r3}
Example
Application
Copy a block of memory
R9 R9: address dd of f th the source R10: address of the destination R11: R11 end d address dd of f th the source
LDMIB
r0!, {r1-r3}
Application
Stack (full: pointing to the last used; ascending: grow towards increasing memory addresses)
POP mode Full ascending (FA) LDMFA Full descending (FD) LDMFD Empty ascending (EA) LDMEA E t d Empty descending di (ED) LDMED =LDM PUSH =STM LDMDA STMFA STMIB LDMIA STMFD STMDB LDMDB STMEA STMIA LDMIB STMED STMDA
Example
LDMFD R13! R13!, {R2 {R2-R9} R9} @ used for ATPCS @ modify R2-R9 STMFD R13!, {R2-R9}
Swap instruction
Swap between memory and register. Atomic operation preventing any other instruction from reading/writing to that location until it completes
Example
Application
Software interrupt
A software interrupt instruction causes a software interrupt exception exception, which provides a mechanism for applications to call OS routines.
Example
Load constants
No ARM instruction loads a 32-bit constant into a register because ARM instructions are 32-bit 32 bit long. There is a pseudo code for this.
Load constants
Assemblers implement this usually with two options depending on the number you try to load.
Instruction set