Module 2 ARM Instruction Set
Module 2 ARM Instruction Set
CHAITHRA.S/AP,CSE/SVCE 1
Topics
Ø Programme Instructions
Ø Coprocessor Instructions
Ø Loading Constants
CHAITHRA.S/AP,CSE/SVCE 2
Introduction
PRE
POST
CHAITHRA.S/AP,CSE/SVCE 3
§ In the pre- and post-conditions, memory is denoted as
– mem
§ For example,
CHAITHRA.S/AP,CSE/SVCE 4
§ ARM instructions process data held in registers and only access
memory with load and store instructions.
§ For instance the ADD instruction below adds the two values stored in
registers r1 and r2 (the source registers).
CHAITHRA.S/AP,CSE/SVCE 5
3.1 DATA PROCESSING INSTRUCTIONS
- move instructions
- arithmetic instructions
- logical instructions
- comparison instructions
- multiply instructions
CHAITHRA.S/AP,CSE/SVCE 6
§ If you use the S suffix on a data processing instruction, then it updates
the flags in the cpsr.
§ Move and logical operations update the carry flag C, negative flag N,
and zero flag Z.
§ The carry flag is set from the result of the barrel shift as the last bit
shifted out.
CHAITHRA.S/AP,CSE/SVCE 7
3.1.1 MOVE INSTRUCTIONS
§ Useful for setting initial values and transferring data between registers
§ Syntax:
PRE r5 = 5
r7 = 8
MOV r7, r5 ; let r7 = r5
POST r5 = 5
r7 = 5
CHAITHRA.S/AP,CSE/SVCE 9
BARREL SHIFTER
§ This shift increases the power and flexibility of many data processing
operations. CHAITHRA.S/AP,CSE/SVCE 10
§ There are data processing instructions that do not use the barrel shift,
§ for example
- MUL (multiply)
CHAITHRA.S/AP,CSE/SVCE 11
§ Fig shows the data flow between the ALU
and the barrel shifter.
§ a d d a s h i f t o p e ra t i o n t o t h e m o v e
instruction
§ Register Rn enters the ALU without any
pre- processing of registers
§ We apply a logical shift left (LSL) to
register Rm before moving it to the
destination register Figure : Barrel shifter and ALU.
§ The MOV instruction copies the shift
operator result N into register Rd.
§ N re p re s e n t s t h e r e s u l t o f t h e L S L
operation
CHAITHRA.S/AP,CSE/SVCE 12
PRE r5=5
r7=8
POST r5=5
r7 = 20
CHAITHRA.S/AP,CSE/SVCE 13
§ The five different shift operations that you can use within the barrel
shifter are summarized in Table below
CHAITHRA.S/AP,CSE/SVCE 14
§ Figure below illustrates a logical shift left by one.
CHAITHRA.S/AP,CSE/SVCE 21
LOGICAL INSTRUCTIONS
§ Syntax:
CHAITHRA.S/AP,CSE/SVCE 22
CHAITHRA.S/AP,CSE/SVCE 23
§ BIC instruction is particularly useful when clearing status bits and is
frequently used to change interrupt masks in the cpsr.
§ The logical instructions update the cpsr flags only if the S suffix is
present.
CHAITHRA.S/AP,CSE/SVCE 24
COMPARISON INSTRUCTIONS
§ update the cpsr flag bits according to the result, but do not affect other
registers
§ After the bits have been set, the information can then be used to
§ Syntax:
CHAITHRA.S/AP,CSE/SVCE 25
CMN compare negated flags set as a result of Rn + N
CMP compare flags set as a result of Rn − N
TEQ flags set as a result of Rn ∧ N
test for equality of two 32-
bit values
TST test bits of a 32-bit value flags set as a result of Rn & N
CHAITHRA.S/AP,CSE/SVCE 26
CHAITHRA.S/AP,CSE/SVCE 27
MULTIPLY INSTRUCTIONS
§ multiply the contents of a pair of registers and, depending upon the
instruction, accumulate the results in with another register
§ Syntax:
§ The change of e
§ Syntax:
B{<cond>}
BL{<cond>}
BX{<cond>}
BLX{<cond>}
CHAITHRA.S/AP,CSE/SVCE 31
B Branch pc = label
BL branch with link pc = label
lr = address of the next instruction after the BL
BX branch exchange pc = Rm & 0xfffffffe, T = Rm & 1
pc = label, T =1
BLX branch exchange pc = Rm & 0xfffffffe, T = Rm & 1
with link lr = address of the next instruction after the BLX
CHAITHRA.S/AP,CSE/SVCE 32
CHAITHRA.S/AP,CSE/SVCE 33
3.3 LOAD-STORE INSTRUCTIONS
i. Single-Register transfer
iii. Swap
CHAITHRA.S/AP,CSE/SVCE 34
3.3.1 SINGLE-REGISTER TRANSFER
§ Syntax:
<LDR|STR>{<cond>}{B} Rd,addressing1
CHAITHRA.S/AP,CSE/SVCE 35
LDR load word into a register Rd <- mem32[address]
STR save byte or word from a register Rd -> mem32[address]
LDRB load byte into a register Rd <- mem8[address]
STRB save byte from a register Rd -> mem8[address]
CHAITHRA.S/AP,CSE/SVCE 36
§ The first instruction loads a word from the address stored in register r1
and places it into register r0.
§ The second instruction goes the other way by storing the contents of
register r0 to the address contained in register r1.
§ The offset from register r1 is zero. Register r1 is called the base address
register. CHAITHRA.S/AP,CSE/SVCE 37
3.3.2 SINGLE-REGISTER LOAD-STORE ADDRESSING MODES
CHAITHRA.S/AP,CSE/SVCE 38
CHAITHRA.S/AP,CSE/SVCE 39
CHAITHRA.S/AP,CSE/SVCE 40
§ The addressing modes available with a particular load or store instruction
depend on the instruction class.
§ A signed offset or register is denoted by “+ /- ”, identifying that it is either a
positive or negative offset from the base address register Rn.
§ The base address register is a pointer to a byte in memory, and the offset
specifies a number of bytes.
CHAITHRA.S/AP,CSE/SVCE 41
CHAITHRA.S/AP,CSE/SVCE 42
CHAITHRA.S/AP,CSE/SVCE 43
3.3..3 MULTIPLE-REGISTER TRANSFER
CHAITHRA.S/AP,CSE/SVCE 44
load multiple {Rd}∗ N <- mem32[start address + 4∗ N]
LDM registers optional Rn updated
save multiple {Rd}∗ N -> mem32[start address + 4∗ N]
STM registers optional Rn updated
CHAITHRA.S/AP,CSE/SVCE 45
CHAITHRA.S/AP,CSE/SVCE 46
§ The base register r0 points to memory address 0x80010 in the PRE
condition.
§ Memory addresses 0x80010, 0x80014, and 0x80018 contain the values 1,
2, and 3 respectively.
CHAITHRA.S/AP,CSE/SVCE 47
§ After the load multiple instruction executes registers r1, r2, and r3
contain these values as shown in Figure below.
§ The base register r0 now points to memory address 0x8001c after the
last loaded word.
CHAITHRA.S/AP,CSE/SVCE 48
§ Replace the LDMIA instruction with a load multiple and increment before
LDMIB instruction and use the same PRE conditions.
CHAITHRA.S/AP,CSE/SVCE 50
CHAITHRA.S/AP,CSE/SVCE 51
CHAITHRA.S/AP,CSE/SVCE 52
§ This routine relies on registers r9, r10, and r11 being set up before the
code is executed. Registers r9 and r11 determine the data to be copied,
and register r10 points to the destination in memory for the data.
§ LDMIA loads the data pointed to by register r9 into registers r0 to r7. It
also updates r9 to point to the next block of data to be copied.
§ STMIA copies the contents of registers r0 to r7 to the destination memory
address pointed to by register r10. It also updates r10 to point to the next
destination location.
§ CMP and BNE compare pointers r9 and r11 to check whether the end of
the block copy has been reached.
§ If the block copy is complete, then the routine finishes; otherwise the
loop repeats with the updated values of register r9 and r10.
§ The BNE is the branch instruction B with a condition mnemonic NE (not
equal).
§ If the previous compare instruction sets the condition flags to not equal,
the branch instruction is executed.
CHAITHRA.S/AP,CSE/SVCE 53
§ Figure 3.6 shows the memory map of the block memory copy and how
the routine moves through memory.
CHAITHRA.S/AP,CSE/SVCE 54
Stack Operations
§ The pop operation (removing data from a stack) uses a load multiple
instruction; similarly, the push operation (placing data onto the stack)
uses a store multiple instruction.
§ When using a stack you have to decide whether the stack will grow up or
down in memory.
CHAITHRA.S/AP,CSE/SVCE 55
§ When you use a full stack (F), the stack pointer sp points to an address
that is the last used or full location (i.e., sp points to the last item on the
stack).
§ In contrast, if you use an empty stack (E) the sp points to an address that
is the first unused or empty location (i.e., it points after the last item on
the stack).
§ Thus, the LDMFD and STMFD instructions provide the pop and push
functions, respectively.
CHAITHRA.S/AP,CSE/SVCE 56
CHAITHRA.S/AP,CSE/SVCE 57
CHAITHRA.S/AP,CSE/SVCE 58
§ When handling a checked stack there are three attributes that need to be
preserved: the stack base, the stack pointer, and the stack limit.
§ The stack pointer initially points to the stack base; as data is pushed onto the stack,
the stack pointer descends memory and continuously points to the top of stack.
§ If the stack pointer passes the stack limit, then a stack overflow error has occurred.
§ Here is a small piece of code that checks for stack overflow errors for a descending
stack:
CHAITHRA.S/AP,CSE/SVCE 59
§ ATPCS defines register r10 as the stack limit or sl.
§ The BLLO instruction is a branch with link instruction plus the condition
mnemonic LO.
§ If sp is less than register r10 after the new items are pushed onto the
stack, then stack overflow error has occurred.
§ If the stack pointer goes back past the stack base, then a stack underflow
error has occurred.
CHAITHRA.S/AP,CSE/SVCE 60
3.3.4 SWAP INSTRUCTION
§ Syntax:
SWP{B}{<cond>} Rd,Rm,[Rn]
CHAITHRA.S/AP,CSE/SVCE 61
SWP swap a word between memory tmp = mem32[Rn]
and a register mem32[Rn] = Rm
Rd = tmp
SWPB swap a byte between memory tmp = mem8[Rn]
and a register mem8[Rn] = Rm
Rd = tmp
CHAITHRA.S/AP,CSE/SVCE 62
§ This instruction is particularly useful when implementing semaphores
and mutual exclusion in an operating system.
CHAITHRA.S/AP,CSE/SVCE 63
§ The address pointed to by the semaphore either contains the value 0
or 1.
§ When the semaphore equals 1, then the service in question is being
used by another process.
§ The routine will continue to loop around until the service is released by
the other process—in other words, when the semaphore address
location contains the value 0.
CHAITHRA.S/AP,CSE/SVCE 64
3.4 SofTWARE INTERRUPT INSTRUCTION
§ Syntax:
CHAITHRA.S/AP,CSE/SVCE 65
§ When the processor executes an SWI instruction, it
.
CHAITHRA.S/AP,CSE/SVCE 66
§ SWI instructions are used to call operating system routines, you need
some form of parameter passing.
§ This is achieved using registers. In this example, register r0 is used to pass
the parameter 0x12.
§ The return values are also passed back via registers.
CHAITHRA.S/AP,CSE/SVCE 67
§ Code called the SWI handler is required to process the SWI call. The
handler obtains the SWI number using the address of the executed
instruction, which is calculated from the link register lr.
§ Here the SWI instruction is the actual 32-bit SWI instruction executed
by the processor.
CHAITHRA.S/AP,CSE/SVCE 68
CHAITHRA.S/AP,CSE/SVCE 69
3.5 PROGRAM STATUS REGISTER INSTRUCTIONS
§ The MRS instruction transfers the contents of either the cpsr or spsr
into a register; in the reverse direction, the MSR instruction transfers
the contents of a register into the cpsr or spsr.
§ Together these instructions are used to read and write the cpsr and
spsr.
§ Syntax:
MRS{<cond>} Rd,<cpsr|spsr>
MSR{<cond>} <cpsr|spsr>_<fields>,Rm
MSR{<cond>} <cpsr|spsr>_<fields>,#immediate
CHAITHRA.S/AP,CSE/SVCE 70
§ In the syntax you can see a label called fields.
§ This can be any combination of control (c), extension (x), status (s), and
flags (f ). These fields relate to particular byte regions in a psr, as shown
in Figure 3.9.
CHAITHRA.S/AP,CSE/SVCE 71
MRS copy program status register to a Rd = psr
general-purpose register
§ The c field controls the interrupt masks, Thumb state, and processor
mode.
§ Example 3.26 shows how to enable IRQ interrupts by clearing the I mask.
This operation involves using both the MRS and MSR instructions to
read from and then write to the cpsr.
CHAITHRA.S/AP,CSE/SVCE 72
CHAITHRA.S/AP,CSE/SVCE 73
3.5.1 COPROCESSOR INSTRUCTIONS
CHAITHRA.S/AP,CSE/SVCE 74
CDP coprocessor data processing—perform an operation
in a coprocessor
MRC MCR coprocessor register transfer—move data to/from
coprocessor registers
LDC STC coprocessor memory transfer—load and store blocks
of memory to/from a coprocessor
§ In the syntax of the coprocessor instructions, the field represents the
coprocessor number between and .
§ The opcode fields describe the operation to take place on the
coprocessor.
§ The Cn, Cm, and Cd fields describe .
§ The coprocessor operations and registers depend on the specific
coprocessor you are using.
.
CHAITHRA.S/AP,CSE/SVCE 75
CHAITHRA.S/AP,CSE/SVCE 76
3.6 LOADING CONSTANTS
§ Since ARM instructions are 32 bits in size, they obviously cannot specify
a general 32-bit constant.
§ Syntax:
Rd, =constant
Rd, label
CHAITHRA.S/AP,CSE/SVCE 77
LDR load constant pseudoinstruction Rd = 32-bit constant
§ It defaults to
.
CHAITHRA.S/AP,CSE/SVCE 78
CHAITHRA.S/AP,CSE/SVCE 79
§ The first conversion produces a simple MOV instruction; the second
conversion produces a pc-relative load.
CHAITHRA.S/AP,CSE/SVCE 80