Module 2(Lecture 1)
Module 2(Lecture 1)
SYSTEMS (17EC62)
1
Assembly Basics: Assembler Language: Basic Syntax
In assembler code, the following instruction formatting is commonly used:
label opcode operand1, operand2, ...; Comments
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.
Then, the opcode (the instruction) followed by a number of operands.
Normally, the first operand is the destination of the operation. The
number of operands in an instruction depends on the type of instruction, and
For example, immediate data are usually in the form #number, as shown here:
MOV R0, #0x12 ; Set R0 = 0x12 (hexadecimal) 2
Define constants using EQU, and then use them inside your program code.
For example,
NVIC_IRQ_SETEN0 EQU 0xE000E100 NVIC_IRQ0_ENABLE EQU 0x1
Data definition directives are available for insertion of constants inside assembly code.
DCI (Define Constant Instruction) can be used to code an instruction if the assembler
cannot generate the exact instruction that you want and if you know the binary code for
the instruction. 3
DCB (Define Constant Byte) for byte size constant values, such as characters
Define Constant Data (DCD) for word size constant values to define binary data in
your code.
LDR R3,=MY_NUMBER ; Get the memory address value of MY_NUMBER
LDR R4,[R3] ; Get the value code 0x12345678 in R4LDR R0,=HELLO_TXT
; Get the starting memory address of HELLO_TXT
BL PrintText ; Call a function called PrintText to display string
3
Assembler Language: Use Of Suffixes
For the Cortex-M3, the conditional execution suffixes are usually used for
branch instructions.
In those cases, the S suffix and the conditional execution suffixes can be
Suffix Description
used at the same time.
Update Application Program Status register (APSR) (flags); for
S
example: ADDS R0, R1 ; this will update APSR
The traditional Thumb syntax can still be used. The choice between whether the
instructions are interpreted as traditional Thumb code or the new UAL syntax is
normally defined by the directive in the assembly file.
For example, with ARM assembler tool, a program code header with “CODE16”
5
directive implies the code is in the traditional Thumb syntax, and “THUMB”
Assembler Language: Unified Assembler Language
With reusing traditional Thumb is that some instructions change the flags in APSR,
even if the S suffix is not used. However, when the UAL syntax is used, whether the
With the new instructions in Thumb-2 technology, some of the operations can be
handled by either a Thumb instruction or a Thumb-2 instruction. For example, R0 =
R0 + 1 can be implemented as a 16-bit Thumb instruction or a 32-bit Thumb-2
instruction. With UAL, you can specify which instruction you want by adding
suffixes:
ADDS R0, #1 ; Use 16-bit Thumb instruction by default for smaller size
ADDS.N R0, #1 ; Use 16-bit Thumb instruction (N=Narrow)
Instruction Descriptions
Till now we have gone through different set of 16-bit, 32-bit instructions and
Here, we shall understand some of the commonly used syntax for ARM
assembly code.
For better understanding let me categorize these instructions into following 4
categories:
The command to move data between registers is MOV (move). For example,
moving data from register R3 to register R8 looks like this:
MOV R8, R3
Another instruction can generate the negative value of the original data; it is called
MVN (move negative).
1. Moving Data instructions
The basic instructions for accessing memory are Load and Store.
Load (LDR) transfers data from memory to registers, and Store transfers data from
registers to memory.
The transfers can be in different data sizes (byte, half word, word, and double
word), 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.
For example, if R8 equals 0x8000:
STMIA.W R8!, {R0-R3} ; R8 changed to 0x8010 after store ;
(increment by 4 words) STMIA.W R8 , {R0-R3} ; R8 unchanged
after store
Instruction List
16-Bit Load and Store Instructions
Instruction Function
LDR Load word from memory to register
LDRH Load half word from memory to register
LDRB Load byte from memory to register
Load half word from memory, sign extend it, and put it in
LDRSH
register
LDRSB Load byte from memory, sign extend it, and put it in register
STR Store word from register to memory
STRH Store half word from register to memory
STRB Store byte from register to memory
LDM/LDMIA Load multiple/Load multiple increment after
STM/STMIA Store multiple/Store multiple increment after
PUSH Push multiple registers
Example Description
LDRB Rd, [Rn, #offset] Read byte from memory location Rn + offset
LDRH Rd, [Rn, #offset] Read half word from memory location Rn + offset
LDR Rd, [Rn, #offset] Read word from memory location Rn + offset
LDRD Rd1,Rd2, [Rn,
#offset] Read double word from memory location Rn + offset
STRH Rd, [Rn, #offset] Store half word to memory location Rn + offset
Example Description
Read multiple words from memory location specified by Rd;
LDMIA Rd!,<reg list> address increment after (IA) each transfer (16-bit Thumb
instruction)
Store multiple words to memory location specified by Rd;
STMIA Rd!,<reg list> address increment after (IA) each transfer (16-bit Thumb
instruction)
LDMIA.W Rd(!),<reg list> Read multiple words from memory location specified by Rd;
address increment after each read (.W specified it is a 32-bit
Thumb-2 instruction)
Read multiple words from memory location specified by Rd;
LDMDB.W Rd(!),<reg list> address Decrement Before (DB) each read (.W specified it is a
32-bit Thumb-2 instruction)
Write multiple words to memory location specified by Rd;
STMIA.W Rd(!),<reg list> address increment after each read (.W specified it is a 32-bit
Thumb-2 instruction)
Write multiple words to memory location specified by Rd;
STMDB.W Rd(!),<reg list>
address DB
PREINDEXING and POSTINDEXING.
For preindexing, the register holding the memory address is adjusted. The
memory transfer then takes place with the updated address. For example,
LDR.W R0,[R1, #offset]! ; Read memory[R1+offset], with R1
update to R1+offset
The use of the “!” indicates the update of base register R1. The “!” is optional;
without it, the instruction would be just a normal memory transfer with offset from a
base address.
The preindexing memory access instructions include load and store instructions of
Examples of Preindexing Memory Access Instructions
various transfer sizes
Example Description
LDR.W Rd, [Rn, #offset]!
LDRB.W Rd, [Rn, #offset]! Preindexing load instructions for various sizes (word, byte, halfword,
LDRH.W Rd, [Rn, #offset]! and double word)
LDRD.W Rd1, Rd2,[Rn, #offset]!
LDRSB.W Rd, [Rn, #offset]!
Preindexing load instructions for various sizes with sign extend (byte,
LDRSH.W Rd, [Rn, #offset]! half word)
STR.W Rd, [Rn, #offset]!
STRB.W Rd, [Rn, #offset]!
Preindexing store instructions for various sizes (word, byte, halfword,
STRH.W Rd, [Rn, #offset]!
and double word)
STRD.WRd1, Rd2,[Rn, #offset]!
Postindexing memory access instructions carry out the memory transfer using the base address
specified by the register and then update the address register afterward. For example,
LDR.W R0,[R1], #offset ; Read memory[R1], with R1 updated to R1+offset
When a postindexing instruction is used, there is no need to use the “!” sign, because all
postindexing instructions update the base address register, whereas in preindexing you might choose
whether to update the base address register or not.
Examples of Postindexing Memory Access Instructions
Example Description
LDR.W Rd, [Rn] #offset
LDRB.W Rd, [Rn] #offset Postindexing load instructions for various sizes (word,
LDRH.W Rd, [Rn] #offset byte, half word, and double word)
LDRD.W Rd1, Rd2, [Rn] #offset
LDRSB.W Rd, [Rn] #offset Postindexing load instructions for various sizes with sign
LDRSH.W Rd, [Rn] #offset extend (byte, half word)
STR.W Rd, [Rn] #offset
STRB.W Rd, [Rn] #offset Postindexing store instructions for various sizes (word,
STRH.W Rd, [Rn] #offset
byte, half word, and double word)
STRD.W Rd1, Rd2, [Rn] #offset
Two other types of memory operation are stack PUSH and stack POP. For example,
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 will have a corresponding POP with the same register list, but
this is not always necessary. For example, a common exception is when POP is used as a
function return:
PUSH {R0-R3, LR} ; Save register contents at beginning of subroutine
In this case, instead of popping the LR register back and then branching to the address in
LR, we POP the address value directly in the program counter.
In the Cortex-M3 to access special registers, we use the instructions MRS and MSR. For
example,
MRS R0, PSR ; Read Processor status word into R0
MSR CONTROL, R1 ; Write value of R1 into control register
Unless you’re accessing the APSR, you can use MSR or MRS to access other special |
registers only in privileged mode.
Moving immediate data into a register is a common thing to do. For example,
you might want to access a peripheral register, so you need to put the address
value into a register beforehand. For small values (8 bits or less), you can use
MOVS (move).
For example,
MOVS R0, #0x12 ; Set R0 to 0x12
For a larger value (over 8 bits), you might need to use a Thumb-2 move
instruction. For example,
MOVW.W R0, #0x789A ; Set R0 to 0x789A
Or if the value is 32-bit, you can use two instructions to set the upper and lower
halves:
MOVW.W R0,#0x789A ; Set R0 lower half to 0x789A
MOVT. W R0,#0x3456 ; Set R0 upper half to 0x3456. Now R0=
0x3456789A
2. Data Processing Instructions
The Cortex-M3 provides many different instructions for data processing. Many
For example, an ADD instruction can operate between two registers or between
one register and an immediate data value:
ADD R0, R0, R1 ; R0 = R0 + R1
ADDS R0, R0, #0x12 ; R0 = R0 + 0x12
ADD.W R0, R1, R2 ; R0 = R1 + R2
These are all ADD instructions, but they have different syntaxes and binary
coding.
With the traditional Thumb instruction syntax, when 16-bit Thumb code is used,
2. Data Processing Instructions
To separate the two different operations, the S suffix should be used if the
following operation depends on the flags:
ADD.W R0, R1, R2 ; Flag unchanged
ADDS.W R0, R1, R2 ; Flag change
Aside from ADD instructions, the arithmetic functions that the Cortex-M3
supports include subtract (SUB), multiply (MUL), and unsigned and signed
divide (UDIV/SDIV).
Instruction List
16-Bit Data Processing Instructions
Instruction Function
ADC Add with carry
ADD Add
ADR Add PC and an immediate value and put the result in a register
AND Logical AND
ASR Arithmetic shift right
BIC Bit clear (Logical AND one value with the logic inversion of another value)
Compare negative (compare one data with two’s complement of another data
CMN
and update flags)
CMP Compare (compare two data and update flags)
Copy (available from architecture v6; move a value from one high or low
CPY
register to another high or low register); synonym of MOV instruction
EOR Exclusive OR
LSL Logical shift left
LSR Logical shift right
MOV Move (can be used for register-to-register transfers or loading immediate data)
MUL Multiply
MVN Move NOT (obtain logical inverted value)
NEG Negate (obtain two’s complement value), eq
Instruction List
16-Bit Data Processing Instructions Continued
Instruction Function
ORR Logical OR
RSB Reverse subtract
ROR Rotate right
SBC Subtract with carry
SUB Subtract
TST Test (use as logical AND; Z flag is updated but AND result is not stored)
REV Reverse the byte order in a 32-bit register (available from architecture v6)
REV16 Reverse the byte order in each 16-bit half word of a 32-bit register
(available from architecture v6)
REVSH Reverse the byte order in the lower 16-bit half word of a 32-bit register
and sign extends the result to 32 bits (available from architecture v6)
SXTB Signed extend byte (available from architecture v6)
SXTH Signed extend half word (available from architecture v6)
UXTB Unsigned extend byte (available from architecture v6)
UXTH Unsigned extend half word (available from architecture v6) 7
Instruction List
32-Bit Data Processing Instructions
Instruction Function Instruction Function
These instructions can be used with or without the “S” suffix to determine if the
APSR should be updated.
If UAL syntax is used and if “S” suffix is not used, the 32-bit version of the
instructions would be selected as all of the 16-bit logic operation instructions
update APSR.
Logic Operation Instructions
Instruction Operation
Instruction Operation
ORRRd, Rn ; Rd = Rd | Rn
EOR Rd, Rn ; Rd = Rd A Rn