You are on page 1of 60

Microcontroller and Embedded Systems

(22AML135)

Module-3:ARM Cortex M3 Instruction Set and


Programming

Dr. MANJUNATH G. ASUTI


B.E, M-Tech, (Ph.D)
Associate Professor
Dept. of ECE
BNM Institute of Technology, Bengaluru
Syllabus

Assembly Basics.

Instruction list and description.

Special Instructions.

Useful Instructions.

Assembly and C language Programming.


Assembly Basics

An assembler is a type of computer program that takes in basic instructions


and converts them into a pattern of bits that the computer's processor can
use to perform basic operations.
Assembly Basics

Label Operation Operands Field Comment


Field (Opcode) Field Field

Start: MOVS R0, #0X52 ;Set R0=0X52

CMP R0, R1 ;Compare R0


and R1
Assembly Basics
Label Field The label is optional. Some of the instructions might have a label in
front of them so that the address of the instructions can be
determined using the label.
Operation Opcode is an operation code which decides the operation to be
(Opcode) Field performed by processor.

Operands Field The opcode is followed by a number of operands. The operands field
contains the data or an address for its corresponding instruction to
be operated or performed.
Comment Field The text after each semicolon (;) is a comment. These comments do
not affect the program operation, but they can make programs easier to
understand.
Assembler Directives
Assembler directives are instructions that direct the assembler to do something.

Example: EQU( Equate) - Constants can be defined using EQU and then it can
be
used in program code.

Ex: NVIC_IRQ_SET EQU 0xE000E100


NVIC_IRQ_ENABLE EQU 0x1
Assembler Directives
DCI (Define Constant Instruction) - is used to code an instruction if the
assembler cannot generate the exact instruction that the user wants and if the
user know the binary code for instruction.

Ex: DCI 0xBE00 ; Breakpoint (BKPT 0) – 16 bit instruction.


Assembler Directives
DCB (Define Constant Byte) – Byte size constant values such as
characters can be used to define using DCB.

DCD (Define Constant Data) – Word size constant values to define binary
data in assembler code is done through DCD.
Assembler Directives
Instruction List and Description
Instruction List and Description
1.Moving data between register and register
The command to move data between registers is MOV (move).
Example:
MOV R8, R3; Moving data from register R3 to register R8.

Another instruction can generate the negative value of the original data is MVN
(move negative).
Example:
MVN R8, R3; The MVN instruction takes the value of R3, performs a bitwise
logical NOT operation on the value, and places the result into R8.
Instruction List and Description
Instruction List and Description
Instruction List and Description
2.Moving data between memory and register
i. LDR and STR, LDM and STM
ii. PUSH and POP
LDR and STR:
The basic instructions for accessing memory are Load and Store.
Load (LDR) transfers data from memory to registers,
Store (STR) transfers data from registers to memory.
The transfers can be in different data sizes (byte, half word, word, and double
word).
Instruction List and Description
Instruction List and Description
Instruction List and Description
Instruction List and Description
Instruction List and Description
LDM and STM
Multiple Load and Store operations can be combined into single instructions
called:

LDM (Load Multiple) and STM (Store Multiple).

The exclamation mark (!) in the instruction specifies whether the register Rd
should be updated after the instruction is completed
Instruction List and Description LDMIA R0!, {R2 - R5}
STMIA R1!, { R2 - R5}
Instruction List and Description
Preindexing and Postindexing
In Preindexing, the register holding the memory address is adjusted. The
memory transfer then takes place with the updated address.

In Postindexing, the memory access instructions carry out the memory transfer
using the base address specified by the register and then update the address
register afterward.

Preindexing

Postindexing
Instruction List and Description
Instruction List and Description
Instruction List and Description
Instruction List and Description
Instruction List and Description
Instruction List and Description
PUSH and POP Instructions
Syntax
PUSH<reg> ; Decrement and store
POP <reg> ; Get and increment

Multiple register PUSH and POP operation


PUSH {R0, R4-R7, R9} ;Push R0, R4, R5, R6, R7, R9 into stack
memory

POP {R2,R3} ;Pop R2 and R3 from stack Usually a PUSH instruction


Instruction List and Description
3.Moving data between Special register and register

MSR and MRS Instructions:


MSR and MRS instructions are used to access the special registers in Cortex
M3
Syntax: MRS <general purpose reg>,<special register>
MSR < special register >,< general purpose reg >

Note: Except accessing the APSR, the use MSR or MRS to access other
special registers only in privileged mode
Special Registers:
Program Status Registers (PSRs).

Application Program Status Register (APSR):Read/Write


MRS R0, APSR; Read Flag state into R0
MSR APSR, R0; Write Flag state from R0

Interrupt Program Status Register (IPSR):Read Only


MRS R0, IPSR; Read Exception/Interrupt state into R0

Execution Program Status Register (EPSR):Read Only


MRS R0, EPSR; Read Execution state into R0
Instruction List and Description
3.Moving data between Special register and register
Instruction List and Description
4.Moving an immediate data value into a register
• Moving immediate data into a register is a common thing to do.
• For example, to access a peripheral register, we need to put the address
value into a register beforehand.
• For small values (8 bits or less), MOVS (move with status update) can be
used.

Ex-1: MOV R0, #0xFFH ; Set R0 = 0xFF (hexadecimal)


MOV R1, #'S' ; Set R1 = ASCII character S

Ex-2: MOVS R0, # 0x12H ; Set R0 to 0x12.


Instruction List and Description
4.Moving an immediate data value into a register

MOVT (Move Top)


Syntax: MOVT{cond} Rd, #imm16
Where cond is an optional condition code. Rd is the destination register.
imm16 is a 16- bit immediate constant.

Operation: MOVT writes a 16-bit immediate value imm16 to the top


halfword, Rd[31:16] of its destination register. The write does not affect lower
half word Rd[15:0].
Arithmetic Instructions
Arithmetic Instructions
Arithmetic Instructions
Arithmetic Instructions
Logical Instructions
Logical Instructions
Shift and Rotate Instructions

• LSL: Logical Shift Left

• LSR: Logical Shift Right

• ASR: Arithmetic Shift Right

• ROR: Rotate Right

• RRX: Rotate Right extended by 1 bit


Shift and Rotate Instructions
LSL: Logical Shift Left
Shift and Rotate Instructions
LSL: Logical Shift Left
LSL Rd,Rn, #immd Rd=Rn<< immd

LSL Rd,Rn Rd=Rd<<Rn Logical Shift Left

LSL.W Rd, Rn,Rm Rd=Rn<<Rm


Shift and Rotate Instructions
LSL: Logical Shift Right
Shift and Rotate Instructions
LSL: Logical Shift Right
LSR Rd,Rn, #immd Rd=Rn>> immd

LSR Rd,Rn Rd=Rd>>Rn Logical Shift Right

LSR.W Rd, Rn,Rm Rd=Rn>>Rm


Shift and Rotate Instructions
Shift and Rotate Instructions
ASR: Arithmetic Shift Right
Shift and Rotate Instructions
ASR: Arithmetic Shift Right
ASR Rd,Rn, #immd Rd=Rn>> immd

ASR Rd,Rn Rd=Rd>>Rn Arithmetic Shift Right

ASR.W Rd, Rn,Rm Rd=Rn>>Rm


Shift and Rotate Instructions
ASR: Arithmetic Shift Right
Shift and Rotate Instructions
ROR: Rotate Right

ROR Rd,Rn Rd rot by Rn

ROR.W Rd, Rn,Rm Rd=Rn rot by Rm Rotate Right

ROR.W Rd, Rn,#immed Rd=Rn rot by immed


Shift and Rotate Instructions
ROR: Rotate Right
Shift and Rotate Instructions
RRX: Rotate Right extended

RRX.W Rd,Rn {C,Rd}={Rn,C) Rotate Right extended


Data Reverse order Instructions

REV Rd,Rn Rd =rev(Rn) Reverse bytes in word

REV16 Rd,Rn Rd=rev16(Rn) Reverse bytes in each word

REVSH Rd,Rn Rd=revsh(Rn) Reverse bytes in bottom half word


and sign extend the result
Data Reverse order Instructions
Data Reverse order Instructions
Bit Field Instructions
Bit Field and Manipulation Instructions
Bit Field and Manipulation Instructions
Bit Field and Manipulation Instructions
Bit Field and Manipulation Instructions
Bit Field and Manipulation Instructions
THANK YOU

You might also like