Download ARM Instruction Set: A Comprehensive Guide for Developers and more Lecture notes Biology in PDF only on Docsity!
ARM DDI 0084D
- ARM7TDMI-S Data Sheet 4-
- 4.1 Instruction Set Summary 4- This chapter describes the ARM instruction set.
- 4.2 The Condition Field 4-
- 4.3 Branch and Exchange (BX) 4-
- 4.4 Branch and Branch with Link (B, BL) 4-
- 4.5 Data Processing 4-
- 4.6 PSR Transfer (MRS, MSR) 4-
- 4.7 Multiply and Multiply-Accumulate (MUL, MLA) 4-
- 4.8 Multiply Long and Multiply-Accumulate Long (MULL,MLAL) 4-
- 4.9 Single Data Transfer (LDR, STR) 4-
- 4.10 Halfword and Signed Data Transfer 4-
- 4.11 Block Data Transfer (LDM, STM) 4-
- 4.12 Single Data Swap (SWP) 4-
- 4.13 Software Interrupt (SWI) 4-
- 4.14 Coprocessor Data Operations (CDP) 4-
- 4.15 Coprocessor Data Transfers (LDC, STC) 4-
- 4.16 Coprocessor Register Transfers (MRC, MCR) 4-
- 4.17 Undefined Instruction 4-
- 4.18 Instruction Set Examples 4-
4-2 ARM7TDMI-S Data Sheet
ARM DDI 0084D
4.1 Instruction Set Summary
4.1.1 Format summary
The ARM instruction set formats are shown below.
Figure 4-1: ARM instruction set formats
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 should not be used, as their action may change in future ARM
implementations.
Cond 0 0 I Opcode S Rn Rd Operand 2 Data Processing / PSR Transfer Cond 0 0 0 0 0 0 A S Rd Rn Rs 1 0 0 1 Rm Multiply Cond 0 0 0 0 1 U A S RdHi RdLo Rn 1 0 0 1 Rm Multiply Long Cond 0 0 0 1 0 B 0 0 Rn Rd 0 0 0 0 1 0 0 1 Rm Single Data Swap Cond 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 Rn Branch and Exchange Cond 0 0 0 P U 0 W L Rn Rd 0 0 0 0 1 S H 1 Rm Halfword Data Transfer: register offset Cond 0 0 0 P U 1 W L Rn Rd Offset 1 S H 1 Offset Halfword Data Transfer: immediate offset Cond 0 1 I P U B W L Rn Rd Offset Single Data Transfer Cond 0 1 1 1 Undefined Cond 1 0 0 P U S W L Rn Register List Block Data Transfer Cond 1 0 1 L Offset Branch Cond 1 1 0 P U N W L Rn CRd CP# Offset Coprocessor Data Transfer Cond 1 1 1 0 CP Opc CRn CRd CP# CP 0 CRm Coprocessor Data Operation Cond 1 1 1 0 CP Opc L CRn Rd CP# CP 1 CRm Coprocessor Register Transfer Cond 1 1 1 1 Ignored by processor Software Interrupt
3 1
4-4 ARM7TDMI-S Data Sheet
ARM DDI 0084D
SBC Subtract with Carry Rd := Rn - Op2 - 1 + Carry 4.
STC Store coprocessor register to memory
address := CRn 4.
STM Store Multiple Stack manipulation (Push) 4.
STR Store register to memory
:= Rd 4.9, 4. SUB Subtract Rd := Rn - Op2 4.
SWI Software Interrupt OS call 4.
SWP Swap register with memory Rd := [Rn], [Rn] := Rm 4.
TEQ Test bitwise equality CPSR flags := Rn EOR Op2 4.
TST Test bits CPSR flags := Rn AND Op2 4.
Mnemonic Instruction Action See Section:
Table 4-1: The ARM Instruction set (Continued)
ARM7TDMI-S Data Sheet 4-
ARM DDI 0084D
4.2 The Condition Field
In ARM state, all instructions are conditionally executed according to the state of the
CPSR condition codes and the instruction’s condition field. This field (bits 31:28)
determines the circumstances under which an instruction is to be executed. If the state
of the C, N, Z and V flags fulfils the conditions encoded by the field, the instruction is
executed, otherwise it is ignored.
There are sixteen possible conditions, each represented by a two-character suffix that
can be appended to the instruction’s mnemonic. For example, a Branch (B in assembly
language) becomes BEQ for "Branch if Equal", which means the Branch will only be
taken if the Z flag is set.
In practice, fifteen different conditions may be used: these are listed in Table 4-2:
Condition code summary. The sixteenth (1111) is reserved, and must not be used.
In the absence of a suffix, the condition field of most instructions is set to "Always" (sufix
AL). This means the instruction will always be executed regardless of the CPSR
condition codes.
Code Suffix Flags Meaning
0000 EQ Z set equal
0001 NE Z clear not equal
0010 CS C set unsigned higher or same
0011 CC C clear unsigned lower
0100 MI N set negative
0101 PL N clear positive or zero
0110 VS V set overflow
0111 VC V clear no overflow
1000 HI C set and Z clear unsigned higher
1001 LS C clear or Z set unsigned lower or same
1010 GE N equals V greater or equal
1011 LT N not equal to V less than
1100 GT Z clear AND (N equals V) greater than
1101 LE Z set OR (N not equal to V) less than or equal
1110 AL (ignored) always
Table 4-2: Condition code summary
ARM7TDMI-S Data Sheet^ 4-
ARM DDI 0084D
4.3.4 Examples
ADR R0, Into_THUMB + 1; Generate branch target address ; and set bit 0 high - hence ; arrive in THUMB state. BX R0 ; Branch and change to THUMB ; state. CODE16 ; Assemble subsequent code as Into_THUMB ; THUMB instructions
ADR R5, Back_to_ARM: Generate branch target to word : aligned ; address - hence bit 0 ; is low and so change back to ARM ; state. BX R5 ; Branch and change back to ARM ; state. . . ALIGN ; Word align CODE32 ; Assemble subsequent code as ARM Back_to_ARM ; instructions
4-8 ARM7TDMI-S Data Sheet
ARM DDI 0084D
4.4 Branch and Branch with Link (B, BL)
The instruction is only executed if the condition is true. The various conditions are
defined Table 4-2: Condition code summary on page 4-5. The instruction encoding
is shown in Figure 4-3: Branch instructions , below.
Figure 4-3: Branch instructions
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.
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.
4.4.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 and R14[1:0] are always cleared.
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.
4.4.2 Instruction cycle times
Branch and Branch with Link instructions take 2S + 1N incremental cycles, where S and
N are as defined in 6.2 Cycle Types on page 6-3.
Cond 101 L offset
31 28 27 25 24 23 0
Link bit
0 = Branch 1 = Branch with Link
Condition field
4-10 ARM7TDMI-S Data Sheet
ARM DDI 0084D
4.5 Data Processing
The data processing instruction is only executed if the condition is true. The conditions
are defined in Table 4-2: Condition code summary on page 4-5.
The instruction encoding is shown in Figure 4-4: Data processing instructions
below.
Figure 4-4: Data processing instructions
The instruction produces a result by performing a specified arithmetic or logical
operation on one or two operands. The first operand is always a register (Rn).
Cond 00 I OpCode Rn Rd Operand 2
31 2827 2625 24 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 - Op 1011 = 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
ARM7TDMI-S Data Sheet^ 4-
ARM DDI 0084D
The second operand may be a shifted register (Rm) or a rotated 8 bit immediate value
(Imm) according to the value of the I bit in the instruction. The condition codes in the
CPSR may be preserved or updated as a result of this instruction, according to the
value of the S bit in the instruction.
Certain operations (TST, TEQ, CMP, CMN) do not write the result to Rd. They are used
only to perform tests and to set the condition codes on the result and always have the
S bit set. The instructions and their effects are listed in Table 4-3: ARM Data
processing instructions.
4.5.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.
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
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 4-3: ARM Data processing instructions
ARM7TDMI-S Data Sheet^ 4-
ARM DDI 0084D
Note LSL #0 is a special case, where the shifter carry out is the old value of the CPSR C flag.
The contents of Rm are used directly as the second operand.
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 4-7: Logical shift right.
Figure 4-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 4-8: Arithmetic shift right.
Figure 4-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 4-9: Rotate right on page 4-13.
Figure 4-9: Rotate right
contents of Rm
value of operand 2
31 0
carry out
5 4
contents of Rm
value of operand 2
31 0
carry out
30 5 4
4-14 ARM7TDMI-S Data Sheet
ARM DDI 0084D
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 4-10: Rotate right
extended.
Figure 4-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.
4 LSR by more than 32 has result zero, carry out zero.
5 ASR by 32 or more has result filled with and carry out equal to bit 31 of Rm.
6 ROR by 32 has result equal to Rm, carry out equal to bit 31 of Rm.
7 ROR by n where n is greater than 32 will give the same result and carry out as
ROR by n-32; therefore repeatedly subtract 32 from n until the amount is in the
range 1 to 32 and see above.
Note The zero in bit 7 of an instruction with a register controlled shift is compulsory; a one in
this bit will cause the instruction to be a multiply or undefined instruction.
contents of Rm
value of operand 2
carry out
contents of Rm
value of operand 2
carry
out
C
in
4-16 ARM7TDMI-S Data Sheet
ARM DDI 0084D
2 CMP,CMN,TEQ,TST (instructions which do not produce a result.)
{cond} Rn,
3 AND,EOR,SUB,RSB,ADD,ADC,SBC,RSC,ORR,BIC
{cond}{S} Rd,Rn,
where:
is Rm{,} or,<#expression>
{cond} is a two-character condition mnemonic. See Table 4-2:
Condition code summary on page 4-5.
{S} set condition codes if S present (implied for CMP, CMN, TEQ,
TST).
Rd, Rn and Rm are expressions evaluating to a register number.
<#expression> if this 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.
is or #expression, or
RRX (rotate right one bit with extend).
s are: ASL, LSL, LSR, ASR, ROR. (ASL is a synonym for LSL,
they assemble to the same code.)
4.5.9 Examples
ADDEQR2,R4,R5 ; If the Z flag is set make R2:=R4+R TEQSR4,#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 R4. MOV PC,R14 ; Return from subroutine. MOVSPC,R14 ; Return from exception and restore CPSR ; from SPSR_mode.
ARM7TDMI-S Data Sheet^ 4-
ARM DDI 0084D
4.6 PSR Transfer (MRS, MSR)
The instruction is only executed if the condition is true. The various conditions are
defined in Table 4-2: Condition code summary on page 4-5.
The MRS and MSR instructions are formed from a subset of the Data Processing
operations and are implemented using the TEQ, TST, CMN and CMP instructions
without the S flag set. The encoding is shown in Figure 4-11: PSR transfer on page
These instructions allow access to the CPSR and SPSR registers. The MRS instruction
allows the contents of the CPSR or SPSR_ to be moved to a general register.
The MSR instruction allows the contents of a general register to be moved to the CPSR
or SPSR_ register.
The MSR instruction also allows an immediate value or register contents to be
transferred to the condition code flags (N,Z,C and V) of CPSR or SPSR_
without affecting the control bits. In this case, the top four bits of the specified register
contents or 32 bit immediate value are written to the top four bits of the relevant PSR.
4.6.1 Operand restrictions
- In User mode, the control bits of the CPSR are protected from change, so only
the condition code flags of the CPSR can be changed. In other (privileged)
modes the entire CPSR can be changed.
Note that the software must never change the state of the T bit in the CPSR. If
this happens, the processor will enter an unpredictable state.
- The SPSR register which is accessed depends on the mode at the time of
execution. For example, only SPSR_fiq is accessible when the processor is in
FIQ mode.
- You must not specify R15 as the source or destination register.
- Also, do not attempt to access an SPSR in User mode, since no such register
exists.
ARM7TDMI-S Data Sheet^ 4-
ARM DDI 0084D
4.6.2 Reserved bits
Only twelve bits of the PSR are defined in ARM7TDMI-S (N,Z,C,V,I,F, T & M[4:0]); the
remaining bits are reserved for use in future versions of the processor. Refer to
Figure 3-6: Program status register format on page 3-12 for a full description of the
PSR bits.
To ensure the maximum compatibility between ARM7TDMI-S programs and future
processors, the following rules should be observed:
- The reserved bits should be preserved when changing the value in a PSR.
- Programs should 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.
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. 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.
4.6.3 Instruction cycle times
PSR Transfers take 1S incremental cycles, where S is as defined in 6.2 Cycle Types
on page 6-3.
4-20 ARM7TDMI-S Data Sheet
ARM DDI 0084D
4.6.4 Assembler syntax
1 MRS - transfer PSR contents to a register
MRS{cond} Rd,
2 MSR - transfer register contents to PSR
MSR{cond} ,Rm
3 MSR - transfer register contents to PSR flag bits only
MSR{cond} ,Rm
The most significant four bits of the register contents are written to the N,Z,C
& V flags respectively.
4 MSR - transfer immediate value to PSR flag bits only
MSR{cond} ,<#expression>
The expression should symbolise a 32 bit value of which the most significant
four bits are written to the N,Z,C and V flags respectively.
Key:
{cond} two-character condition mnemonic. See Table 4-2:
Condition code summary on page 4-5.
Rd and Rm are expressions evaluating to a register number other than
R
is CPSR, CPSR_all, SPSR or SPSR_all. (CPSR and
CPSR_all are synonyms as are SPSR and SPSR_all)
is CPSR_flg or SPSR_flg
<#expression> where this 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.