Slide 2 ARM Architecture and Instruction Set
Slide 2 ARM Architecture and Instruction Set
Email: skumar.rai@thapar.edu
ARM Architecture
• The ARM is a Reduced Instruction Set Computer
(RISC) system.
User mode programs can not alter the status register except
for the condition code flags.
Flags
The upper four bits of the status register contains a set of
four flags, collectively known at condition code.
Negative (N)
Zero (Z)
Carry (C)
Overflow (V)
The condition code can be used to control the flow of the
program execution.
Exceptions
Reset when the reset pin is held low, this is normally when the
system is first turned on or when the reset button is pressed.
Data Movement
Arithmetic
Memory Access
Flow Control
Back
ARM instructions
Type of operation:
Arithmetic
Branch
Logical
Move
Arithmetic Instructions
ADD Add
SUB Subtract
MUL Multiply
CMP Compare
B Branch
BL Branch with link
Load and Store Instructions
LDR Load word
LDRB Load byte
LDRSB Load signed byte
LDRH Load half word
LDRSH Load signed half word
LDM Load multiple
LDM sp! Pop
STR Store word
STRB Store byte
STRH Store half word
STM Store multiple
STM sp! Push
Logical Instructions:
AND AND
EOR Exclusive OR
ORR OR
BIC Bit clear
TST Test
TEQ Test equivalence
Move Instructions
MOV Move
MVN Move and negate
SWP Swap
SWPB Swap byte
MRS Move program status register to register
MSR Move register to program status register
Arithmetic Instruction
Add
Syntax: ADD{cond}{S} Rd, Rn, Operand2
Usage: Adds the value in Rn to Operand2 and places the sum in Rd.
Examples:
ADD R7, R4, #99 ;adds 99 to the value in R4 and places the sum in R7
ADD R1, R2, R3 ;adds the value in R3 to the value in R2 and places
the sum in R1
Add with carry
Syntax: ADC{cond}{S} Rd, Rn, Operand2
Examples:
ADC R7, R4, #99 ;adds 99 to the value in R4 and adds another 1 if
the carry flag is set. Places the sum in R7
ADC R1, R2, R3 ;adds the value in R3 to the value in R2 and adds 1 if
the carry flag is set. Places the sum in R1
Subtract
Syntax: SUB{cond}{S} Rd, Rn, Operand2
Examples:
SUB R7, R4, #99 ;subtracts 99 from the value in R4 and places
the result in R7
Examples:
Examples:
RSB R7, R4, #99 ;subtracts the value in R4 from 99 and places the
result in R7
RSB R1, R2, R3 ;subtracts the value in R2 from the value in R3 and
places the difference in R1
Reverse subtract with carry
Syntax: RSC{cond}{S} Rd, Rn, Operand2
Examples:
Example:
MUL R5, R3, R9 ;multiply the values in R3 and R9 and places the
result in R5
Multiply and accumulate
Syntax: MLA{cond}{S} Rd, Rm, Rs, Rn
and places the least significant 32 bits of the result in register Rd.
according to the result, the V flag is not affected and the C flag is
Example:
MLA R5, R3, R9, R5 ;multiply the values in R3 and R9, add the product
Example:
Example:
UMLAL R6, R5, R3, R9 ;multiply the values in R3 and R9 and add the
product to the values in R5 and R6
Multiply - signed long
Syntax: SMULL{cond}{S} RdLo, RdHi, Rm, Rs
Example:
SMULL R6, R5, R3, R9 ;multiply the values in R3 and R9 and places
the result in R5 and R6
Multiply and accumulate - signed long
Syntax: SMLAL{cond}{S} RdLo, RdHi, Rm, Rs
Example:
SMLAL R6, R5, R3, R9 ;multiply the values in R3 and R9 and add the
product to the values in R5 and R6
Compare
Examples:
CMP R1, #9 ;set the flags as if 9 was subtracted from the value
in R1.
CMP R6, R2 ;set the flags for the result of (R6 - R2) but discard
the result
Compare negative
Syntax: CMN{cond} Rn, Operand2
Examples:
CMN R1, #9 ;set the flags as if 9 was added to the value in R1.
CMN R6, R2 ;set the flags for the result of (R6 + R2) but
discard the result
Comparisons
The only effect of the comparisons is to
Operations are:
Examples:
BNE loop
B display
Branch with link
Syntax: BL{cond} label
Examples:
BLCS toobig
BL display
Load and Store Instructions
LDR Load word
LDRB Load byte
LDRSB Load signed byte
LDRH Load half word
LDRSH Load signed half word
LDM Load multiple
LDM sp! Pop
STR Store word
STRB Store byte
STRH Store half word
STM Store multiple
STM sp! Push
Load word
Syntax: LDR{cond} Rd, address mode
Examples:
LDR r7, [r3] ;load r7 with the value in memory location with
address given by r3
Example:
LDRB r7, [r3] ;load r7 with the byte in memory location
with address given by r3 and clear the top 24 bits of r7
Load signed byte
Syntax: LDRSB{cond} Rd, address mode
Example:
LDRSB r7, [r3] ;load r7 with the byte in memory location with
address given by r3 and extend the sign bit to 32 bits
Load half word
Syntax: LDRH{cond} Rd, address mode
Example:
Example:
LDRSH r7, [r3] ;load r7 with two bytes in memory location
with address given by r3 and extend the sign bit to 32 bits
Load multiple
Syntax: LDM{cond}mode Rn{!}, reglist
Example1:
LDMIA r7!, {r3-r5, r9, r11} ;load registers r3, r4, r5, r9 and r11 with
values starting from memory location with address given by r7. After
each transfer increment r7 by 4 and update r7 at the end of the
instruction.
Example2
Pop
Syntax: LDM{cond}mode sp!, reglist
Example:
EQ Z set Equal
NE Z clear Not equal
CS or HS C set Higher or same (unsigned >=)
MVN R1, R1
SWI &11
32 Bit Addition: Direct
LDR R1, value 1
SWI &11
32 Bit addition: Indirect
LDR R0, Value 1
SWI &11
64 bit addition
Example:
Example:
Example:
STMDA r7!, {r3-r5, r9, r11} ;stores values in registers r3, r4, r5, r9 and
r11 to memory locations starting from address given by r7. After each
transfer decrement r7 by 4 and update r7 at the end of the instruction.
Push
Syntax: STM{cond}mode sp!, reglist
Example:
STMEA sp!, {r3-r5, lr} ;store values in registers r3, r4, r5 and
value in link register to the stack. The stack is 'empty
ascending'.
Logical Instructions:
AND AND
EOR Exclusive OR
ORR OR
BIC Bit clear
TST Test
TEQ Test equivalence
AND
Syntax: AND{cond}{S} Rd, Rn, Operand2
Examples:
AND r7, r4, #0xFF ;ANDs 0x000000FF with r4 and places the
result in r7
Examples:
EOR r7, r4, #0xFF ;Exclusive ORs 0x000000FF with r4 and places result
in r7
EOR r1, r2, r3 ;Exclusive ORs r3 with r2 and places the result in r1
OR
Syntax: ORR{cond}{S} Rd, Rn, Operand2
Examples:
ORR r7, r4, #0xFF ;ORs 0x000000FF with r4 and places the result in r7
Examples:
MVN r7, r4 ;invert the value in r4 and place the result into r7
Swap
Syntax: SWP{cond} Rd, Rm, [Rn]
Examples:
SWP r1, r1, [r9] ;swap the data in register r1 with the data held in
memory at address given by value in r9.
SWP r6, r8, [r2] ;load r6 with data from memory at address given by
r2 and then store data in r8 at the same memory address.
Swap byte
Syntax: SWPB{cond} Rd, Rm, [Rn]
Example:
SWPB r1, r1, [r9] ;swap least significant byte in register r1 with the
byte held in memory at address given by value in r9. Clear top 24
bits of r1.
Move program status register to register
Examples:
MRS r1, CPSR ;move the value in the CPSR into register r1
MRS r5, SPSR ;move the value in the SPSR into register r5
Move register to program status register
Syntax: MSR{cond} <psr>_<fields>, Rm
Usage: Moves the contents of register Rd into the current program status
register (CPSR) or the saved program status register (SPSR). One or more
fields must be specified; these are the control field, c, the extension field, x,
the status field, s and the flags field, f. The source register can be replaced
bits.
Examples:
MSR SPSR_c, #0x7A ;move the immediate value, #0x7A, into the control
Examples
ARM Block diagram
.
The value in the register is first shifted by a numeric constant before being
applied to the remainder of the instruction.
EQ Z set Equal
NE Z clear Not equal
CS or HS C set Higher or same (unsigned >=)
Zero offset
Pre-indexed offset
Post-indexed offset
Zero offset
Syntax: [Rn]
For example:
Both the base register, Rn, and the offset are enclosed in
square brackets in the assembly language instruction.
LDR r7, [r3, #4] ;load r7 with the value in memory location
with address given by 4 added to value in r3
LDR r7, [r3, #4]! ;load r7 with the value in memory location
with address given by 4 added to value in r3 and update r3
back
Literals or program relative
instruction.
machines code instructions, one is the actual value of the data and
Example:
A register
Examples:
ADD r2, r4, r9
MOV r6, r0
SUB r7, r3, r14
Example:
MOV r6, r0, LSL #5 ;logical shift left the value in r0 by 5 bits and
place it in r6
ADD r4, r7, r2, ASR #8 ;arithmetic shift right the value in r2 by 8
bits and add it to the value in r7. Place the sum in r4.
SUB r3, r9, r12, RRX ;extended rotate right the value in r12 by
one bit and subtract it from the value in r9. Place the difference
in r3.
A register shifted by another register.
The value in the register is first shifted by a value in another register before
being applied to the remainder of the instruction.
Only the least significant byte is used as the shift value so that same shift
occurs if the value held by the register is 0x00000014 or 0x031A0014.
MOV r6, r0, LSL r3 ;logical shift left the value in r0 by the
value in r3 and place it in r6
ADD r4, r7, r2, ASR r8 ;arithmetic shift right the value in r2
by the value in r8 and add it to the value in r7. Place the sum
in r4.
SUB r3, r9, r12, ROR r1 ;rotate right the value in r12 by the
value in r1 and subtract it from the value in r9. Place the
difference in r3.
Logical shift left (LSL)
The value in the register is shifted to the left by a specified
number, n, of bits and the right hand n bits are set to 0.
Example:
Example:
Execute instruction MOV r3, r2, LSR #2
of bits and the left hand n bits are set to the value of the most
Example:
Before r2 holds the value: 1000 1101 1000 1001 0011 0100 0010 0001
After r3 holds the value: 1110 0011 0110 0010 0100 1101 0000 1000
or
Example:
Execute instruction MOV r3, r2, ROR #2
Example:
brackets.
separated by commas.
pop.
Examples:
{r0, r13, pc} ;Registers r0, r13 (stack pointer) and r15
(program counter).
{r2, r5-r8, sp} ;Registers r2, r5, r6, r7, r8 and r13 (stack
pointer).
back
back1
Modes for multiple load and store
There are four modes for LDM and STM; these are increment after
(IA), increment before (IB), decrement after (DA) and decrement
before (DB).
back
Conditional Execution
MOVCS R0, R1
The instruction will move the value of the register R1 into the R0
register only when the carry flag has been set. R0 will remain unaffected
if the C flag was clear
Greater and the Less conditions are for use with signed numbers.
Higher and Lower conditions are for use with unsigned numbers.
These condition codes only really make sense after a comparison
(CMP) instruction
Most data-processing instructions can also update the condition codes
according to their result.
Placing an “S” after the mnemonic will cause the flags to be updated.
MOV R0, #0 will move the value 0 into the register R0 without setting
the flags.
MOVS R0, #0 will do the same, move the value 0 into the register R0,
but it will also set the condition code flags accordingly,
the Zero flag will be set, the Negative flag will be reset
and the Carry and overflow flags will not be affected.
MOVS R0, R1
MOVEQS R0, R2
MOVEQ R0, R3
Thus the second instruction is only executed if the Z flag is set, i.e., the
value of R1 was zero. If the value of R1 was not zero the instruction is
skipped.
Thus the third instruction is only executed if both R1 and R2 are both
zero.
Data Processing Operands: <op1>
The majority of the instructions relate to data processing of some form.
One of the operands to these instructions is routed through the Barrel Shifter.
This can be very useful when dealing with lists, tables and other
complex data structures.
An <op1> argument may come from one of two sources, a constant value or
a register, and can be modified in five different ways.
Unmodified Value
MOV R0, #1234 Will move the immediate constant value 1234 10 into
the register R0
MOV R0, R1 Will move the value in the register R1 into the register R0.
Logical Shift Left
This will take the value of a register and shift the value up, towards the most
significant bit, by n bits.
The number of bits to shift is specified by either a constant value or another
register.
The lower bits of the value are replaced with a zero.
This is a simple way of performing a multiply by a power of 2 (×2 n).
MOV R0, R1, LSL #2 R0 will become the value of R1 shifted left by 2 bits.
The value of R1is not changed.
MOV R0, R1, LSL R2 R0 will become the value of R1 shifted left by the
number of bits specified in the R2 register.
R0 is the only register to change, both R1and R2 are not affected by this operation.
If the instruction is to set the status register, the carry flag (C) is the last bit that
was shifted out of the value.
Logical Shift Right
As the most significant bit is used to hold the sign, this means the
sign of the value is maintained, thus providing a signed divide by
2n operation (÷2n).
MOV R0, R1, ASR #2 Register R0 will become the value of register R1
shifted to the right by 2 bits, with the sign
maintained.
MOV R0, R1, ASR R2 Register R0 will become the value of the
register R1 shifted to the right by the number of
bits specified by the R2 register.
R1 and R2 are not altered by this operation.
Rotate Right
In the Rotate Right operation, the least significant bit is copied into the carry
(C) flag, while the value of the C flag is copied into the most significant bit
of the value.
In this way none of the bits in the value are lost, but are simply moved from
the lower bits to the upper bits of the value.
MOV R0, R1, ROR #2 This will rotate the value of R1 by two bits. The most
significant bit of the resulting value will be the same as
the least significant bit of the original value.
The second most significant bit will be the same as the Carry flag.
In the S version the Carry flag will be set to the second least significant bit
of the original value.
The value of R1 is not changed by this operation.
MOV R0, R1, ROR R2 Register R0 will become the value of the register R1
rotated to the right by the number of bits specified by the
R2 register. R1 and R2 are not altered by this operation.
Rotate Right Extended
LDR R0, [R1] Will load the register R0 with the 32-bit word at the memory
address held in the register R1.
In this instruction there is no offset specified, so an offset of zero is
assumed.
The value of R1 is not changed in this instruction.
LDR R0, [R1, #4] Will load the register R0 with the word at the
memory address calculated by adding the constant
value 4 to the memory address contained in the R1
register.
The new address is also written back into the base register, R1.
LDR R0, [R1], R2 Loads the register R0 with the value at the memory address
held in the base register, R1.
It will then calculate the new value for the base register by adding the value
in the offset register, R2, to the current value of the base register.
The offset register, R2, is not altered by this operation.
LDR R0, [R1], R2, LSL #2 First loads the 32-bit value at the memory address
contained in the base register, R1, into the destination
register, R0.
It will then calculate the new value for the base register by adding the
current value to the value obtained by shifting the value in the offset register,
R2, left by 2 bits.
The offset register, R2, will not be affected by this operation.
Example
Pre-indexing with write back
All Multiply instructions take two register operands as the input to the multiplier.
MUL Multiplies the values of two registers together, truncates the result to 32 bits,
and stores the result in a third register.
MLA Multiplies the values of two registers together, add the value of a third
register, truncates the result to 32 bits, and stores the result in a fourth register.
There are four Multiply instructions that produce 64-bit results (long multiply).
Two of the variants multiply the values of two registers together and store the 64-
bit result in third and fourth registers.
The signed variants produce a different result in the most significant 32 bits if
either or both of the source operands is negative.
The remaining two variants multiply the values of two registers together, add the
64-bit value from the third and fourth registers and store the 64-bit result back into
those registers (third and fourth).
There are signed SMLAL and unsigned UMLAL variants. These instructions
perform a long multiply and accumulate.
All four long multiply instructions can optionally set the N (Negative) and Z (Zero)
condition code flags.
Data Processing Instructions
Most data-processing instructions take two source
operands, though Move and Move Not take only one.
CMP, CMN, TST and TEQ always update the condition code flags.
The assembler automatically sets the S bit in the instruction for them.
MOVCS R1, R1, LSL# 0 x05 or MOVEQS R1, R1, LSL# 0x05
SWI &11
Result: R1 = 0x000000A0
D) Arithmetic shift by value contained in
register R2
A) Direct method
B) Indirect method
C) Using Barrel shifter
A) Direct Method
SWI &11
Conditional Codes
The property of conditional execution is common to all
ARM instructions.
NV Never
Assembler Format
Examples
Logical Instructions
BIC: Clear specified bits
Example
ORR : Logical OR
Example
EOR: Logical exclusive OR
Example
TEQ: Test Equivalence
MVN <dest>,<rhs>
In general, -n = NOT (n-1). This means that to load a
negative number, you subtract one from its positive value
Examples:
Examples:
Example:
;Add the 64-bit number in R2,R3 to that in R0,R1
ADDS R0,R0,R2 ;Add the lower words, getting carry
ADC R1,R1,R3 ;Add upper words, using carry SUB Subtract
Subtraction: SUB
This instruction subtracts the <rhs> operand from the
<lhs> operand, storing the result in <dest>.
Examples:
Example:
;Subtract the 64-bit number in R2,R3 from that in R0,R1
SUBS R0,R0,R2 ;Sub the lower words, getting borrow
SBC R1,R1,R3 ;Sub upper words, using borrow
RSB Reverse subtract
Examples
RSC: Reverse subtract with carry
CMP: Compare
Compare Negative
The CMN instruction compares two numbers, but
negates the right hand side before performing the
comparison.
CMN R0,#1 ;Compare R0 with -1
Load and store multiple registers in ARM
Instructions are available in ARM to load and store multiple
registers.
PUSH: Store multiple registers onto the stack and update the
stack pointer.
POP: Load multiple registers off the stack, and update the stack
pointer
In LDM and STM instructions:
src DCD 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4
dst DCD 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,0 ,0 ,0 ,0
Addressing Modes of ARM
1. Data Processing Operands
One’s complement
MVN R1, R1
SWI &11
16 Bit Addition: Direct
LDR R1, value 1
SWI &11
16 Bit addition: Indirect
LDR R0, Value 1
SWI &11
Find the larger of two numbers.
LDR R1, Value 1
LDR R2, Value 2
CMP R1, R2
BHI Done
MOV [R0], R2
SWI &11
Done MOV [R0], R1
SWI &11
64 bit addition
LDR R1, value 1
LDR R2, value 2
LDR R3, value 2
LDR R4, value 4
ADDS R5, R1, R3
ADC R6, R2, R4
SWI &11
Write a program in ARM assembly language to copy consecutive word
from source to destination in memory using