19ECE304 - Chapter 3,5 - ARM
19ECE304 - Chapter 3,5 - ARM
Interfacing (3 0 3 4)
Mr. Giriraja C V
Ms. Jayashree M Oli
Ms. Priya B K
1
Chapter 3 – ARM Assembly Language Programming
Chapter 5 – The ARM Instruction set- 5.1 to 5.15, 5.20, 5.22, 5.23
Instruction Set:
• Two instruction sets
• ARM
• Standard 32-bit instruction set
• THUMB
• 16-bit compressed form
• Code density better than most CISC
• Dynamic decompression in pipeline
2
ARM Instruction Set:
• Features:
• Load/Store architecture
• 3-address data processing instructions
• Conditional execution
• Load/Store multiple registers
• Shift & ALU operation in single clock cycle
4
ARM Instruction Set: ARM Instruction Set
Data processing
instructions
Data transfer
instructions
Block transfer
instructions
Multiply instructions
Software interrupt
instructions 5
Data types:
• Supports six data types:
1. 8 bit signed and unsigned bytes
2. 16 bit signed and unsigned half words, aligned on 2 byte boundaries
3. 32 bit signed and unsigned words, aligned on 4 byte boundaries
• ARM instructions are 32 bit word and aligned on 4 byte boundaries
• Thumb instructions are 16 bit half word and aligned on 2 byte boundaries
• Operands are 32 bit
• Shorter data types are supported by data transfer instructions
• When byte is loaded from memory, it is zero or sign extended to 32 bits and treated as a 32 bit
value for internal processing
• ARM coprocessor supports floating point values
6
Data Processing Instructions:
• Arithmetic and logical operations
• Modifies the data values
• 3-address format – for ex: ADD R1, R2, R3
• Two 32-bit operands
(operand 1 is register, operand 2 is register or immediate data (literals) or a shifted register)
• 32-bit result placed in a register
• Barrel shifter for operand 2 allows full 32-bit shift within the instruction cycle
• Each operand register and result register is independently specified in the instruction
• Data can be unsigned integer or signed 2’s complement integer
• Shift applied can be arithmetic or logical or rotate
• C flag is set for addition, subtraction and compare instruction
• None of the three register should be PC except for shift operation
• If PC is used as a source, the value given is the address of the current instruction + 8 bytes
• PC can
7
be destination register if it is a branch instruction
Data Processing Instructions:
• Arithmetic operations
• ADD, ADC, SUB, SBC, RSB, RSC
• Bit-wise logical operations
• AND, EOR, ORR, BIC
• Register movement operations
• MOV, MVN
• Comparison operations
• TST, TEQ, CMP, CMN
8
Arithmetic Operations:
• Performs binary
arithmetic on
two 32 bit
operands
• Operands may
be unsigned or
2’s complement
signed integer
• C bit in CPSR
is effected
9
Subtraction:
Bit wise logical operations:
• Performs Boolean
logic operation on
each bit pair of the
input operands
Register movement operation:
Compare operations:
19
Conditional execution of instruction:
29
Shifted Register operands:
35
Multiply Instructions:
• Special form of data processing instruction
• 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
37
38
43
44
Data Processing Instructions:
Conditional codes
e.g.:
+
Data processing instructions if (z==1) R1=R2+(R3*4)
+ compiles to
Barrel shifter
EQADDS R1,R2,R3, LSL #2
=
( SINGLE INSTRUCTION ! )
Powerful tools for efficient coded programs
45
Data Transfer Instructions:
• Moves data between register and memory
46
Data Transfer Instructions:
Single register load and store instructions
• Load/store instructions
• Flexible
• Used to move signed and unsigned Word, Half Word and Byte to and from registers
• Can be used to load PC (if target address is beyond branch instruction range)
LDRSH Load Signed Half Word STRSH Store Signed Half Word
92
95
Breakpoint instruction (BKPT) V5T only):
• Used for software debugging
• Processor will break from normal instruction execution and enters debugging
• Leads to pre fetch abort
• BRK
• Unconditional instructions
Count leading zeros (CLZ -architecture v5T only):
• Used to renormalize the numbers
• The instruction sets Rd to the number of the bit position of the most significant 1 in Rm.
• If Rm is zero Rd will be set to 32.
Privileged modes:
• Used to handle
1. Exceptions
2. Supervisor calls
3. Software interrupts
• Privileged mode is entered through controlled mechanism
• Using memory protection, privileged modes allow a fully protected operating system to be
built
• Used for trapping errant software
• Current operating mode is defined by the bottom five bits of CPSR
100
SPSR- Saved Program Status Register:
• All privileged mode except system mode is associated with SPSR
• Used to save the state of CPSR when any privileged mode is entered
• Once it is returned from privileged mode, SPSR is restored back to CPSR
• If privileged mode is used as a re-entrant then SPSR must be copied to general register
103
Exception:
• Used to handle unexpected events which arise during the execution of program such as
• Interrupt
• Memory fault
• Software interrupt
• Undefined instruction traps
• System reset function
It is divided into three groups
1. Exception generated as the direct effect of executing an instruction
Ex: Software interrupt, undefined instruction, pre fetch abort
2. Exception generated as the side effect of executing an instruction
Ex: data abort, memory fault
3. Exception generated externally unrelated to the instruction flow
Ex: Reset, IRQ, FIQ 104
Exception entry:
• When exception occurs, ARM completes the execution of the current instruction and check
whether it is due to side effect or due to external event or direct effect
• Processor changes the operating mode to the particular exception
• Saves the address of the next instruction in R14
• Saves the value of CPSR in SPSR_mode of the exception mode
• Disables IRQ by making bit7 of CPSR as 1
• If exception is FIQ then bit 6 of CPSR is set to 1
• PC starts executing the exception from the relevant vector address
• Vector address contains a branch to the routine
• Registers in privileged modes are used to hold the return address
• Stack pointer is used to save user register contents and can be used by the exception handler
105
Exception return:
• Once the exception has been handled the user task is normally resumed.
• This requires the handler code to restore the user state exactly as it was when the exception
first arose
• Any modified user registers must be restored from the handler's stack.
• The CPSR must be restored from the appropriate SPSR.
• The PC must be changed back to the relevant instruction address in the user instruction
stream.
• The last two steps cannot be carried out independently
• For ex:
• If CPSR is restored first, R14 holding the return address is no longer accessible
• If PC is restored first exception handler loses control of instruction stream and CPSR cannot
be restored first
106
Exception return:
• ARM provides two mechanism to make both the steps
1. Used when return address is stored in R14
2. Used when return address is stored on to a stack
107
Exception return:
• IRQ and FIQ must return one instruction early in order to execute the instruction that was
'usurped' for the exception entry
• Pre fetch abort must return one instruction early to execute the instruction that had caused a
memory fault when first requested
• Data abort must return two instructions early to retry the data transfer instruction that was the
instruction before the one 'usurped' for the exception entry
108
Exception priorities:
1. Reset (highest priority)
2. Data abort
3. FIQ
4. IRQ
5. Pre fetch abort
6. SWI, undefined instruction (including absent coprocessor)
• If FIQ, IRQ and other exception other than reset occurs simultaneously, FIQ will get chance
due to highest priority
• But if third one is data abort then data abort will get chance and then FIQ will get chance
109
Status register to general register transfer instructions:
• To save or modify the contents of the CPSR or the SPSR of the current mode, those
contents must first be transferred into a general register, the selected bits modified and then
the value returned to the status register.
• MRS{<cond>} Rd, CPSR
• MRS r0, CPSR ; move the CPSR to r0
• MRS r3, SPSR ; move the SPSR to r3
• SPSR should not be used in user or system mode
General register to status register transfer instructions:
• To save or modify the contents of the CPSR or the SPSR of the current mode, those contents
must first be transferred into a general register, the selected bits modified and then the value
returned to the status register.
• MSR{<cond>} CPSR_f or SPSR_f, #<32-bit immediate>
To switch from supervisor mode into IRQ mode (for instance, to initialize the IRQ stack
pointer at start up):
• MRS r0, CPSR ; move the CPSR to r0
• BIC r0, r0, #0xlf ; clear the bottom 5 bits
• ORR r0, r0, #0x12 ; set the bits to IRQ mode
• MSR CPSR_f, r0 ; move back to CPSR
Note:
• Attempt to modify CPSR [23:0] in user mode have no effect
• Attempt to access SPSR [23:0] in user mode or system mode should be avoided
Memory faults:
Page absent:
• Addressed memory location has been paged out to disk
• Infrequently used pages are held on disk, attempt to access instruction or data from such a
page will fail
• System software identifies the cause and fetch the page from disk to memory
• But fetching a page from disk is a slow process
Page protected:
• Memory location is temporarily inaccessible
• When a page is loaded into memory, OS may make it initially read only
• When an attempt is made to write results in fault
Soft memory errors:
• Due to alpha particles radiation change, changing the state of a dynamic RAM storage cell
• Hardware error detector detects and forwards to software error corrector which corrects the
error
Memory faults:
Pre fetch abort:
• During instruction fetch
• Internally ARM puts the instruction to the pipeline along with abort flag
• Once the instruction enters the decode stage, decoder on seeing the flag generates the pre
fetch abort vector
Data abort:
• During accessing data from memory
Abort timing:
• When memory fault occurs, it need to preserve the state
ARM data aborts:
• PC is preserved and stored to R14_abt
115