The ARM Instruction Set: Advanced RISC Machines
The ARM Instruction Set: Advanced RISC Machines
The ARM Instruction Set: Advanced RISC Machines
Processor Modes
* The ARM has six operating modes: User (unprivileged mode under which most tasks run) FIQ (entered when a high priority (fast) interrupt is raised) IRQ (entered when a low priority (normal) interrupt is raised) Supervisor (entered on reset and when a Software Interrupt instruction is executed) Abort (used to handle memory access violations) Undef (used to handle undefined instructions) * ARM Architecture Version 4 adds a seventh mode: System (privileged mode using the same registers as user mode)
The Registers
* ARM has 37 registers in total, all of which are 32-bits long. 1 dedicated program counter 1 dedicated current program status register 5 dedicated saved program status registers 30 general purpose registers * However these are arranged into several banks, with the accessible bank being governed by the processor mode. Each mode can access a particular set of r0-r12 registers a particular r13 (the stack pointer) and r14 (link register) r15 (the program counter) cpsr (the current program status register) and privileged modes can also access a particular spsr (saved program status register)
The ARM Instruction Set - ARM University Program - V1.0 4
Register Organisation
General registers and Program Counter
User32 / System r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) FIQ32 r0 r1 r2 r3 r4 r5 r6 r7 r8_fiq r9_fiq r10_fiq r11_fiq r12_fiq r13_fiq r14_fiq r15 (pc) Supervisor32 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13_svc r14_svc r15 (pc) Abort32 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13_abt r14_abt r15 (pc) IRQ32 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13_irq r14_irq r15 (pc) Undefined32 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13_undef r14_undef r15 (pc)
User Mode
r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 (sp) r14 (lr) r15 (pc) cpsr r8_fiq r9_fiq r10_fiq r11_fiq r12_fiq r13_fiq r14_fiq
FIQ Mode
r0 r1 r2 r3 r4 r5 r6
EXCEPTION
Return address calculated from User mode PC value and stored in FIQ mode LR
spsr_fiq
cpsr spsr_fiq
N Z CV
I F T
Mode
Copies of the ALU status flags (latched if the instruction has the "S" bit set).
Condition Code Flags N = Negative result from ALU flag. Z = Zero result from ALU flag. C = ALU operation Carried out V = ALU operation oVerflowed Mode Bits M[4:0] define the processor mode.
Interrupt Disable bits. I = 1, disables the IRQ. F = 1, disables the FIQ. T Bit (Architecture v4T only) T = 0, Processor in ARM state T = 1, Processor in Thumb state
Condition Flags
Logical Instruction Flag Negative (N=1) No meaning Bit 31 of the result has been set Indicates a negative number in signed operations Arithmetic Instruction
Zero (Z=1)
Carry (C=1)
oVerflow (V=1)
No meaning
Result was greater than 31 bits Indicates a possible corruption of the sign bit in signed numbers
9
10
Copies CPSR into SPSR_<mode> Sets appropriate CPSR bits If core implements ARM Architecture 4T and is currently in Thumb state, then ARM state is entered. Mode field bits Interrupt disable flags if appropriate. Maps in appropriate banked registers Stores the return address in LR_<mode> Sets PC to vector address
Reset Undefined Instruction Software Interrupt Prefetch Abort Data Abort Reserved IRQ FIQ
PC - 4
DECODE
PC - 8
EXECUTE
Register(s) read from Register Bank Shift and ALU operation Write register(s) back to Register Bank
* Rather than pointing to the instruction being executed, the PC points to the instruction being fetched.
The ARM Instruction Set - ARM University Program - V1.0 12
Instruction type
Data processing / PSR Transfer
Multiply Long Multiply Swap Load/Store Byte/Word Load/Store Multiple
Halfword transfer : Immediate offset (v4 only) Halfword transfer: Register offset (v4 only)
Cond Cond Cond Cond Cond Cond Cond Cond Cond Cond Cond Cond Cond Cond
0 0 I Opcode
Rn Rd RdHi Rn Rn Rn Rn Rn
Rd Rn RdLo Rd Rd Rs Rs
Operand2 1 0 0 1 1 0 0 1 Rm Rm Rm
0 0 0 0 0 0 A S 0 0 0 0 1 U A S 0 0 0 1 0 B 0 0 0 1 I P U B W L 1 0 0 P U S W L 0 0 0 P U 1 W L 0 0 0 P U 0 W L
(v3M / v4 only)
Rd Rd
Offset1 1 S H 1 Offset2 0 0 0 0 1 S H 1 Rm
1 0 1 L 0 0 0 1
Offset 0 0 1 0 1 1 1 1 1 1 1 1 Rn CRn L CRn CRd CRd Rd 1 1 1 1 0 0 0 1 CPNum CPNum CPNum Op2 Op2 Offset 0 1 CRm CRm Rn
1 1 0 P U N W L 1 1 1 0 1 1 1 0 1 1 1 1 Op1 Op1
Coprocessor data transfer Coprocessor data operation Coprocessor register transfer Software interrupt
SWI Number
13
Conditional Execution
* Most instruction sets only allow branches to be executed conditionally. * However by reusing the condition evaluation hardware, ARM effectively increases number of instructions. All instructions contain a condition field which determines whether the CPU will execute them. Non-executed instructions soak up 1 cycle. Still have to complete cycle so as to allow fetching and decoding of following instructions. * This removes the need for many branches, which stall the pipeline (3 cycles to refill). Allows very dense in-line code, without branches. The Time penalty of not executing several conditional instructions is frequently less than overhead of the branch or subroutine call that would otherwise be needed.
The ARM Instruction Set - ARM University Program - V1.0 14
Cond
0000 = EQ - Z set (equal) 0001 = NE - Z clear (not equal) 0010 = HS / CS - C set (unsigned higher or same) 0011 = LO / CC - C clear (unsigned lower) 0100 = MI -N set (negative)
1001 = LS - C clear or Z (set unsigned lower or same) 1010 = GE - N set and V set, or N clear and V clear (>or =)
15
Cond
1 L
Offset
Link bit
Condition field * The offset for branch instructions is calculated by the assembler: By taking the difference between the branch instruction and the target address minus 8 (to allow for the pipeline). This gives a 26 bit offset which is right shifted 2 bits (as the bottom two bits are always zero as instructions are word aligned) and stored into the instruction encoding. This gives a range of 32 Mbytes.
The ARM Instruction Set - ARM University Program - V1.0 17
Arithmetic Operations
* Operations are: ADD operand1 + operand2 ADC operand1 + operand2 + carry SUB operand1 - operand2 SBC operand1 - operand2 + carry -1 RSB operand2 - operand1 RSC operand2 - operand1 + carry - 1 * Syntax: <Operation>{<cond>}{S} Rd, Rn, Operand2 * Examples ADD r0, r1, r2 SUBGT r3, r3, #1 RSBLES r4, r5, #5
The ARM Instruction Set - ARM University Program - V1.0 20
Comparisons
* The only effect of the comparisons is to UPDATE THE CONDITION FLAGS. Thus no need to set S bit. * Operations are: CMP operand1 - operand2, but result not written CMN operand1 + operand2, 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
21
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: <Operation>{<cond>}{S} Rd, Rn, Operand2 * Examples: AND r0, r1, r2 BICEQ r2, r3, #7 EORS r1,r3,r0
22
Data Movement
* Operations are: MOV operand2 MVN NOT operand2 Note that these make no use of operand1. * Syntax: <Operation>{<cond>}{S} Rd, Operand2 * Examples: MOV r0, r1 MOVS r2, #10 MVNEQ r1,#0
23
24
CF
Destination
25
Destination
CF
Destination
Sign bit shifted in
CF
Destination
CF
Destination
CF
27
Barrel Shifter
ALU
Result
The ARM Instruction Set - ARM University Program - V1.0 28
31
* These can be loaded using, for example: MOV r0, #0x40, 26 ; => MOV r0, #0x1000 (ie 4096) * To make this easier, the assembler will convert to this form for us if simply given the required constant: MOV r0, #4096 ; => MOV r0, #0x1000 (ie 0x40 ror 26) * The bitwise complements can also be formed using MVN: MOV r0, #0xFFFFFFFF ; assembles to MVN r0, #0 * If the required constant cannot be generated, an error will be reported.
The ARM Instruction Set - ARM University Program - V1.0 32
* As this mechanism will always generate the best instruction for a given case, it is the recommended way of loading constants.
33
Multiplication Instructions
* The Basic ARM provides two multiplication instructions. * Multiply MUL{<cond>}{S} Rd, Rm, Rs ; Rd = Rm * Rs * Multiply Accumulate - does addition for free MLA{<cond>}{S} Rd, Rm, Rs,Rn ; Rd = (Rm * Rs) + Rn * Restrictions on use: Rd and Rm cannot be the same register Can be avoid by swapping Rm and Rs around. This works because multiplication is commutative. Cannot use PC. These will be picked up by the assembler if overlooked. * Operands can be considered signed or unsigned Up to user to interpret correctly.
The ARM Instruction Set - ARM University Program - V1.0 34
Multiplication Implementation
* The ARM makes use of Booths Algorithm to perform integer multiplication. * On non-M ARMs this operates on 2 bits of Rs at a time. For each pair of bits this takes 1 cycle (plus 1 cycle to start with). However when there are no more 1s left in Rs, the multiplication will early-terminate. * Example: Multiply 18 and -1 : Rd = Rm * Rs
Rm Rs 17 cycles 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 18 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 Rs Rm 4 cycles
* Note: Compiler does not use early termination criteria to decide on which order to place operands.
The ARM Instruction Set - ARM University Program - V1.0 35
r2 0x5
0x5
40
Load and Store Word or Byte: Offsets from the Base Register
* As well as accessing the actual location contained in the base register, these instructions can access a location offset from the base register pointer. * This offset can be An unsigned 12bit immediate value (ie 0 - 4095 bytes). A register, optionally shifted by an immediate value * This can be either added or subtracted from the base register: Prefix the offset value or register with + (default) or -. * This offset can be applied: before the transfer is made: Pre-indexed addressing optionally auto-incrementing the base register, by postfixing the instruction with an !. after the transfer is made: Post-indexed addressing causing the base register to be auto-incremented.
The ARM Instruction Set - ARM University Program - V1.0 41
Memory
r0 0x5
0x5
* To store to location 0x1f4 instead use: STR r0, [r1,#-12] * To auto-increment base pointer to 0x20c use: STR r0, [r1, #12]! * If r2 contains 3, access 0x20c by multiplying this by 4: STR r0, [r1, r2, LSL #2]
The ARM Instruction Set - ARM University Program - V1.0 42
Memory r0 0x5
r1 0x200
0x200
0x5
* To auto-increment the base register to location 0x1f4 instead use: STR r0, [r1], #-12 * If r2 contains 3, auto-incremenet base register to 0x20c by multiplying this by 4: STR r0, [r1], r2, LSL #2
43
* When used in a privileged mode, this does the load/store with user mode privilege. Normally used by an exception handler that is emulating a memory access instruction that would normally execute in user mode.
44
8 4 0
45
46
Effect of endianess
* The ARM can be set up to access its data in either little or big endian format. * Little endian: Least significant byte of a word is stored in bits 0-7 of an addressed word. * Big endian: Least significant byte of a word is stored in bits 24-31 of an addressed word. * This has no real relevance unless data is stored as words and then accessed in smaller sized quantities (halfwords or bytes). Which byte / halfword is accessed will depend on the endianess of the system involved.
47
Endianess Example
r0 = 0x11223344
31 24 23 16 15 87 0
11
22
33
44
31
24 23
16 15
87
31
24 23
16 15
87
r1 = 0x100
11
22
33
44
Memory
LDRB r2, [r1]
44
33
22
11
r1 = 0x100
Little-endian
31 24 23 16 15 87 0
Big-endian
31 24 23 16 15 87 0
00
00
00
44
00
00
00
11
r2 = 0x44
The ARM Instruction Set - ARM University Program - V1.0
r2 = 0x11
48
Cond
0 0 P U S W L
Rn
Register list
Condition field
Up/Down bit
0 = Down; subtract offset from base 1 = Up ; add offset to base
Base register
Load/Store bit
0 = Store to memory 1 = Load from memory
0 = dont load PSR or force user mode 1 = load PSR or force user mode
49
50
Stacks
* A stack is an area of memory which grows as new data is pushed onto the top of it, and shrinks as data is popped off the top. * Two pointers define the current limits of the stack. A base pointer used to point to the bottom of the stack (the first location). A stack pointer used to point the current top of the stack.
PUSH {1,2,3}
SP 3 2 SP BASE 1 BASE BASE SP 2 1
POP
Result of pop = 3
51
Stack Operation
* Traditionally, a stack grows down in memory, with the last pushed value at the lowest address. The ARM also supports ascending stacks, where the stack structure grows up through memory. * The value of the stack pointer can either: Point to the last occupied address (Full stack) and so needs pre-decrementing (ie before the push) Point to the next occupied address (Empty stack) and so needs post-decrementing (ie after the push) * The stack type to be used is given by the postfix to the instruction: STMFD / LDMFD : Full Descending stack STMFA / LDMFA : Full Ascending stack. STMED / LDMED : Empty Descending stack STMEA / LDMEA : Empty Ascending stack * Note: ARM Compiler will always use a Full descending stack.
The ARM Instruction Set - ARM University Program - V1.0 52
Stack Examples
STMFD sp!, {r0,r1,r3-r5} STMED sp!, {r0,r1,r3-r5} STMFA sp!, {r0,r1,r3-r5} STMEA sp!, {r0,r1,r3-r5}
0x418
SP r5 r4 r3 r1 r0 SP r5 r4 r3 r1 r0
Old SP
Old SP
r5 r4 r3 r1 SP r0 SP
r5 r4 r3 r1 r0
Old SP
Old SP
0x400
0x3e8
53
* See the chapter on the ARM Procedure Call Standard in the SDT Reference Manual for further details of register usage within subroutines. * If the pop instruction also had the S bit set (using ^) then the transfer of the PC when in a priviledged mode would also cause the SPSR to be copied into the CPSR (see exception handling module).
The ARM Instruction Set - ARM University Program - V1.0 54
55
Increasing Memory
r12
56
Rn
Memory
Rm Rd
* Thus to implement an actual swap of contents make Rd = Rm. * The compiler cannot produce this instruction.
The ARM Instruction Set - ARM University Program - V1.0 57
Cond
1 1 1
Condition Field * In effect, a SWI is a user-defined instruction. * It causes an exception trap to the SWI hardware vector (thus causing a change to supervisor mode, plus the associated state saving), thus causing the SWI exception handler to be called. * The handler can then examine the comment field of the instruction to decide what operation has been requested. * By making use of the SWI mechansim, an operating system can implement a set of privileged operations which applications running in user mode can request. * See Exception Handling Module for further details.
The ARM Instruction Set - ARM University Program - V1.0 58