MODULE 2
Introduction to the ARM Instruction Set
BY: GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
ARM Instruction Set
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Data Processing Instructions
- move instructions,
- arithmetic instructions,
- logical instructions,
- comparison instructions, and
- multiply instructions.
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Move Instructions
GURURAJ S CSE DEPT
Examples
GURURAJ S CSE DEPT
Condition Embedded in ADD Instruction
{cond} Rd, N
MOVEQ r0, r1 ; If zero flag set then…
; ... r0 = r1
Example
AREA MOV1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
SUBS R0,R0,#4 ; FIRST SUBTRACT 5 AND NEXT 4
MOVEQ R2,R1
MOV R3,#10
BACK B BACK
END
GURURAJ S CSE DEPT
MOVS Rd, N
MOVS r0,r1 ; r0 = r1
; ... and set flags
GURURAJ S CSE DEPT
Example
AREA MOV2, CODE, READONLY
START
MOV R0,#0
MOV R1,#6
MOVS R2,R0 ; Z
MOVS R3,R1 ; z
BACK B BACK
END
GURURAJ S CSE DEPT
Barrel Shifter
GURURAJ S CSE DEPT
-- the MUL (multiply),
-- CLZ (count leading zeros), and
-- QADD (signed saturated 32-bit add)
GURURAJ S CSE DEPT
Example
GURURAJ S CSE DEPT
Barrel shifter operations
GURURAJ S CSE DEPT
Note: x represents the register being shifted and y represents the shift amount.
GURURAJ S CSE DEPT
Barrel Shifter - Left Shift
LSL #5 = multiply by 32
GURURAJ S CSE DEPT
Logical Shift Left (LSL)
Destination
CF
0
Barrel Shifter - Right Shifts
Logical Shift Right
LSR #5 = divide by 32
GURURAJ S CSE DEPT
Destination
CF
Logical Shift Right
...0
Arithmetic Shift Right
Arithmetic Shift Right
ASR #5 = divide by 32
GURURAJ S CSE DEPT
Destination
CF
Arithmetic Shift Right
Sign bit shifted in
Barrel Shifter - Rotations
Rotate Right (ROR)
•Similar to an ASR but the bits wrap around as they leave the LSB and appear as the MSB.
e.g. ROR #5
GURURAJ S CSE DEPT
Destination
CF
Rotate Right
Barrel Shifter - Rotations �Rotate Right Extended (RRX)�
Rotate Right Extended (RRX)
• This operation uses the CPSR C flag as a 33rd bit.
GURURAJ S CSE DEPT
Destination
CF
Rotate Right through Carry
MOVS - example
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Arithmetic Instructions
GURURAJ S CSE DEPT
Example-1
GURURAJ S CSE DEPT
Example-2
GURURAJ S CSE DEPT
Example-3
GURURAJ S CSE DEPT
Using the Barrel Shifter with Arithmetic�Instructions
GURURAJ S CSE DEPT
Example-1
AREA MOV2, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
add R2,R0,R1, LSL #0x2
BACK B BACK
END
GURURAJ S CSE DEPT
Example-2
AREA ADD1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
SUBS R0,R0,#4 ; FIRST SUBTRACT 5 AND NEXT 4
ADDEQ R2,R1,R0
MOV R3,#10
BACK B BACK
END
GURURAJ S CSE DEPT
Logical Instructions
GURURAJ S CSE DEPT
Example-1
GURURAJ S CSE DEPT
Example-2
GURURAJ S CSE DEPT
Example-3
AREA ADD1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
AND R2,R1,R0
BACK B BACK
END
GURURAJ S CSE DEPT
Example-4
AREA ADD1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
AND R2,R1,R0
BACK B BACK
END
GURURAJ S CSE DEPT
Example-5
AREA ADD1, CODE, READONLY
START
mov R0,#15
mov R1,#5
bic R2,R0,R1 ; r2=r0 & (~R1)
BACK B BACK
END
GURURAJ S CSE DEPT
Comparison Instructions
GURURAJ S CSE DEPT
CMP Example
GURURAJ S CSE DEPT
CMN Instruction
RO=50
R1=10
CMN RO, R1
GURURAJ S CSE DEPT
TEQ
GURURAJ S CSE DEPT
TEQ- Example
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
TEQ R0,R1
BACK B BACK
END
GURURAJ S CSE DEPT
TST - Instruction
GURURAJ S CSE DEPT
TST – EXAMPLE-1
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#9
TST R0,R1 ; 6 & 9 = 0 Z
BACK B BACK
END
GURURAJ S CSE DEPT
TST – EXAMPLE-2
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
TST R0,R1 ; 6 & 5 = 4 z
BACK B BACK
END
GURURAJ S CSE DEPT
Multiply Instructions
GURURAJ S CSE DEPT
MLA- EXAMPLE-1
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MOV R2,#1
MLA R3,R0,R1,R2 ;R3=(R0*R1) + R2
BACK B BACK
END
GURURAJ S CSE DEPT
MLA- EXAMPLE-2
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MOV R2,#1
MLA R0,R0,R1,R2 ;R0=(R0*R1) + R2 NOT ALLOWED
BACK B BACK
END
GURURAJ S CSE DEPT
MLA- EXAMPLE-3
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MOV R2,#1
MLA R1,R0,R1,R2 ;R1=(R0*R1) + R2 ALLOWED
BACK B BACK
END
GURURAJ S CSE DEPT
MLA- EXAMPLE-4
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MOV R2,#1
MLA R2,R0,R1,R2 ;R2=(R0*R1) + R2 ALLOWED
BACK B BACK
END
GURURAJ S CSE DEPT
MUL – EXAMPLE-1
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MUL R3,R0,R1 ;R3=R0*R1
BACK B BACK
END
GURURAJ S CSE DEPT
MUL- EXAMPLE-2
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MUL R0,R0,R1 ; THIS REGISTER SEQUENCE NOT ;ALLOWED
BACK B BACK
END
GURURAJ S CSE DEPT
MUL- EXAMPLE-2
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
MUL R1,R0,R1 ; THIS REGISTER SEQUENCE IS ;ALLOWED BECAUSE N IS COPIED TO TEMP REG
BACK B BACK
END
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
SMLAL - EXAMPLE
AREA TEST, CODE, READONLY
ENTRY
START
ldr R0,=0x5
ldr R1,=0x5
LDR R2,=0X2
LDR R3,=0X4
SMLAL R3,R2,R0,R1 ;[R3Lo,R2Hi]=[R3,R2]+(R0*R1)
BACK B BACK
END
GURURAJ S CSE DEPT
SMULL - EXAMPLE
AREA TEST, CODE, READONLY
ENTRY
START
ldr R0,=0x5
ldr R1,=0x5
LDR R2,=0X2
LDR R3,=0X4
SMULL R3,R2,R0,R1 ;[R3Lo,R2Hi]=(R0*R1)
BACK B BACK
END
GURURAJ S CSE DEPT
UMULL- Example
AREA UMULL1, CODE, READONLY
START
ldr R0,=0xf0000002
ldr R1,=0x00000002
umull r3,R2,R1,R0
MOV R3,#10
BACK B BACK
END
GURURAJ S CSE DEPT
UMLAL- Example
AREA TEST, CODE, READONLY
ENTRY
START
ldr R0,=0x5
ldr R1,=0x5
LDR R2,=0X2
LDR R3,=0X4
UMLAL R3,R2,R0,R1 ;[RdHi, RdLo] = [RdHi, RdLo] + (Rm *Rs)
BACK B BACK
END
GURURAJ S CSE DEPT
Branch Instructions
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Forward and Backward branch
GURURAJ S CSE DEPT
BL (Branch with Link instruction)
GURURAJ S CSE DEPT
BX (Branch Exchange), BLX (Branch Exchange with Link)
pc = Rm & 0xfffffffe, T = Rm & 1
GURURAJ S CSE DEPT
Load-Store Instructions
- single-register transfer,
- multiple-register transfer, and
- swap.
GURURAJ S CSE DEPT
Single-Register Transfer
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Single-Register Load-Store Addressing Modes
-- preindex with writeback,
-- preindex, and
-- postindex
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Preindexing with writeback: (Base Register Updated)
GURURAJ S CSE DEPT
Preindexing: (Base Register not updated)
GURURAJ S CSE DEPT
Postindexing: (Base Register Updated)
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Multiple-Register Load-Store Instructions
GURURAJ S CSE DEPT
-- t is the number of cycles required for each sequential access to memory.
GURURAJ S CSE DEPT
Addressing mode for load-store multiple instructions.
GURURAJ S CSE DEPT
Example
GURURAJ S CSE DEPT
Graphical Representation (IA).
GURURAJ S CSE DEPT
Graphical Representation (IB).
GURURAJ S CSE DEPT
Load-store multiple pairs when base update used.
GURURAJ S CSE DEPT
EXAMPLE-1 LDMIA
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R2,=CVALUE ; ADDRESS OF CODE REGION
LDMIA R2!,{R3-R6}
BACK B BACK
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
END
GURURAJ S CSE DEPT
EXAMPLE-2 LDMIB
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R2,=CVALUE ; ADDRESS OF CODE REGION
LDMIB R2!,{R3-R6}
BACK B BACK
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
END
GURURAJ S CSE DEPT
EXAMPLE (STMIA)
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R2,=CVALUE ; ADDRESS OF CODE REGION
LDMIA R2!,{R3-R6}
LDR R7,=DVALUE
STMIA R7!,{R3-R6}
BACK B BACK
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
AREA DATA1, DATA, READWRITE
DVALUE DCD 0X00
END
GURURAJ S CSE DEPT
EXAMPLE (STMIB and LDMDA)
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R0,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMIB r0!, {r1-r3}
MOV r1, #1
MOV r2, #2
MOV r3, #3
LDMDA r0!, {r1-r3}
BACK B BACK
GURURAJ S CSE DEPT
EXAMPLE (STMIB and LDMDB)
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R0,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMIB r0!, {r1-r3}
MOV r1, #1
MOV r2, #2
MOV r3, #3
LDMDB r0!, {r1-r3}
BACK B BACK
end
GURURAJ S CSE DEPT
Stack Operations
-- The push operation (placing data onto the stack) uses a store multiple instruction
-- ascending (A) or
-- descending (D).
Ascending stacks grow towards higher memory addresses;
Descending stacks grow towards lower memory addresses.
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Example-STMFD
GURURAJ S CSE DEPT
Example-STMED
GURURAJ S CSE DEPT
Example-1�POPING ONE AT A TIME
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR SP,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMFA SP!, {r1-r3}
LDMFA SP!,{R4}
LDMFA SP!,{R5}
LDMFA SP!,{R6}
BACK B BACK
END
Note: STMFA == STMIB, LDMFA == LDMDA
GURURAJ S CSE DEPT
Example-2�POPING ALL 3 ELEMENTS
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR SP,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMFA SP!, {r1-r3}
LDMFA SP!,{R4-R6}
BACK B BACK
END
GURURAJ S CSE DEPT
Example-3�POPING FROM EMPTY STACK
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR SP,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMFA SP!, {r1-r3}
LDMFA SP!,{R4-R6}
LDMFA SP!,{R7} ; UNDER FLOW
BACK B BACK
END
GURURAJ S CSE DEPT
Example-4�Full Descending Stack
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR SP,=0X4000001F
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMFD SP!, {r1-r3}
LDMFD SP!,{R4-R6}
LDMFD SP!,{R7}
BACK B BACK
END
GURURAJ S CSE DEPT
Checking Stack Under Flow
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Checking Stack Under Flow
; check for stack overflow
Mov r10,rn
CMP sp, r10
BLT _stack_underflow ; condition
GURURAJ S CSE DEPT
Swap Instruction
—it reads and writes a location in the same bus operation, preventing any other instruction from reading or writing to that location until it completes.
GURURAJ S CSE DEPT
Swap Syntax
GURURAJ S CSE DEPT
Note: Swap cannot be interrupted by any other instruction or any other bus access. The system “holds the bus” until the transaction is complete
Example-1
GURURAJ S CSE DEPT
Example-2
GURURAJ S CSE DEPT
EXAMPLE-3
AREA ASCENDING , CODE, READONLY
ENTRY ;Mark first instruction to execute
START
LDR R0,=0X11111111
LDR R1,=CVALUE
LDR R2,[R1]
LDR R1,=DVALUE
STR R2,[R1]
SWP R3,R0,[R1]
BACK B BACK
CVALUE DCD 0X22222222
AREA DATA1, DATA, READWRITE
DVALUE DCD 0X22222222
END
GURURAJ S CSE DEPT
Software Interrupt Instruction
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Example
AREA HelloW,CODE,READONLY; declare area
SWIWrite EQU 0x00 ; Angel SWI number
ENTRY ; code entry point
START ADR r1, TEXT-1 ; r1 -> “Hello World” -1
LOOP MOV r0, #0x1 ; Angel write char in [r1]
LDRB r2, [r1,#1]! ; get the next byte
CMP r2, #0 ; check for text end
SWINE SWIWrite ; if not end print ..
BNE LOOP ; .. and loop back
MOV r0, #0x18 ; Angel exception call
LDR r1, =0x20026 ; Exit reason
SWI &11 ; end of execution
TEXT = "Hello World",0xA,0xD,0
END ; end of program source
GURURAJ S CSE DEPT
Program Status Register Instructions
GURURAJ S CSE DEPT
MRS, MSR Syntax
GURURAJ S CSE DEPT
GURURAJ S CSE DEPT
Example
GURURAJ S CSE DEPT
Coprocessor Instructions
- data processing,
- register transfer, and
- memory transfer instructions.
GURURAJ S CSE DEPT
Co-processor Instruction Syntax
GURURAJ S CSE DEPT
Example�
GURURAJ S CSE DEPT
Here CP15 register-0 contains the processor identification number. This register is copied into the general-purpose register r10.
Loading Constants
GURURAJ S CSE DEPT
Thank You
GURURAJ S CSE DEPT