MODULE 2
MODULE 2
MODULE 2
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
• For example to move the value zero to
destination register and set the condition
flags:
MOVS Rd, N
MOVS r0,r1 ; r0 = r1
; ... and set flags
Example
AREA MOV2, CODE, READONLY
START
MOV R0,#0
MOV R1,#6
MOVS R2,R0 ; Z
MOVS R3,R1 ; z
BACK B BACK
END
3.1.2 Barrel Shifter
• In MOV instruction N is a simple register/immediate value.
• A unique and powerful feature of the ARM processor is the ability to shift
the 32-bit binary pattern in one of the source registers left or right by a
specific number of positions before it enters the ALU.
• This shift increases the power and flexibility of many data processing
operations.
• There are data processing instructions that do not use the barrel shift, for
example,
-- the MUL (multiply),
-- CLZ (count leading zeros), and
-- QADD (signed saturated 32-bit add)
Example
Barrel shifter operations
CF Destination 0
Barrel Shifter - Right Shifts
Logical Shift Right
•Shifts right by the specified amount (divides by
powers of two)
• e.g.
LSR #5 = divide by 32
...0 Destination CF
Arithmetic Shift Right
Arithmetic Shift Right
•Shifts right (divides by powers of two) and
preserves the sign bit, for 2's complement
operations.
•e.g.
ASR #5 = divide by 32
Arithmetic Shift Right
Destination CF
Rotate Right
Destination CF
Barrel Shifter - Rotations
Rotate Right Extended (RRX)
Rotate Right Extended (RRX)
• This operation uses the CPSR C flag as a 33rd
bit.
•Rotates right by 1 bit. Encoded as ROR #0.
Destination CF
MOVS - example
3.1.3 Arithmetic Instructions
Example-1
• Subtract instruction subtracts a value stored in
register r2 from a value stored in register r1.
The result is stored in register r0.
Example-2
• Reverse Subtract Instruction (RSB) subtracts r1
from the constant value #0, writing the result
to r0. You can use this instruction to negate
numbers.
Example-3
• SUBS instruction is useful for decrementing loop counters. In
this example we subtract the immediate value one from the
value one stored in register r1. The result value zero is written
to register r1. The cpsr is updated with the ZC flags being set.
3.1.4 Using the Barrel Shifter with
Arithmetic Instructions
• The wide range of second operand shifts available on
arithmetic and logical instructions is a very powerful feature
of the ARM instruction set.
Example-1
AREA MOV2, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
add R2,R0,R1, LSL #0x2
BACK B BACK
END
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
3.1.5 Logical Instructions
• Logical instructions perform bitwise logical
operations on the two source registers.
Example-1
• Logical OR operation between registers r1 and
r2. r0 holds the result.
Example-2
• A more complicated logical instruction called
BIC, which carries out a logical bit clear.
Example-3
AREA ADD1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
AND R2,R1,R0
BACK B BACK
END
Example-4
AREA ADD1, CODE, READONLY
START
MOV R0,#5
MOV R1,#6
AND R2,R1,R0
BACK B BACK
END
Example-5
AREA ADD1, CODE, READONLY
START
mov R0,#15
mov R1,#5
bic R2,R0,R1 ; r2=r0 & (~R1)
BACK B BACK
END
3.1.6 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.
CMP Example
CMN Instruction
• The CMN instruction adds the value of N to
the value in Rn.
• This is the same as an ADDS instruction,
except that the result is discarded.
• The conditional flags are updated accordingly.
RO=50
R1=10
CMN RO, R1
TEQ
• Test Equivalence.
• The TEQ instruction performs a bitwise
Exclusive OR operation on the value in Rn and
the value of N. the result is discarded.
• Use the TEQ instruction to test if two values
are equal, without affecting the V or C flags
(as CMP does). It affects the N and Z flags.
TEQ- Example
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
TEQ R0,R1
BACK B BACK
END
TST - Instruction
• This instruction tests the value in a register
against N. It updates the condition flags on
the result, but does not place the result in any
register.
• The TST instruction performs a bitwise AND
operation on the value in Rn and the value
of N.
• This is the same as an ANDS instruction,
except that the result is discarded.
TST – EXAMPLE-1
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#9
TST R0,R1 ;6&9=0Z
BACK B BACK
END
TST – EXAMPLE-2
AREA TEST, CODE, READONLY
ENTRY
START
MOV R0,#6
MOV R1,#5
TST R0,R1 ;6&5=4z
BACK B BACK
END
3.1.7 Multiply Instructions
• The multiply instructions multiply the
contents of a pair of registers.
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
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
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
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
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
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
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
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
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
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
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
3.2 Branch Instructions
• A branch instruction changes the flow of
execution or is used to call a routine.
• Example:
BX (Branch Exchange), BLX (Branch
Exchange with Link)
• The branch exchange (BX) and branch exchange with link (BLX) are the third type
of branch instruction.
• The T bit in the cpsr is updated by the least significant bit of the branch register.
• Similarly the BLX instruction updates the T bit of the cpsr with the least
significant bit and additionally sets the link register with the return address.
• pc = label, T = 1
• pc = Rm & 0xfffffffe, T = Rm & 1
• lr = address of the next instruction after the BLX
3.3 Load-Store Instructions
• Load-store instructions transfer data between
memory and processor registers.
• There are three types of load-store
instructions:
- single-register transfer,
- multiple-register transfer, and
- swap.
3.3.1 Single-Register Transfer
• These instructions are used for moving a single data item in
and out of a register.
• The datatypes supported are signed and unsigned words (32-
bit), halfwords (16-bit), and bytes.
• For example, LDR can only load 32-bit words on a memory
address that is a multiple of four bytes—0, 4, 8, and so on.
• This example shows a load from a memory address contained
in register r1, followed by a store back to the same address in
memory.
3.3.2 Single-Register Load-Store
Addressing Modes
• The ARM instruction set provides different
modes for addressing memory.
• These modes incorporate one of the indexing
methods:
-- preindex with writeback,
-- preindex, and
-- postindex
Preindexing with writeback: (Base
Register Updated)
Preindexing: (Base Register not
updated)
Postindexing: (Base Register Updated)
3.3.3 Multiple-Register Load-Store
Instructions
• Load-store multiple instructions can transfer
multiple registers between memory and the
processor in a single instruction.
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
END
EXAMPLE-2 LDMIB
AREA ASCENDING , CODE, READONLY
CVALUE
DCD 0X44444444 ;
DCD 0X11111111 ;
DCD 0X33333333 ;
DCD 0X22222222 ;
END
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 ;
END
EXAMPLE (STMIB and LDMDA)
AREA ASCENDING , CODE, READONLY
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
EXAMPLE (STMIB and LDMDB)
AREA ASCENDING , CODE, READONLY
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
3.3.3.1 Stack Operations
• The ARM architecture uses the load-store multiple
instructions to carry out stack operations.
– The pop operation (removing data from a stack) uses a load
multiple instruction;
-- The push operation (placing data onto the stack) uses
a store multiple instruction
• We have to decide whether the stack will grow up or
down in memory. A stack is either
-- ascending (A) or
-- descending (D).
Ascending stacks grow towards higher memory
addresses;
Descending stacks grow towards lower memory
addresses.
• In 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 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).
Example-STMFD
• The STMFD instruction pushes registers onto the stack,
updating the sp. Below example push onto a full descending
stack.
• When the stack grows the stack pointer (sp) points to the last
full entry in the stack. Hence it is called as full and descending
because address decreases.
Example-STMED
• The STMED instruction pushes the registers
onto the stack but updates register sp to point
to the next empty location downward.
Example-1
POPING ONE
AREA ASCENDING , CODE, READONLY
AT A TIME
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
LDR SP,=0X40000000
LDR R1,=0x00000009
LDR R2,=0x00000008
LDR R3,=0x00000007
STMFA SP!, {r1-r3}
LDMFA SP!,{R4-R6}
BACK B BACK
END
Example-3
POPING FROM EMPTY STACK
AREA ASCENDING , CODE, READONLY
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
Example-4
Full Descending Stack
AREA ASCENDING , CODE, READONLY
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
Checking Stack Under Flow
• In handling the stack there are three attributes that need
to be preserved:
– The stack base,
– the stack pointer, and
– the stack limit.
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
Example-2
• This example shows a simple data guard that can be used to
protect data from being written by another task.
• The SWP instruction “holds the bus” until the transaction is
complete.
END
3.4 Software Interrupt Instruction
• A software interrupt instruction - (SWI)
• This causes a software interrupt exception,
• Provides a mechanism for applications to call
operating system routines.
• When the processor executes an SWI
instruction, it sets the program counter pc to
the offset 0x8 in the vector table.
• The processor mode changes to SVC, This
allows an operating system routine to be
called in a privileged mode.
• Each SWI instruction has an associated SWI
number, which is used to represent a
particular function call or feature.
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
3.5 Program Status Register
Instructions
• The ARM instruction set provides two
instructions to directly control a program
status register (psr).
• 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.
MRS, MSR Syntax
• In syntax the label called fields can be any
combination of control (c), extension (x),
status (s), and flags (f ).
Example
• Register r1 is then copied back into the cpsr, which enables IRQ interrupts.
•This code preserves all the other settings in the cpsr and only modifies the I bit in
the control field.
•This example is in SVC mode. In user mode you can read all cpsr bits, but you can
only update the condition flag field f.
3.5.1 Coprocessor Instructions
• Coprocessor instructions are used to extend the instruction set.
• Note that these instructions are only used by cores with a coprocessor.
Co-processor Instruction Syntax
•The opcode fields describe the operation to take place on the coprocessor.
•The Cn, Cm, and Cd fields describe registers within the coprocessor.
Here CP15 register-0 contains the processor identification number. This register is
copied into the general-purpose register r10.
3.6 Loading Constants
• In ARM there is no ARM instruction to move a 32-bit
constant into a register.
• To aid programming there are two pseudo
instructions to move a 32-bit value into a register.