Embedded Systems UEC513 D MST
Embedded Systems UEC513 D MST
(UEC513)
By:
Dr. Debabrata Ghosh
Assistant Professor, ECED
• Use this PPT sensibly. It is for reference only. By no means, it is comprehensive and self
sufficient for your exams. Use the course syllabus along with the text books for
comprehensive knowledge.
UEC513 : EMBEDDED SYSTEMS
L T P Cr
3 0 2 4.0
Course Objective:The objective of this course is to equip students with the necessary fundamental
knowledge and skills that enable them to design basic embedded systems. It covers architecture,
programming of ARM processor ad it’s interfacing with peripheral devices.
Syllabus
Introduction to Embedded Systems: Definition, Embedded Systems Vs General Computing
Systems, Classification of Embedded Systems, Major application areas. General purpose processor
architecture and organization, Von-Neumann and Harvard architectures, CISC and RISC architectures,
Big and Little endian processors, Processor design trade-offs, Processor cores: soft and hard.
Introduction to ARM Processor: The ARM design philosophy, ARM core data flow model,
Architecture, Register set, ARM7TDMI Interface signals, General Purpose Input Output Registers,
Memory Interface, Bus Cycle types, Pipeline, ARM processors family, Operational Modes, Instruction
Format, Data forwarding.
Programming based on ARM7TDMI: ARM Instruction set, condition codes, Addressing modes,
Interrupts, Exceptions and Vector Table. Assembly Language Programming, Thumb state, Thumb
Programmers model, Thumb Applications, ARM coprocessor interface and Instructions.
ARM Tools and Interfacing of Peripherals: ARM Development Environment, Arm Procedure Call
Standard (APCS), Example C/C++ programs, Embedded software development, Image structure,
linker inputs and outputs, Protocols (I2C, SPI), Memory Protection Unit (MPU). Physical Vs Virtual
Memory, Paging, Segmentation. The Advanced Microcontroller Bus Architecture (AMBA), DMA,
Peripherals, Interfacing of peripherals with ARM.
Course Learning Objectives (CLO)
The students will be able to:
1.Explain embedded system, its processor architecture and distinguish it from general
computing system.
2.Describe ARM processor internal architecture, assembly instructions, their format and
Develop ARM processor-based assembly language program for a given statement.
3.Describe how thumb mode operations are designed and various coprocessors are
interfaced in an embedded system.
4.Interface various hardware peripherals in embedded systems.
5.Recognize issues to be handled in any processor software tool chain for embedded
system development especially using C/C++.
Text Books
1.Carl Hamacher, ZvonkoVranesic, SafwatZaky, Naraig Manjikian, “COMPUTER
ORGANIZATION AND EMBEDDED SYSTEMS, Sixth Edition, McGraw Hill, 2012.
2.Steve Furber, “ARM System-on-Chip Architecture, Second Edition, PEARSON, 2013
Reference Books
1.Stephen Welsh, Peter Knaggs, “ARM: Assembly Language Programming”, Bourne
Mouth University Publication, 2003.
2.Andrew N. Sloss, Dominic Symes, Chris Wright “ARM System Developers Guide,
Designing and Optimizing System Software”, Elsevier Publication.
Role of Address, data and control busses
Address Bus:
• To recognized any I/O device or memory location by
processor.
• Assign address must be unique.
• Processor send the address on address lines and
decoding circuit will find respective device.
• More number of address bus means more number of
devices can be interfaced with CPU.
2m=n
m: number of address lines
n: number of memory locations to be addressed
▪ The address bus is unidirectional
Data Bus:
• Required data transfer or received through data bus.
It indicates the data handling capacity of CPU.
• More data buses mean a more expensive CPU and
computer but with higher data handling capacity
• The data bus is bidirectional
Control bus:
• Use to decide the direction of data, either transfer or
received.
Block diagram of a general purpose microprocessor
Snipp
Embedded System Definitions
1. An embedded system is a system that has software embedded into
computer-hardware, which makes it dedicated for an application
(s) or specific part of an application or product or part of a larger
system.
2. An embedded system is one that has a dedicated purpose software
embedded in a computer hardware.
3. It is a dedicated computer based system for an application(s) or
product. It may be an independent system or a part of large system.
Its software usually embeds into a ROM (Read Only Memory) or
flash.
4. It is any device that includes a programmable computer but is not
itself intended to be a general purpose computer.
5. Embedded Systems are the electronic systems that contain a
microprocessor or a microcontroller, but we do not think of them as
computers– the computer is hidden or embedded in the system.
1/1/2024 14
Applications of Embedded System
• 1. Consumer Electronics: Camcorders, Cameras.
• 2. Household appliances: Washing machine, Refrigerator.
• 3. Automotive industry: Anti-lock breaking system(ABS), engine control.
• 4. Home automation & security systems: Air conditioners, sprinklers, fire
alarms.
• 5. Telecom: Cellular phones, telephone switches
• 6. Computer peripherals: Printers, scanners.
• 7. Computer networking systems: Network routers and switches.
• 8. Healthcare: EEG, ECG machines.
• 9. Banking & Retail: Automatic teller machines, point of sales.
• 10. Card Readers: Barcode, smart card readers.
1/1/2024 15
Embedded systems vs general
computing system
1. Computing revolution began with general-purpose computing,
not sufficient for embedded computing (requires meeting
computational deadline, meeting power efficiency, meeting limited
memory availability)
2. Personal computer: built around general-purpose processor,
supports multiple peripherals, runs many applications
3. DVD player: embedded system specifically for decoding digital
video and generating video output. Not possible to: change OS of it,
not possible to change embedded software to make it work as a TV,
not possible to install printer software in it
4. Demarcation shrinking in certain embedded applications:
smartphone. Smartphones are not meant for any specific application
or dedicated purpose (so, it is not an embedded system). OS of
smartphone is unalterable by the end user (so it is an embedded
system)
Classification of Embedded systems
Based on generation:
Order in which embedded systems evolved
• First generation: built around 8-bit microprocessors and 4-bit
microcontrollers, simple hardware circuits, assembly firmware. Ex:
telephone keyboard
• Second generation: built around 16-bit microprocessors and 8 or
16-bit microcontrollers, complex and powerful instruction set,
embedded OS. Ex: SCADA
• Third generation: built around 32-bit processors and 16-bit
microcontrollers, application and domain specific processors,
complex and powerful instruction set, instruction pipeline, embedded
OS
• Fourth generation: SoC, reconfigurable processors, high
performance, tight integration, miniaturization, real time embedded
OS. Ex: smart phone
Classification of Embedded systems
Based on complexity & performance:
• Small scale: built around low performance and low cost 8
or 16-bit microprocessors/microcontrollers, simple
application, not time critical, may or may not contain OS,
Ex. electronic toy
• Medium scale: built around medium performance, low
cost 16 or 32-bit microprocessors/microcontrollers,
complex hardware and firmware, usually contain
embedded OS, Ex. Automated Teller Machine (ATM)
• Large scale: highly complex hardware and firmware, high
performance, 32 or 64-bit RISC processors, PLDs, may
contain coprocessors, contains RTOS, Ex. Autonomous
Vehicle Control System
Classification of Embedded systems
Based on functional requirements:
• Real-time embedded system: Strictly time specific,
defence, medical, healthcare. Ex: traffic control
Soft real-time: deadline is not strictly followed, ex: weather
monitoring system, TV remote, washing machine
Hard real-time: deadline is strictly followed, ex: air traffic control
systems, weapon defence systems, alarm for gas leakage,
pacemaker
• Stand alone embedded systems: independent systems.
Ex: MP3 player, microwave oven
• Networked embedded system: connected to a network.
Ex: Home security, ATM
• Mobile embedded system: small, easy to use, portable.
Ex: MP3 player, mobile phone, digital camera
Classification of Embedded systems
Based on triggering:
• Embedded systems which are Reactive in nature can be
based on triggering.
Event triggered: Event-Triggered systems respond to
external stimuli or events. They execute tasks when specific
events occur. Ex: automotive airbag system, activated by
crash sensor's signal.
Time triggered: Time-Triggered systems operate
based on predefined schedules or time intervals. Tasks are
scheduled and executed at specific times, ensuring
deterministic behavior. Automated irrigation systems,
watering plants at specific intervals.
Constraints of an Embedded system
⮚ Design
⮚ Available system-memory
⮚ Available processor speed
⮚ Limited power dissipation when running the system
continuously in cycles of the system start, wait for event,
wake-up and run, sleep and stop
⮚ Performance
⮚ Size
⮚ Non-recurring design cost, and manufacturing costs
1/1/2024 21
Classifications of computer
architecture
On the basis of hardware:
⮚ Von Neumann Architecture
⮚ Harvard Architecture
⮚ The data and program are stored in the same ⮚ The data and program memories are separate
Memory.
⮚ Share single common bus for data as well ⮚ Has separate buses for data and Instructions
as instruction fetching. fetching.
⮚ Pipelining complex. Low performance as ⮚ Easier to pipeline, so high performance
compared to Harvard
⮚ Slow ⮚ Faster
⮚ Cheaper ⮚ More costly
⮚ Efficient utilization of memory space ⮚ Wastage of memory
⮚ The data and instructions can not be fetched ⮚ The data and instructions can be fetched
at the same time. at the same time.
Processor architecture: CISC vs RISC
Multiply two numbers in memory: source code a = a*b (where a, b are memory locations,
a= 2:3, b=5:2) LOAD A, 2:3
LOAD B, 5:2
MULT 2:3, 5:2
PROD A, B
STORE 2:3, A
1) CISC instructions operate on memory and 1) RISC instructions operate mainly on registers
registers 2) Generally single cycle instructions, fixed
2) Generally multiple cycle instructions, length instructions
variable length instructions 3) Few addressing modes
3) More addressing modes 4) Lots of registers
4) Few Registers 5) Instruction set reduced and simple
5) Instruction set large and complex 6) Decoding simple
6) Decoding Complex 7) Pipeline easier and excellent
7) Pipeline harder and poor 8) Large program for a specific task
8) Small program for a specific task 9) More emphasis on software
9) More emphasis on hardware 10) Use of Hardwired Control Unit
10) Use of Microprogrammed Control Unit 11) E.g. ARM, DLX,PIC
11) E.g. Intel, AMD X86
Processor cores: soft and hard
❖ Design approaches for implementing processor cores in IC
❖ Hard core: not reconfigurable, components are fixed, integrated into chip
during manufacturing
❖ Soft core: implemented using configurable hardware through the use of
programmable logic device, such as FPGA
Design flexibility
❖ How easily development platform can be expanded: 1) adding IP
(Intellectual Property) blocks or 2) use parallel, serial interfaces
❖ Hard core solution: use of standard interfaces
❖ Soft core solution: use of digital IP cores
Processor cores: soft and hard
Soft core and hard core tradeoff:
Cost
❖ Hard core solution: cost effective
❖ Soft core solution: expensive system
Difference in price between microcontroller and FPGA
❖ Low price of microcontroller: Advanced technology and high volume
production
❖ High price of FPGA: not so advanced technology and less volume
production
Power consumption
Design system for high energy efficiency
❖ Hard core solution: power saving modes, very little power consumption
❖ Soft core solution: FPGA less power efficient
Endianness
Introduction to ARM
⮚ Key Component in Embedded Systems.
⮚ Mostly Single Cycle Execution except variable cycle execution for certain instructions.
⮚ Uniform and Fixed op code width of 32 bits to ease decoding and pipelining.
⮚ These registers are truly general purpose, with no special uses being placed
on them by the processors’ architecture.
⮚ A few registers (R8–R12) are common to all processor mode with the
exception of the fiq mode.
⮚ When the processor is in the fast interrupt mode these registers are replaced
with the different set of registers (R8_fiq – R12_fiq)
Registers of ARM
⮚ The general purpose register can be used to handle 8-bit bytes,
16-bit half words, or 32-bit words.
⮚ The “user” (usr) and “System” (sys) modes share the same
registers.
⮚ Each exception mode has its own version of R13, which points to a stack
dedicated to that exception mode.
⮚ When an execution occurs, the exception mode’s version of R14 is set to the
address after the instruction which has just been completed.
⮚ The SPSR is a copy of the CPSR just before the exception occurred.
The Program Counter, PC or r15
⮚ Register r15 holds the Program Counter known as the PC.
⮚ The exception modes also have a saved program status register (SPSR),
that is used to preserve the value of CPSR when the associated exception
occurs.
⮚ Because the User and System modes are not exception modes, there is no
SPSR available.
Bit pattern Current Program Status
Register (CPSR)
Current Program Status Register (CPSR)
⮚ Any bit not currently used is reserved for future use and should be
zero.
⮚ The I and F bits indicate if interrupts (I) or Fast Interrupts (F) are
allowed.
Modes of ARM Processor
ARM has 7 modes
⮚ One non-privileged mode (allows read access to control field and read write to
conditional flags)
⮚ User mode (Normal programs and applications)
Modes of ARM Processor
SVR
IRQ SWI/RESET
ABT
USER
Sys Call UND
FIQ
SYS
Internal Architecture of ARM
⮚ All exception modes have their own Stack Pointer (SP) and Link (LR) registers.
⮚ The fast interrupt mode has more registers (r8_fiq – r12_fiq) for fast interrupt
processing.
Exceptions
The seven exceptions are
⮚ 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.
https://en.wikipedia.org/wiki/List_of_ARM_microarchitectures
Instruction Set
The ARM Instruction set can be divided into six broad classes of
instruction
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
Elements inside curly brackets are optional.
Usage: Adds the value in Rn to Operand2 and places the sum in Rd.
Condition flags: If S is specified then all flags are updated according to the result.
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
Elements inside curly brackets are optional.
Usage: Adds the value in Rn to Operand2 and adds another 1 if the carry flag is
set (C = 1). The sum is placed in Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
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
Elements inside curly brackets are optional.
Usage: Subtracts Operand2 from the value in Rn and places the difference in
Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
Examples:
SUB R7, R4, #99 ;subtracts 99 from the value in R4 and places the result in R7
SUB R1, R2, R3 ;subtracts the value in R3 from the value in R2 and places the
difference in R1.
SUB Rd, Rn, Rm
Rm = 0x00110011
Rn = 0xFFFFFFFF
Subtract with carry
C=0
SUB vs SBC
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
Elements inside curly brackets are optional.
Usage: Subtracts the value in Rn from Operand2 and subtracts another 1 if the
carry flag is clear. Places the difference in Rd.
Condition flags: If S is specified then all flags are updated according to the
result.
Examples:
ADD
ADC
SUB
SBC
RSB
RSC
Multiply
Syntax: MUL{cond}{S} Rd, Rm, Rs
Elements inside curly brackets are optional.
Usage: Multiplies the values in registers Rm and Rs and places the least
significant 32 bits of the product in register Rd.
Condition flags: If S is specified then the N and Z flags are updated according to
the result, the V flag is not affected (because only lower 32 bit result is taken,
irrespective of operands are signed or unsigned) and the C flag is unpredictable
for the ARM7 and earlier processors.
Example:
MUL R5, R3, R9 ;multiply the values in R3 and R9 and places the result in R5
Multiply
Multiply and accumulate
Syntax: MLA{cond}{S} Rd, Rm, Rs, Rn
Usage: Adds the value in Rn to the product of the values in Rm and Rs and places the least
significant 32 bits of the result in register Rd.
Condition flags: If S is specified then the N and Z flags are updated according to the result,
the V flag is not affected (because only lower 32 bit result is taken, irrespective of operands
are signed or unsigned) and the C flag is unpredictable for the ARM7 and earlier processors.
Example:
MLA R5, R3, R9, R5 ;multiply the values in R3 and R9, add the product to the value in R5 and
places the result in R5
Multiply - unsigned long
Syntax: UMULL{cond}{S} RdLo, RdHi, Rm, Rs
Elements inside curly brackets are optional.
Usage: Multiplies the values (as unsigned integers) in registers Rm and
Rs and places the least significant 32 bits of the product in register RdLo
and the most significant 32 bits of the product in register RdHi.
Condition flags: If S is specified then the N and Z flags are updated
according to the result and the V and C flags are unpredictable for the
ARM7 and earlier processors.
Example:
UMULL R6, R5, R3, R9 ;multiply the values in R3 and R9 and places the
result in R5 and R6
Multiply - unsigned long
Multiply and accumulate - unsigned long
Syntax: UMLAL{cond}{S} RdLo, RdHi, Rm, Rs
Elements inside curly brackets are optional.
Usage: Multiplies the values (as unsigned integers) in registers Rm and Rs and
adds the 64 bit product to the unsigned 64 bit value in registers RdLo (least
significant 32 bits) and RdHi (most significant 32 bits).
Condition flags: If S is specified then the N and Z flags are updated according to
the result and the V and C flags are unpredictable for the ARM7 and earlier
processors.
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 and accumulate - unsigned long
Multiply - signed long
Syntax: SMULL{cond}{S} RdLo, RdHi, Rm, Rs
Elements inside curly brackets are optional.
Usage: Multiplies the values (as two's complement signed integers) in registers
Rm and Rs and places the least significant 32 bits of the product in register
RdLo and the most significant 32 bits of the product in register RdHi.
Condition flags: If S is specified then the N and Z flags are updated according to
the result and the V and C flags are unpredictable for the ARM7 and earlier
processors.
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
Elements inside curly brackets are optional.
Usage: Multiplies the values (as two's complement signed integers) in registers
Rm and Rs and adds the 64 bit product to the two's complement signed 64 bit
value in registers RdLo (least significant 32 bits) and RdHi (most significant 32
bits).
Condition flags: If S is specified then the N and Z flags are updated according to
the result and the V and C flags are unpredictable for the ARM7 and earlier
processors.
Example:
SMLAL R6, R5, R3, R9 ;multiply the values in R3 and R9 and add the product to
the values in R5 and R6
Summary
• MUL
• MLA
• UMULL
• UMLAL
• SMULL
• SMLAL
Compare
Usage: Subtracts Operand2 from the value in Rn and updates the flags
accordingly. The result is discarded.
Condition flags: All flags are updated according to the result.
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 Rn, Operand2
Usage: Add Operand2 to the value in Rn and updates the flags accordingly.
The result is discarded.
Condition flags: All flags are updated according to the result.
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:
Usage: Reloads the program counter with the memory address given by
label. The label identifies the instruction to branch to in the assembly
language program.
Examples:
For Signed Number
BNE loop
For Unsigned Number
B display
Branch with link
Syntax: BL{cond} label
Usage: Reloads the program counter with the memory address given
by label. The label identifies the instruction to branch to in the
assembly language program. The memory address of the next
instruction after the BL instruction is copied to register r14, the link
register.
Examples:
BLCS LABEL
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
Usage: Loads register Rd with 4 bytes from a location in memory with address
determined by address mode. Ensure that the address is divisible to 4.
Examples:
LDR r7, [r3] ;load r7 with the value in memory location with address given by r3
LDR r1, [r2], #4 ;load r1 with the value in memory location with address given by r2 then add 4 to
r2. (Post Index Register Relative)
LDR r1, [r2,#4] ; load r1 with the value in memory location with address given by r2+4 to r2.(Normal
Register Relative)
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
Usage: Loads the least significant byte of register Rd with 1 byte from a
memory location with address determined by address mode. The most
significant bit of the loaded byte (the sign bit) is extended across the
top 24 bits of Rd.
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
Elements inside curly brackets are optional.
Usage: Loads the bottom 16 bits of register Rd with 2 bytes from a
location in memory with address determined by address mode. The top
16 bits of Rd are cleared.
Condition flags: Flags are not affected.
Example:
LDRH r7, [r3] ;load r7 with two bytes in memory location with address
given by r3 and clear the top 16 bits of r7
Load signed half word
Syntax: LDRSH{cond} Rd, address mode
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.
1/1/2024 LEC/UEC-405/July-Dec 2017 119
1/1/2024 LEC/UEC-405/July-Dec 2017 120
1/1/2024 LEC/UEC-405/July-Dec 2017 121
1/1/2024 LEC/UEC-405/July-Dec 2017 122
Store word
Syntax: STR{cond} Rd, address mode
Elements inside curly brackets are optional.
Usage: Stores the value in register Rd into 4 bytes of memory with
address determined by address mode. Ensure that the address is
divisible to 4.
Example:
Usage: Loads the registers specified in reglist with values from the
stack. One of four modes must be specified; FD, FA, ED or EA.
Example:
LDMFD sp!, {r3-r5, pc} ;load registers r3, r4, r5 and program
counter with values from the stack. The stack is 'full descending'.
Push
POP
Store Multiple Instruction
Store Multiple Instruction
Store Multiple Instruction
Store Multiple Instruction
Stack Instruction
Programming Examples
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
LDR R1, value 1
LDR R2, value 2
LDR R3, value 3
LDR R4, value 4
ADDS R5, R1, R3
ADC R6, R2, R4
SWI &11
To find number of positive and negative numbers out of 10 32-bit numbers
AREA PROGRAM, CODE, READONLY AREA PROGRAM, CODE, READONLY AREA PROGRAM, CODE, READONLY
ENTRY ENTRY ENTRY
MAIN MAIN MAIN
MOV R9, #10 MOV R9, #5 MOV R9, #5
LDR R0,VALUE LDR R0,VALUE LDR R0,VALUE
LABEL LABEL LABEL
LDR R1,[R0],#4 LDR R1,[R0],#4 LDR R1,[R0],#4
LDR R2,=&80000000 SUBS R2,R1,#00 OR MOVS R2,R1 SUBS R2,R1,#00
ANDS R3,R1,R2 ADDPL R4,R4,#1 ADDGT R4,R4,#1
ADDEQ R4,R4,#1 ADDMI R5,R5,#1 ADDLT R5,R5,#1
ADDNE R5,R5,#1 SUBS R9,R9,#1 SUBS R9,R9,#1
SUBS R9,R9,#1 BNE LABEL BNE LABEL
BNE LABEL AREA PROGRAM, DATA, READONLY AREA PROGRAM, DATA, READONLY
AREA PROGRAM, DATA, READONLY VALUE DCD &00001000 VALUE DCD &00001000
VALUE DCD &00001000 END END
END
AREA PROGRAM, CODE, READONLY AREA PROGRAM, CODE, READONLY AREA PROGRAM, CODE, READONLY
ENTRY ENTRY ENTRY
MAIN MAIN MAIN
MOV R9, #5 MOV R9, #5 MOV R9, #5
LDR R0,VALUE LDR R0,VALUE LDR R0,VALUE
LABEL LABEL LABEL
LDR R1,[R0],#4 LDR R1,[R0],#4 LDR R1,[R0],#4
CMP R1,#00 CMP R1,#00 LSLS R2,R1,#1
ADDPL R4,R4,#1 ADDGT R4,R4,#1 ADDCC R4,R4,#1
ADDMI R5,R5,#1 ADDLT R5,R5,#1 ADDCS R5,R5,#1
SUBS R9,R9,#1 SUBS R9,R9,#1 SUBS R9,R9,#1
BNE LABEL BNE LABEL BNE LABEL
AREA PROGRAM, DATA, READONLY AREA PROGRAM, DATA, READONLY AREA PROGRAM, DATA, READONLY
VALUE DCD &00001000 VALUE DCD &00001000 VALUE DCD &00001000
END END END
Example: Block Copy
– Copy a block of memory, which is an exact
multiple of 12 words long from the location
pointed to by r12 to the location pointed to by
r13. r14 points to the end of block to be copied.
r13
; r12 points to the start of the source data
; r14 points to the end of the source data r14 IncreasingM
; r13 points to the start of the destination data emory
loop LDMIA r12!, {r0-r11} ; load 48 bytes
STMIA r13!, {r0-r11} ; and store them
CMP r12, r14 ; check for the end r12
BNE loop ; and loop until done
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
Elements inside curly brackets are optional.
Usage: Performs a bit by bit logical AND of Operand2 with the value in
Rn and places the result in Rd.
Condition flags: If S is specified then the N and Z flags are updated
according to the result, the C flag is updated if Operand2 was calculated
using a shift and the V flag is not affected.
Examples:
AND r7, r4, #0xFF ;ANDs 0x000000FF with r4 and places the result in r7
AND r1, r2, r3 ;ANDs r3 with r2 and places the result in r1
Exclusive OR
Syntax: EOR{cond}{S} Rd, Rn, Operand2
Elements inside curly brackets are optional.
Usage: Performs a bit by bit exclusive OR of Operand2 with the value in Rn and
places the result in Rd.
Condition flags: If S is specified then the N and Z flags are updated according to
the result, the C flag is updated if Operand2 was calculated using a shift and the
V flag is not affected.
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
Elements inside curly brackets are optional.
Usage: Performs a bit by bit logical OR of Operand2 with the value in Rn and
places the result in Rd.
Condition flags: If S is specified then the N and Z flags are updated according to
the result, the C flag is updated if Operand2 was calculated using a shift and the
V flag is not affected.
Examples:
ORR r7, r4, #0xFF ;ORs 0x000000FF with r4 and places the result in r7
ORR r1, r2, r3 ;ORs r3 with r2 and places the result in r1
Bit clear
Syntax: BIC{cond}{S} Rd, Rn, Operand2
Elements inside curly brackets are optional.
Usage: Performs a bit by bit logical AND of Operand2 with the value in
Rn and updates the flags accordingly. The result is discarded.
Condition flags: The N and Z flags are updated according to the result,
the C flag is updated if Operand2 was calculated using a shift/rotate and
the V flag is not affected.
Same as ANDS operation (but, result is discarded)
All of the Shift and Rotate instructions affect Carry Flag. In case of
left/right shift, highest/ lowest bit is copied to the carry flag
Examples:
TST r4, #0xFF ;AND 0x000000FF with r4 and sets the flags accordingly
TST r2, r3 ;AND r3 with r2 and sets the flags accordingly
Test
Test equivalence
Syntax: TEQ{cond} Rn, Operand2
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]
Elements inside curly brackets are optional.
Usage: Byte from memory address given by value in Rn is loaded into least
significant byte of Rd and the top 24 bits of Rd are cleared. Least significant
byte in register Rm is stored at memory location with address given by value
in Rn.
Condition flags: Flags are not affected.
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
Syntax: MRS{cond} Rd, psr
Elements inside curly brackets are optional.
Usage: Moves the contents of the current program status register (CPSR) or
the saved program status register (SPSR) into register Rd.
Condition flags: Flags are not affected.
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 Rm 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 by an immediate value formed from 8
bits rotated by an even number of bits.
Examples:
MSR SPSR_c, #0x7A ;move the immediate value, #0x7A, into the control field of the
saved program status register
Format of CPSR or SPSR
Examples
.
The value in the register is first shifted by a numeric constant before being
applied to the remainder of the instruction.
Example:
Execute instruction MOV r3, r2, LSL #2
Before r2 holds the value:
1000 1101 1000 1001 0011 0100 0010 0001
After r3 holds the value:
0011 0110 0010 0100 1101 0000 1000 0100
Logical shift right (LSR)
The value in the register is shifted to the right by a specified
number, n, of bits and the left hand n bits are set to 0.
Example:
Execute instruction MOV r3, r2, LSR #2
The value in the register is shifted to the right by a specified number, n, of bits and the left hand n bits are
set to the value of the most significant bit (sign bit) before the shift.
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
1. IMMEDIATE
MOV R0, #25H
ADD R0, R1, #25H
2. REGISTER
MOV R0, R1
ADD R0, R1, R2
3. DIRECT
LDR R0, VALUE
STR R0, VALUE
Addressing modes
4. INDIRECT
LDR R0, [R1]
STR R0, [R1]
5. REGISTER RELATIVE
NORMAL: LDR R0, [R1, #04H]
PRE INDEX: LDR R0, [R1, #04H]!
POST INDEX: LDR R0, [R1], #04H
6. BASE INDEXED
R0 and R1 ?
Example
Pre-indexing with write back
Zero offset LDR r7, [r3] ;load r7 with the value in memory location with address given by r3
Pre-indexed offset
Post-indexed offset
Pre-index Examples:
An immediate value in the range -4095 to +4095
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
STR r5, [r2, -r7] ;store value in r5 at memory location with address given
by value in r7 subtracted from the value in r2
STR r5, [r2, r7, LSL #3] ;store value in r5 at memory location with address
given by value in r7 left shifted by three bits added to the value in r2
Post-index Examples:
An immediate value in the range -4095 to +4095
LDR r7, [r3], #4 ;load r7 with the value in memory location with address
given by r3 and then add 4 to the value in r3
STR r5, [r2,] -r7 ;store value in r5 at memory location with address given
by value in r2 and then subtract the value in r7 from the value in r2
STR r5, [r2], r7, LSL #3 ;store value in r5 at memory location with
address given by value in r2 and then add the value in r7 left shifted by
three bits to the value in r2
Second operand
Operand2 occurs in the syntax of a number of ARM instructions.
▪ A register
Examples:
ADD r2, r4, r9
MOV r6, r0
SUB r7, r3, r14
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.
Conditional Execution
Branch instructions
• All ARM processors support a branch instruction that
allows a conditional branch forwards or backwards up to
32MB.
• The other is called a shifter operand and is either an immediate value or a register.
• If the second operand is a register value, it can have a shift applied to it.
• CMP, CMN, TST and TEQ always update the condition code flags.
• The assembler automatically sets the S bit in the instruction for them.
Usage: Subtracts Operand2 from the value in Rn and updates the flags
accordingly. The result is discarded.
Condition flags: All flags are updated according to the result.
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 Rn, Operand2
Usage: Add Operand2 to the value in Rn and updates the flags accordingly.
The result is discarded.
Condition flags: All flags are updated according to the result.
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
Test
Syntax: TST{cond} Rn, Operand2
Elements inside curly brackets are optional.
Usage: Performs a bit by bit logical AND of Operand2 with the value in
Rn and updates the flags accordingly. The result is discarded.
Condition flags: The N and Z flags are updated according to the result,
the C flag is updated if Operand2 was calculated using a shift/rotate and
the V flag is not affected.
Same as ANDS operation (but, result is discarded)
All of the Shift and Rotate instructions affect Carry Flag. In case of
left/right shift, highest/ lowest bit is copied to the carry flag
Examples:
TST r4, #0xFF ;AND 0x000000FF with r4 and sets the flags accordingly
TST r2, r3 ;AND r3 with r2 and sets the flags accordingly
Test equivalence
Syntax: TEQ{cond} Rn, Operand2
src DCD 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4
dstDCD 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0,0 ,0 ,0 ,0
Programming Examples
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 for multiplication
of numbers by repetitive addition.
Alternate program
MOV R1,#0x03
MOV R2, #32
MOV R3, #0x00
MOV R4, #0x00
NEXT MOVS R1, R1, RRX
ADDCC R3,R3,#0x01
ADDCS R4, R4, #0x01
SUB R2, R2, #0x01
BNE NEXT
Write a program in ARM assembly language to perform
multiplication of numbers by repetitive addition.
Cond
E FF
4 FF
2 FF
Data processing instructions
31 28 27 25 24 23 0
Cond 1 0 1 L Offset
31 28 27 25 24 23 0
Cond 1 0 1 L Offset
31 28 27 25 24 23 0
Cond 1 0 1 L Offset
During decoding: Processor takes the 24-bit signed offset value, left shift it
by 2 bits, sign ext it to 32 bits, and adds this to PC + 8
Branch instructions
31 28 27 25 24 23 0
Cond 1 0 1 L Offset
During encoding, the 24 bit offset is right shifted 2 bits and stored into the instruction encoding.
Multiply
• MUL{cond}{S} Rd, Rm, Rs
• MLA{cond}{S} Rd, Rm, Rs, Rn
Load/Store
• LDR|STR {<cond>}{B} Rd,addressing1
LDR{<cond>}SB/H/SH Rd, addressing2
LDR R0, [R1]
LDR R0, [R1, R2, LSL #2]
Load/Store
Load/Store Multiple registers
• Syntax: <LDM|STM>{<cond>}<addressing mode> Rn{!},<registers>{ˆ}
• LDMIA r0!, (r1-r3)
• STMIB r0!, (r1-r3)
Load/Store Multiple registers