




















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















































This chapter describes the ARM Processor instruction set. 5.1 Instruction Set Summary 5- 5.2 The Condition Field 5- 5.3 Branch and Branch with link (B, BL) 5- 5.4 Data Processing 5- 5.5 PSR Transfer (MRS, MSR) 5- 5.6 Multiply and Multiply-Accumulate (MUL, MLA) 5- 5.7 Single Data Transfer (LDR, STR) 5- 5.8 Block Data Transfer (LDM, STM) 5- 5.9 Single Data Swap (SWP) 5- 5.10 Software Interrupt (SWI) 5- 5.11 Coprocessor Instructions 5- 5.12 Coprocessor data operations (CDP) 5- 5.13 Coprocessor Data Transfers (LDC, STC) 5- 5.14 Coprocessor Register Transfers (MRC, MCR) 5- 5.15 Undefined Instruction 5- 5.16 Instruction Set Examples 5- 5.17 Instruction Speed Summary 5-
ARM Processor Instruction Set - Summary
Preliminary
A summary of the ARM Processor instruction set is shown in òFigure 5-1: Instruction set summary. Note Some instruction codes are not defined but do not cause the Undefined instruction trap to be taken, for instance a Multiply instruction with bit 6 changed to a 1. These instructions must not be used, as their action may change in future ARM implementations.
Figure 5-1: Instruction set summary
31 28 27 24 23 20 19 16 15 12 11 8 7 5 4 3 0 Cond 0 0 Opcode
21 S Rn Rd Operand 2 Data Processing PSR Transfer Multiply
Single Data Swap
Single Data Transfer
Undefined
Block Data Transfer
Coproc Data Transfer
Branch
Coproc Data Operation
Coproc Register Transfer
Software Interrupt
26 25 22 I
Cond
Cond
Cond
Cond
Cond
Cond
Cond
Cond
Cond
Cond
0 0 0 0 0 0 A S Rd Rn Rs 1 0 0 1 Rm
0 0 0 1 0 B 0 0 Rn Rd 0 0 0 0 1 0 0 1 Rm
0 1 I P^ U B^ W^ L Rn Rd offset
0 1 1 XXXXXXXXXXXXXXXXXXXX 1 XXXX
1 0 0 P U S W L Rn Register List
1 0 1 L
1 1 0
offset
1 1 1 0 0 CRm
1 1 1 0 CP Opc L
P U N W L Rn CRd CP# offset
1 1 1 1
CP Opc CRn CRd
CRn Rd
CP#
CP#
CP
CP 1 CRm
ignored by processor
ARM Processor Instruction Set - B, BL
Preliminary
The instruction is only executed if the condition is true. The various conditions are defined at the beginning of this chapter. The instruction encoding is shown in ò Figure 5-3: Branch instructions on page 5-4. Branch instructions contain a signed 2's complement 24-bit offset. This is shifted left two bits, sign extended to 32-bits, and added to the PC. The instruction can therefore specify a branch of +/- 32Mbytes. The branch offset must take account of the prefetch operation, which causes the PC to be 2 words (8 bytes) ahead of the current instruction.
Figure 5-3: Branch instructions Branches beyond +/- 32Mbytes must use an offset or absolute destination which has been previously loaded into a register. In this case the PC should be manually saved in R14 if a Branch with Link type operation is required.
5.3.1 The link bit
Branch with Link (BL) writes the old PC into the link register (R14) of the current bank. The PC value written into R14 is adjusted to allow for the prefetch, and contains the address of the instruction following the branch and link instruction. Note that the CPSR is not saved with the PC. To return from a routine called by Branch with Link use MOV PC,R14 if the link register is still valid or LDM Rn!,{..PC} if the link register has been saved onto a stack pointed to by Rn.
5.3.2 Instruction cycle times
Branch and Branch with Link instructions take 3 instruction fetches. For more information see ò 5.17 Instruction Speed Summary on page 5-52.
Cond 101 L offset
31 28 27 25 24 23 0
Link bit 0 = Branch 1 = Branch with Link Condition field
ARM Processor Instruction Set - B, BL
Preliminary
5.3.3 Assembler syntax
B{L}{cond}
{L} is used to request the Branch with Link form of the instruction. If absent, R14 will not be affected by the instruction.
{cond} is a two-character mnemonic as shown in òFigure 5-2: Condition codes on page 5-3 (EQ, NE, VS etc). If absent then AL (ALways) will be used.
5.3.4 Examples
here BAL here ; assembles to 0xEAFFFFFE (note effect B there of PC offset) ALways condition used as default
CMP R1,#0 ; compare R1 with zero and branch to fred BEQ fred if R1 was zero otherwise continue to ; next instruction
BL sub+ROM ; call subroutine at computed address
ADDS R1,#1 ; add 1 to register 1, setting CPSR flags BLCC sub ; on the result then call subroutine if ; the C flag is clear,which will be the ; case unless R1 held 0xFFFFFFFF
ARM Processor Instruction Set - Data
Preliminary
Figure 5-4: Data processing instructions
5.4.1 CPSR flags
The data processing operations may be classified as logical or arithmetic. The logical operations (AND, EOR, TST, TEQ, ORR, MOV, BIC, MVN) perform the logical action on all corresponding bits of the operand or operands to produce the result. If the S bit is set (and Rd is not R15, see below) the V flag in the CPSR will be unaffected, the C flag will be set to the carry out from the barrel shifter (or preserved when the shift operation is LSL #0), the Z flag will be set if and only if the result is all zeros, and the N flag will be set to the logical value of bit 31 of the result.
Cond 00 I OpCode (^) Rn Rd Operand 2
31 282726 2524 2120 19 16 15 1211 0
Destination register 1st operand register Set condition codes
Operation Code
0 = do not alter condition codes 1 = set condition codes
0000 = AND - Rd:= Op1 AND Op 0010 = SUB - Rd:= Op1 - Op 0011 = RSB - Rd:= Op2 - Op 0100 = ADD - Rd:= Op1 + Op 0101 = ADC - Rd:= Op1 + Op2 + C 0110 = SBC - Rd:= Op1 - Op2 + C 0111 = RSC - Rd:= Op2 - Op1 + C 1000 = TST - set condition codes on Op1 AND Op 1001 = TEQ - set condition codes on Op1 EOR Op 1010 = CMP - set condition codes on Op1 - Op21011 = CMN - set condition codes on Op1 + Op 1100 = ORR - Rd:= Op1 OR Op 1101 = MOV - Rd:= Op 1110 = BIC - Rd:= Op1 AND NOT Op 1111 = MVN - Rd:= NOT Op Immediate Operand 0 = operand 2 is a register
1 = operand 2 is an immediate value
Shift Rm
Rotate
S
Unsigned 8 bit immediate value
2nd operand register shift applied to Rm
shift applied to Imm
Imm
Condition field
11 8 7 0
11 4 3 0
0001 = EOR - Rd:= Op1 EOR Op
ARM Processor Instruction Set - Data
Preliminary The arithmetic operations (SUB, RSB, ADD, ADC, SBC, RSC, CMP, CMN) treat each operand as a 32-bit integer (either unsigned or 2's complement signed, the two are equivalent). If the S bit is set (and Rd is not R15) the V flag in the CPSR will be set if an overflow occurs into bit 31 of the result; this may be ignored if the operands were considered unsigned, but warns of a possible error if the operands were 2's complement signed. The C flag will be set to the carry out of bit 31 of the ALU, the Z flag will be set if and only if the result was zero, and the N flag will be set to the value of bit 31 of the result (indicating a negative result if the operands are considered to be 2's complement signed).
Assembler Mnemonic
OpCode Action
AND 0000 operand1 AND operand
EOR 0001 operand1 EOR operand
SUB 0010 operand1 - operand
RSB 0011 operand2 - operand
ADD 0100 operand1 + operand
ADC 0101 operand1 + operand2 + carry
SBC 0110 operand1 - operand2 + carry - 1
RSC 0111 operand2 - operand1 + carry - 1
TST 1000 as AND, but result is not written
TEQ 1001 as EOR, but result is not written
CMP 1010 as SUB, but result is not written
CMN 1011 as ADD, but result is not written
ORR 1100 operand1 OR operand
MOV 1101 operand2 (operand1 is ignored)
BIC 1110 operand1 AND NOT operand2 (Bit clear)
MVN 1111 NOT operand2 (operand1 is ignored)
Table 5-1: ARM data processing instructions
ARM Processor Instruction Set - Shifts
Preliminary
A logical shift right (LSR) is similar, but the contents of Rm are moved to less significant positions in the result. LSR #5 has the effect shown in ò Figure 5-7: Logical shift right on page 5-10.
Figure 5-7: Logical shift right The form of the shift field which might be expected to correspond to LSR #0 is used to encode LSR #32, which has a zero result with bit 31 of Rm as the carry output. Logical shift right zero is redundant as it is the same as logical shift left zero, so the assembler will convert LSR #0 (and ASR #0 and ROR #0) into LSL #0, and allow LSR #32 to be specified. An arithmetic shift right (ASR) is similar to logical shift right, except that the high bits are filled with bit 31 of Rm instead of zeros. This preserves the sign in 2's complement notation. For example, ASR #5 is shown in ò Figure 5-8: Arithmetic shift right.
Figure 5-8: Arithmetic shift right The form of the shift field which might be expected to give ASR #0 is used to encode ASR #32. Bit 31 of Rm is again used as the carry output, and each bit of operand 2 is also equal to bit 31 of Rm. The result is therefore all ones or all zeros, according to the value of bit 31 of Rm. Rotate right (ROR) operations reuse the bits which 'overshoot' in a logical shift right operation by reintroducing them at the high end of the result, in place of the zeros used to fill the high end in logical right operations. For example, ROR #5 is shown in òFigure 5-9: Rotate right.
contents of Rm
value of operand 2
31 0
carry out
0 0 0 0 0
5 4
contents of Rm
value of operand 2
31 0
carry out
30 5 4
ARM Processor Instruction Set - Shifts
Preliminary
Figure 5-9: Rotate right The form of the shift field which might be expected to give ROR #0 is used to encode a special function of the barrel shifter, rotate right extended (RRX). This is a rotate right by one bit position of the 33-bit quantity formed by appending the CPSR C flag to the most significant end of the contents of Rm as shown in ò Figure 5-10: Rotate right extended.
Figure 5-10: Rotate right extended
Register specified shift amount Only the least significant byte of the contents of Rs is used to determine the shift amount. Rs can be any general register other than R15. If this byte is zero, the unchanged contents of Rm will be used as the second operand, and the old value of the CPSR C flag will be passed on as the shifter carry output. If the byte has a value between 1 and 31, the shifted result will exactly match that of an instruction specified shift with the same value and shift operation. If the value in the byte is 32 or more, the result will be a logical extension of the shift described above:
1 LSL by 32 has result zero, carry out equal to bit 0 of Rm.
2 LSL by more than 32 has result zero, carry out zero.
3 LSR by 32 has result zero, carry out equal to bit 31 of Rm.
31 0
5 4
contents of Rm
value of operand 2
31 0
carry out
1
in
ARM Processor Instruction Set - TEQ, TST, CMP
Preliminary
5.4.7 Instruction cycle times
Data Processing instructions vary in the number of incremental cycles taken as follows:
Normal Data Processing 1instruction fetch
Data Processing with register specified shift
1 instruction fetch + 1 internal cycle
Data Processing with PC written 3 instruction fetches
Data Processing with register specified shift and PC written
3 instruction fetches and 1 internal cycle See ò 5.17 Instruction Speed Summary on page 5-52 for more information.
5.4.8 Assembler syntax
1 MOV,MVN - single operand instructions
2 CMP,CMN,TEQ,TST - instructions which do not produce a result.
3 AND,EOR,SUB,RSB,ADD,ADC,SBC,RSC,ORR,BIC
{cond} is a two-character condition mnemonic, see òFigure 5-2: Condition codes on page 5-
{S} sets condition codes if S present (implied for CMP, CMN, TEQ, TST).
Rd,Rn,Rm are expressions evaluating to a register number. If <#expression> is used, the assembler will attempt to generate a shifted immediate 8-bit field to match the expression. If this is impossible, it will give an error.
ARM Processor Instruction Set - TEQ, TST, CMP
Preliminary
5.4.9 Examples
ADDEQ R2,R4,R5 ; if the Z flag is set make R2:=R4+R
TEQS R4,#3 ; test R4 for equality with 3 ; (the S is in fact redundant as the ; assembler inserts it automatically)
SUB R4,R5,R7,LSR R2 ; logical right shift R7 by the number ; in the bottom byte of R2, subtract ; result from R5, and put the answer ; into R
MOV PC,R14 ; return from subroutine
MOVS PC,R14 ; return from exception and restore ; CPSR from SPSR_mode
ARM Processor Instruction Set - MRS, MSR
Preliminary
Figure 5-11: PSR Transfer
Cond
31 28 27 21 1615 1211 0
Condition field
P
2322
0 = CPSR 1 = SPSR_
(^00010) s 001111 Rd 000000000000
Destination register Source PSR
Condition field
MRS
31 2827 2322 21 0
MSR
Cond 00010 Pd Rm
4 3
Condition field
31 2827 23 22
MSR
Cond^ Pd
1010011111 00000000
12 11
Source register
(^21 ) 00 I 10 1010001111
(^110) Source operand
Immediate Operand
Rm
Rotate
Unsigned 8 bit immediate value shift applied to Imm
Imm
11 8 7 0
11 4 3 0
Destination PSR 0 = CPSR 1 = SPSR_
Destination PSR 0 = CPSR 1 = SPSR_
0 = Source operand is a register
1 = Source operand is an immediate value
00000000
Source register
(transfer PSR contents to a register)
(transfer register contents to PSR)
(transfer register contents or immediate value to PSR flag bits only)
ARM Processor Instruction Set - MRS, MSR
Preliminary
5.5.2 Reserved bits
Only eleven bits of the PSR are defined in ARM processor (N,Z,C,V,I,F and M[4:0]); the remaining bits (PSR[27:8,5]) are reserved for use in future versions of the processor. To ensure the maximum compatibility between ARM processor programs and future processors, the following rules should be observed:
1 The reserved bits must be preserved when changing the value in a PSR.
2 Programs must not rely on specific values from the reserved bits when checking the PSR status, since they may read as one or zero in future processors. A read-modify-write strategy should therefore be used when altering the control bits of any PSR register; this involves transferring the appropriate PSR register to a general register using the MRS instruction, changing only the relevant bits and then transferring the modified value back to the PSR register using the MSR instruction. For example, the following sequence performs a mode change: MRS R0,CPSR ; take a copy of the CPSR BIC R0,R0,#0x1F ; clear the mode bits ORR R0,R0,#new_mode ; select new mode MSR CPSR,R0 ; write back the modified CPSR When the aim is simply to change the condition code flags in a PSR, a value can be written directly to the flag bits without disturbing the control bits. For example, the following instruction sets the N,Z,C and V flags: MSR CPSR_flg,#0xF0000000; set all the flags regardless of ; their previous state (does not ; affect any control bits) No attempt should be made to write an 8-bit immediate value into the whole PSR since such an operation cannot preserve the reserved bits.
5.5.3 Instruction cycle times
PSR Transfers take 1 instruction fetch. For more information see ò 5.17 Instruction Speed Summary on page 5-52.
5.5.4 Assembler syntax
1 MRS - transfer PSR contents to a register MRS{cond} Rd,
2 MSR - transfer register contents to PSR MSR{cond}
3 MSR - transfer register contents to PSR flag bits only MSR{cond}
ARM Processor Instruction Set - MUL, MLA
Preliminary
The instruction is only executed if the condition is true. The various conditions are defined at the beginning of this chapter. The instruction encoding is shown in òFigure 5-12: Multiply instructions. The multiply and multiply-accumulate instructions use an 2-bit Booth's algorithm to perform integer multiplication. They give the least significant 32 bits of the product of two 32-bit operands, and may be used to synthesize higher precision multiplications.
Figure 5-12: Multiply instructions The multiply form of the instruction gives Rd:=RmRs. Rn is ignored, and should be set to zero for compatibility with possible future upgrades to the instruction set. The multiply-accumulate form gives Rd:=RmRs+Rn, which can save an explicit ADD instruction in some circumstances. Both forms of the instruction work on operands which may be considered as signed (2’s complement) or unsigned integers.
5.6.1 Operand restrictions
Due to the way multiplication was implemented, certain combinations of operand registers should be avoided. (The assembler will issue a warning if these restrictions are overlooked.) The destination register (Rd) should not be the same as the operand register (Rm), as Rd is used to hold intermediate values and Rm is used repeatedly during multiply. A MUL will give a zero result if Rm=Rd, and an MLA will give a meaningless result. R must not be used as an operand or as the destination register. All other register combinations will give correct results, and Rd, Rn and Rs may use the same register when required.
Cond 0 0 0 0 0 0 A S Rd Rn Rs 1 0 0 1 Rm
31 2827 22212019 1615 1211 8 7 4 3 0
Operand registers Destination register Set condition code
Accumulate
0 = do not alter condition codes 1 = set condition codes 0 = multiply only 1 = multiply and accumulate Condition Field
ARM Processor Instruction Set - MUL, MLA
Preliminary
5.6.2 CPSR flags
Setting the CPSR flags is optional, and is controlled by the S bit in the instruction. The N (Negative) and Z (Zero) flags are set correctly on the result (N is made equal to bit 31 of the result, and Z is set if and only if the result is zero). The C (Carry) flag is set to a meaningless value and the V (oVerflow) flag is unaffected.
5.6.3 Instruction cycle times
The Multiply instructions take 1 instruction fetch and m internal cycles. For more information see ò 5.17 Instruction Speed Summary on page 5-52.
m is the number of cycles required by the multiply algorithm, which is determined by the contents of Rs. Multiplication by any number between2^(2m-3) and2^(2m-1)-1 takes 1S+mI cycles for1
5.6.4 Assembler syntax
MUL{cond}{S} Rd,Rm,Rs MLA{cond}{S} Rd,Rm,Rs,Rn
{cond} two-character condition mnemonic, see ò Figure 5-2: Condition codes on page 5-
{S} set condition codes if S present
Rd, Rm, Rs and Rn
expressions evaluating to a register number other than R15.
5.6.5 Examples
MUL R1,R2,R3 ; R1:=R2R MLAEQS R1,R2,R3,R4 ; conditionally R1:=R2R3+R4, ; setting condition codes