Arm7 Scrib1
Arm7 Scrib1
ADDRESSING MODES
INSTRUCTION SET
Dr. P. H. Zope
Assistant Professor
SSBTs COET Bambhori Jalgaon
North Maharashtra University Jalgaon India
[email protected]
9860631040
Addressing modes
When accessing an operand for a data processing or
movement instruction, there are several standard techniques
used to specify the desired location. Most processors support
several of these addressing modes
Addressing modes
1. Immediate addressing: the desired value is presented as a binary value
in the instruction.
Addressing modes
6. Base plus offset addressing: the instruction specifies a register (the
base) and a binary offset to be added to the base to form the memory
address.
7. Base plus index addressing: the instruction specifies a base register
and another register (the index) which is added to the base to form the
memory address.
8. Base plus scaled index addressing: as above, but the index is
multiplied by a constant (usually the size of the data item, and usually a
power of two) before being added to the base.
9. Stack addressing: an implicit or specified register (the stack pointer)
points to an area of memory (the stack) where data items are written
(pushed) or read (popped) on a last-in-first-out basis.
4
Addressing modes
Memory is addressed by generating the Effective Address
(EA) of the operand by adding a signed offset to the
contents of a base register Rn.
Pre-indexed mode:
Post-indexed mode:
EA is the contents of Rn.
Offset is then added to this address and the result is written
back to Rn.
Thumb
Thumb is a 16-bit instruction set
Optimized for code density from C code
Improved performance form narrow memory
Subset of the functionality of the ARM instruction set
I/O System
ARM handles input/output peripherals as memorymapped with interrupt support
Internal registers in I/O devices as addressable
locations with ARMs memory map read and written
using load-store instructions
Interrupt by normal interrupt (IRQ) or fast interrupt
(FIQ)
Interrupt input signals are level-sensitive and
maskable
May include Direct Memory Access (DMA)
hardware
9
1.
2.
3.
4.
5.
6.
7.
10
e)
f)
g)
MOVE INSTRUCTIONS.
BARREL SHIFTER.
ARITHMETIC INSTRUCTIONS.
USING THE BARREL SHIFTER WITH
ARITHMETIC INSTRUCTIONS.
LOGICAL INSTRUCTIONS.
COMPARISION INSTRUCTIONS.
MULTIPLY INSTRUCTIONS.
11
MOV Instruction
12
PRE
r5 = 5
r7 = 8
MOV r7, r5
; let r7 = r5
POST
r5 = 5
r7 = 5
13
14
EXAMPLE
PRE
POST
r0 = 0x00000000
r1 = 0x00000002
r2 = 0x00000001
SUB r0, r1, r2
r0 = 0x00000001
15
16
Example
This example shows a logical OR operation between registers
r1 and r2. r0 holds the result.
PRE
POST
r0 = 0x00000000
r1 = 0x02040608
r2 = 0x10305070
ORR r0, r1, r2
r0 = 0x12345678
17
Comparison Instructions
The comparison instructions are used to compare or test a register with
a 32-bit value. They 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 change program flow by using conditional
execution.
18
Example
This example shows a CMP comparison instruction. You can see that
both registers, r0 and r9, are equal before executing the instruction.
The value of the z flag prior to execution is 0 and is represented by a
lowercase z. After execution the z flag changes to 1 or an uppercase Z.
This change indicates equality.
Multiply Instructions
The multiply instructions multiply the contents of a pair of registers and,
depending upon the instruction, accumulate the results in with another
register. The long multiplies accumulate onto a pair of registers
representing a 64-bit value. The final result is placed in a destination
register or a pair of registers.
20
Example
This example shows a simple multiply instruction that multiplies
registers r1 and r2 together and places the result into register r0. In this
example, register r1 is equal to the value 2, and r2 is equal to 2. The
result, 4, is then placed into register r0.
PRE r0 = 0x00000000
r1 = 0x00000002
r2 = 0x00000002
MUL r0, r1, r2 ; r0 = r1*r2
POST r0 = 0x00000004
r1 = 0x00000002
r2 = 0x00000002
21
Syntax:
BL{<cond>} label
B{<cond>} label
BX{<cond>} Rm
BLX{<cond>} label | R
22
23
25
26
27
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
28
register r1. The offset from register r1 is zero. Register r1 is called the
base address register.
29
31
Swap Instruction
The swap instruction is a special case of a load-store instruction. It swaps
the contents of memory with the contents of a register. This instruction is
an atomic operationit reads and writes a location in the same bus
operation, preventing any other instruction from reading or writing to that
location until it completes.
32
The swap instruction loads a word from memory into register r0 and
overwrites the memory with register r1.
33
34
35
EXAMPLE
PRE
cpsr = nzcVqift_USER
pc = 0x00008000
lr = 0x003fffff; lr = r14
r0 = 0x12
0x00008000 SWI 0x123456
POST cpsr = nzcVqIft_SVC
spsr = nzcVqift_USER
pc = 0x00000008
lr = 0x00008004
r0 = 0x12
36
SYNTAX:
MRS{<COND>} RD,<CPSR|SPSR>
MSR{<COND>} <CPSR|SPSR>_<FIELDS>,RM
MSR{<COND>} <CPSR|SPSR>_<FIELDS>,#IMMEDIATE
psr byte fields
39
Loading constants
You might have noticed that there is no ARM
instruction to move a 32-bit constant into a
register. Since ARM instructions are 32 bits in
size, they obviously cannot specify a general 32-bit
constant.
To aid programming there are two pseudo
instructions to move a 32-bit value into a register
40
Syntax:
LDR Rd, =constant
ADR Rd, label
41
ARMv5E Extensions
The ARMv5E extensions provide many new
instructions.
ARMv5E provides greater exibility and efciency
when manipulating 16-bit values,which is important
for applications such as 16-bit digital audio
processing.
42
43
44
45
Example
This example shows an ADD instruction with the EQ
condition appended.
This instruction will only be executed when the zero ag in
the cpsr is set to 1.
46
Gives....
Long branch range
Powerful arithmetic operations
Large address space
48
001
Always
condition
31
1110
10
Major
opcode
28
Minor
opcode
24
00 1
Rd
Constant
Destination &
source register
21 20 19
0100 1
16 15
Rd
Zero extended
constant
12
Rd
11
0000
Constant
I op1+op2S
ARM: ADDS Rd, Rd, #Constant
50
Branch Instructions
Thumb supports four types of branch instruction:
an unconditional branch that allows a forward or backward branch of
up to 2Kbytes
a conditional branch to allow forward and backward branches of up
to 256 bytes
a branch with link is supported with a pair of instructions that allow
forward and backwards branches of up to 4Mbytes
a branch and exchange instruction branches to an address in a
register and optionally switches to ARM code execution
B
B
BL
BX
conditional branch
unconditional branch
Branch with link
Branch and exchange instruction set
51
MOV, Move
MUL, Multiply
MVN, Move NOT
NEG, Negate
ORR, Logical OR
ROR, Rotate Right
SBC, Subtract with Carry
SUB, Subtract
TST, Test
52
LDR
LDRB
LDRH
LDRSB
LDRSH
STR
STRB
STRH
Load word
Load unsigned byte
Load unsigned halfword
Load signed byte
Load signed halfword
Store word
Store byte
Store halfword
53
LDM
POP
PUSH
STM
Load multiple
Pop multiple
Push multiple
Store multiple
54
55
56
Syntax:
BX Rm
BLX Rm | label
57
58
59
Syntax:
<ADC|ADD|AND|BIC|EOR|MOV|MUL|MVN|NEG|O
RR|SBC|SUB> Rd, Rm
<ADD|ASR|LSL|LSR|ROR|SUB> Rd, Rn
#immediate
<ADD|MOV|SUB> Rd,#immediate
<ADD|SUB> Rd,Rn,Rm
ADD Rd,pc,#immediate
ADD Rd,sp,#immediate
<ADD|SUB> sp, #immediate
<ASR|LSL|LSR|ROR> Rd,Rs
<CMN|CMP|TST> Rn,Rm
CMP Rn,#immediate
MOV Rd,Rn
60
This example shows a simple Thumb ADD instruction. It takes two low
registers r1 and r2 and adds them together. The result is then placed
into register r0, overwriting the original contents. The cpsr is also
updated.
PRE cpsr = nzcvIFT_SVC
r1 = 0x80000000
r2 = 0x10000000
ADD r0, r1, r2
POST r0 = 0x90000000
cpsr = NzcvIFT_SVC
61
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
POP
3
2
SP
1
SP
BASE
BASE
Result of
pop = 3
1
BASE
62
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
63
Stack Examples
STMFD sp!,
{r0,r1,r3-r5}
STMED sp!,
{r0,r1,r3-r5}
STMEA sp!,
{r0,r1,r3-r5}
STMFA sp!,
{r0,r1,r3-r5}
0x418
S
P
Old SP
Old SP
S
P
r5
r4
r3
r1
r0
r5
r4
r3
r1
r0
S
P
Old SP
r5
r4
r3
r1
r0
S
P
Old SP
r5
r4
r3
r1
r0
0x400
0x3e8
64
* 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
65
Direct functionality of
Block Data Transfer
* When LDM / STM are not being used to implement stacks, it is clearer
to specify exactly what functionality of the instruction is:
i.e. specify whether to increment / decrement the base pointer, before or
after the memory access.
* In order to do this, LDM / STM support a further syntax in addition to
the stack one:
STMIA / LDMIA : Increment After
STMIB / LDMIB : Increment Before
STMDA / LDMDA : Decrement After
STMDB / LDMDB : Decrement Before
66
IncreasingM
emory
r12
67
Rn
temp
2
3
Memory
Rd
R
mimplement an actual swap of contents make Rd = Rm.
* Thus to
68
Rn
temp
2
3
Memory
Rd
R
mimplement an actual swap of contents make Rd = Rm.
* Thus to
69
28
Cond
27
24 23
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.
* .
The ARM Instruction Set - ARM University Program - V1.0
70
71
72