05 Instruction Set
05 Instruction Set
05 Instruction Set
1
Overview
Memory System and Addressing
Thumb Instruction Set
2
Memory Maps For Cortex M0+ and MCU
KL25Z128VLK4
0x2000_2FFF
SRAM_U (3/4)
16 KB SRAM
0x2000_0000
SRAM_L (1/4)
0x1FFF_F000
0x0001_FFFF
128KB Flash
0x0000_0000
Endianness
For a multi-byte
value, in what order
are the bytes stored?
Little-Endian: Start
with least-significant
byte
Examples
ADDS <Rd>, <Rn>, <Rm>
Add registers: <Rd> = <Rn> + <Rm>
AND <Rdn>, <Rm>
Bitwise and: <Rdn> = <Rdn> & <Rm>
CMP <Rn>, <Rm>
Compare: Set condition flags based on result of computing
<Rn> - <Rm>
Where Can the Operands Be Located?
In a general-purpose register R
Destination: Rd
Source: Rm, Rn
Both source and destination: Rdn
Target: Rt
Source for shift amount: Rs
An immediate value encoded in instruction word
In a condition code flag
In memory
Only for load, store, push and pop instructions
Update Condition Codes in APSR?
14
Memory Addressing
LDR R0, [R4, #8] will load register R0 with the contents of
the memory word (4 bytes) starting at location R4 + 8.
STR R1, [R4, R5] will store register R1 to the memory word
(4 bytes) starting at location R4 + R5.
15
Loading/Storing Smaller Data Sizes
Some load and store instructions can handle half-word (16
bits) and byte (8 bits)
Store just writes to half-word or byte
STRH, STRB
Loading a byte or half-word requires padding or extension:
What do we put in the upper bits of the register?
Example: How do we extend 0x80 into a full word?
Unsigned? Then 0x80 = 128, so zero-pad to extend to word
0x0000_0080 = 128
Signed? Then 0x80 = -128, so sign-extend to word 0xFFFF_FF80
= -128
Signed Unsigned
Byte LDRSB LDRB
Half-word LDRSH LDRH
In-Register Size Extension
Can also extend byte or half-word already in a register
Signed or unsigned (zero-pad)
How do we extend 0x80 into a full word?
Unsigned? Then 0x80 = 128, so zero-pad to extend to word
0x0000_0080 = 128
Signed? Then 0x80 = -128, so sign-extend to word
0xFFFF_FF80 = -128
Signed Unsigned
Byte SXTB UXTB
Half-word SXTH UXTH
Load/Store Multiple
LDM/LDMIA: load multiple registers starting from
[base register], update base register afterwards
LDM <Rn>!,<registers>
LDM <Rn>,<registers>
STM/STMIA: store multiple registers starting at [base
register], update base register after
STM <Rn>!, <registers>
LDMIA and STMIA are pseudo-instructions, translated
by assembler
An example:
LDM R4, {R0, R1, R2, R3}
Pseudo-code:
• For the ARM Cortex-M, all pushes and pops use 32-bit data items; no other size is possible.
• Since all possible stack pointer values are multiples of four, the hardware is designed so that the
two least significant bits of the stack pointer are always zeros. 22
Add Instructions
Add registers, update condition flags
ADDS <Rd>,<Rn>,<Rm>
Add registers and carry bit, update condition
flags
ADCS <Rdn>,<Rm>
Add registers
ADD <Rdn>,<Rm>
Add immediate value to register
ADDS <Rd>,<Rn>,#<imm3>
ADDS <Rdn>,#<imm8>
Add Instructions with Stack Pointer
Add SP and immediate value
ADD <Rd>,SP,#<imm8>
Signed multiply
Note: upper word of result is truncated
Logical Operations
Bitwise AND registers, update condition flags
ANDS <Rdn>,<Rm>
Bitwise OR registers, update condition flags
ORRS <Rdn>,<Rm>
Bitwise Exclusive OR registers, update condition flags
EORS <Rdn>,<Rm>
Bitwise AND register and complement of second register,
update condition flags
BICS <Rdn>,<Rm>
Move inverse of register value to destination, update
condition flags
MVNS <Rd>,<Rm>
Update condition flags by ANDing two registers, discarding
result
TST <Rn>, <Rm>
Compare
Compare - subtracts second value from first,
discards result, updates APSR
CMP <Rn>,#<imm8>
CMP <Rn>,<Rm>
REV16 <Rd>,<Rm>
REVSH - reverse bytes MSB LSB
in low half-word
(signed) and sign- MSB LSB
extend Sign extend
REVSH <Rd>,<Rm> MSB LSB
Changing Program Flow - Branches
Unconditional Branches
B <label>
Target address must be within 2 KB of branch instruction
(-2048 B to +2046 B)
Conditional Branches
B<cond> <label>
<cond> is condition - see next page
B<cond> target address must be within 256 B of branch
instruction (-256 B to +254 B)
Condition Codes
Append to branch
instruction (B) to make
a conditional branch