Professional Documents
Culture Documents
Topic 3 ARM Instruction Set Part - 1
Topic 3 ARM Instruction Set Part - 1
Instruction SET
and
Programming
Part-I
– THUMB
• 16-bit compressed form
• Code density better than most CISC
• Dynamic decompression in pipeline
ARM Instruction Set
ARM instruction set
Data processing
instructions
Data transfer
Block transfer instructions
instructions
Branching instructions
Multiply instructions
Software interrupt
instructions
ARM Instruction Set
• ARM architecture is such that 32-bit ARM
instruction set & 16 bit Thumb instruction both
can be used.
• Various addressing modes are as follow:
1. Register addressing
2. Immediate addressing
3. Indirect base and indexed addressing
Register Addressing Modes
• All the operands are within the register itself.
• Example:
MOV R1,R2 R2R1
MVN R1,R2 R2’R1 (Complement of R2)
(Logic operation NOT and MOV combined in MVN)
ADD R1,R2,R3 R1=R2+R3
SUB R5,R4,R7 R5=R4-R7
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)
Immediate Addressing Mode
• Second source operand is immediate operand
• MOV R0,#0 R0=0
• ADD r6,r7,#6 R6=R7+6
• CMP R7,#1000 Compare content of R7 with 1000
• MOV R1,#0x17 Hexadecimal value 17h in R1
• MOV R2,#17 Decimal value 17 in R2
• MOV R1,R1,LSL #2
R1 shifted left by 2 bit position and saved back in
R1 (R1=R1*4)
Indirect Base and Indexed addressing
• STR [R5,#&0C],R4
Store Register R4 to the memory address pointed
by R5+12
• Indirect addressing mode by index plus offset with
post auto indexing
For example: LDR R1,[R2],#8
In this instruction R1 is loaded from memory
location pointed by R2 and after loading, offset of
8 is added into R2. This is called auto post
indexing
Indirect addressing by index plus
offset with pre auto indexing
• LDR R4,[R3,#8]!
Load R4 from memory address pointed by R3
after adding offset 8 into R3.
If R3=0000 0000 0000 0010 then R4 will be
loaded from the location (R3+8) = 0000 0000
0000 1010 in case of pre-auto indexing. In case of
post indexing R4 will be loaded from 0000 0000
0000 0010 and then R3 becomes 0000 0000 0000
1010
Arithmetic Instructions
• Operations such as addition, subtraction and
multiplication are available in direct and modified
form.
• ADD Rd,Rn,<Oprnd> Exa: ADD R1,R2,#55
Above instruction adds Rn to <Oprnd> and store
result in Rd. (R1=R2+55)
• ADDS Rd,Rn,<Oprnd> Exa: ADDS R1,R2,#55
Same as ADD but ADDS has additional effect of
testing the result stored in Rd and setting four
condition codes in CPSR.
Arithmetic Instructions
• ADC Rd,Rn,<oprnd> Exa: ADC R4,R5,R6
ADC includes carry during addition process. In
above example R4=R5+R6+Carry. C
• SUB Rd,Rn,<oprnd> Exa: SUB R2,R2,R5
R2=R2-R5
• SUBS Rd,Rn,<oprnd> Exa: SUBS R2,R2,R5
R2=R2-R5 CPSR is affected by result in R2
• RSB (Reverse subtraction)
RSB Rd,Rn,<oprnd> Exa: RSB R2,R2,R5
R2=R5-R2
• RSBS (Reverse subtraction with CPSR affected)
Arithmetic Instructions
• SBC (Consider carry during subtraction)
• SBC Rd,Rn,<oprnd> Exa: SBC R1,R2,R3
R1=R2-R3-NOT(carry)
• SBCS (Same as SBC but CPSR is affected by result
in destination)
• RSC Rd,Rn,<oprnd> Exa: RSC R1,R2,R3
R1=R3-R2-NOT(carry)
• RSCS (Same as RSC but CPSR is affected by
result)
Instructions
• MUL R1,R2,R3; R1:=R2*R3
• MOV R2,R0, LSL #3 ; Shift R0 left by 3 R2
(R2=R0*8)
• ADD R9,R5,R5,LSL #2 ; R9=R5*4+R5
(R9=5*R5)
• SUB R10,R9,R8,LSR#4 ; R10=R9-R8/16
• MLA R1,R2,R3,R4 ; R1=(R2*R3)+R4
(Multiply and accumulate useful for convolution in
signal processing applications)
• MOV R12,R4,ROR R3 ; R12=R4 rotated right by
value of R3
Load and store instructions
Transfers data between memory and processor
registers
• Single Register transfer: Data type supported are
32 bit signed or unsigned, 16 bit and 8 bit
(Word, Half-word or Byte)
• Multiple Register transfer: Transfer multiple
registers between memory and processor in single
instruction
• Swap: swap content of memory with content of
processor registers.
Load and store Instructions
• Load/store instructions are basically data transfer
instructions from processor memory to registers and vice-
varsa
• Can be used to load PC
(if target address is beyond branch instruction range)
to selected GPR R7
CPSR MSR R8
• MRS moves contents SPSR
from selected GPR
R14
to CPSR/SPSR R15
• Only in privileged
modes
Shift operations by barrel shifter
LSL : Logical Left Shift MSB moves to carry flag and 0
inserted at LSB
CF Destination 0 EXAMPLES
Multiplication by a power of 2 MOV R0,R1,LSL #2
MOV R0,R1,LSL R2
Immediate value
– 8 bit number, with a range of 0-255.
• Rotated right through even
ALU
number of positions
– Allows increased range of 32-bit
constants to be loaded directly into
Result registers
Multiply Instructions
• Integer multiplication (32-bit result)
• Long integer multiplication (64-bit result)
• Built in Multiply Accumulate Unit (MAC)
• Multiply and accumulate instructions add
product to running total
Multiply Instructions
• Instructions:
For Example
if (z==1) R1=R2+(R3*4)
compiles to
( SINGLE INSTRUCTION ! )
Example: Conditional Execution
MOVCS R0,R1
• This instruction will move value of register
R1 to register R0 only if carry flag is set.
MOVEQ R0,R1
• This instruction will move value of register
R1 to register R0 only if zero flag is set.
BCC label
• This instruction takes branch to label if
carry flag is cleared
Example: Conditional Execution
ADDEQ R0,R1,R2
• Instruction executed only if zero flag is set
R0=R1+R2 only if zero flag is set.
Advantage of conditional execution
instruction:
• Conditional instructions reduces number
of branches and number of pipeline
flushes
• Increases code density
Branching Instructions
• Two types of branch instructions: B & BL
• B: Ordinary branch instruction
• BL: Branch and link instruction
• BL Perform branch operation and save
address following the branch (return
address) in the link register R14.
• BL <Name of subroutine>
Branching Instructions
• Branch and Exchange (BX)
• This instruction is only executed if the
condition is true.
• This instruction performs a branch by
copying the contents of a general register,
Rn, into the program counter (PC)
Branching Instructions
• Branch exchange (BX) and
Branch link exchange (BLX):
same as B/BL +
exchange instruction set (ARM THUMB)
• To return from subroutine to the main
program there is no RET instruction but
MOV PC,R14 can be used to return
Example: Conditional Execution
Consider that value loaded in R1=55 and R2=66
CMP R1,R2
• BEQ <label> : Branch not taken because (R1-R2) ≠ 0
• BNE <label> : Branch taken because R1 ≠ R2
• BGE <lable> : Branch not taken because R1<R2
• BLT <label> : Branch taken because R1<R2
• BMI <label> : Branch taken because result minus (R1-R2)
Example: conditional execution
Consider following instructions:
MOVS R0,R1
MOVEQS R0,R2
MOVEQ R0,R3
Explanation:
• The first instruction moves content of register R1 into R0
and change N and Z flag depending on value stored in
R0.
• Second instruction will transfer R2 to R0 only if zero flag
is set. That means if during first instruction R1=0, second
instruction will execute. If second instruction is executed it
will change N and Z flag depending on value stored in R2.
• Third instruction transfer register R3 to R0 only if zero flag
is set. That means R2=0 during second instruction. Thus,
third instruction will be executed only of R1 and R2 both
were 0.
Program: To add elements of array
ORG 0x0000
MOV R1,#3 ;total 3 elements in array
MOV R2,#0x0118
MOV R0,#0 ; Reset R0 to store result
Loop: LDR R3,[R2] ; R3=[R2]
ADD R0,R0,R3 ; R0=R0+R3
ADD R2,R2,#4 ; R2 moves to next element
SUB R1,R1,#1 ; Decrement counter
CMP R1,#0 ; See whether R1=0 or not
BNE Loop
END
Exercise
• Visit website: www.arm.com for latest
updates
• Find out what is CAN and utilization of
CAN.
• List ARM based microcontrollers
• What are the features of ARM based
Phillips microcontroller LPC-2129 and LPC-
2294?
Thank you ….