Thumb Instruction Set
Thumb Instruction Set
1824
The University of Manchester
t Outline: r the Thumb programmers model r Thumb instructions r Thumb implementation r Thumb applications
MANCHEstER
1824
The University of Manchester
t Outline: the Thumb programmers model r Thumb instructions r Thumb implementation r Thumb applications
MANCHEstER
1824
The University of Manchester
What is Thumb?
t Thumb is: r a compressed, 16-bit representation of a subset of the ARM instruction set
primarily to increase code density also increases performance in some cases
t It is not a complete architecture r all Thumb-aware cores also support the ARM instruction set
therefore the Thumb architecture need only support common functions
MANCHEstER
1824
The University of Manchester
31 30 29 28 27 NZCV
MANCHEstER
1824
The University of Manchester
Hi registers
MANCHEstER
1824
The University of Manchester
t Thumb register use: r r0 - r7 are general purpose registers r r13 is used implicitly as a stack pointer
in ARM code this is a software convention
r a few instructions can access r8 - r15 r the CPSR ags are set by data processing instructions & control conditional branches
MANCHEstER
1824
The University of Manchester
r support for 8-bit byte, 16-bit half-word and 32-bit data types
half-words are aligned on 2-byte boundaries words are aligned on 4-byte boundaries
MANCHEstER
1824
The University of Manchester
MANCHEstER
1824
The University of Manchester
t Outline: r the Thumb programmers model Thumb instructions r Thumb implementation r Thumb applications
MANCHEstER
1824
The University of Manchester
(2) B <label>
(3) BL <label>
15 12 11 7 6 5 0 1 0 0 0 1 1 1 0H
(4) BX Rm
MANCHEstER
1824
The University of Manchester
t These are similar to ARM instructions except: r offsets are scaled to half-word, not word r range is reduced to t into 16 bits r BL works in two stages:
H=0: LR := PC + signextend(offset << 12) H=1: PC := LR + (offset << 1) LR := oldPC + 3
r the assembler generates both halves r LR bit[0] is set to facilitate return via BX
MANCHEstER
1824
The University of Manchester
t Subroutine calls r later ARMs support BLX instruction r to synthesize BLX or earlier ARM:
ADR r0, subr + 1 ADR lr, return BX r0 return ... ;
2005 PEVEIT Unit ARM System Design
MANCHEstER
1824
The University of Manchester
t The Thumb SWI operates exactly like the ARM SWI r the (interpreted) immediate is just 8 bits
Thumb Angel SWI uses value 0xAB r0 call value is exactly as in ARM code
MANCHEstER
1824
The University of Manchester
15 13 12 11 10 6 5 0 0 0 op #sh
MANCHEstER
1824
The University of Manchester
MOV does not affect the ags (it can be distinguished using the mnemonic CPY after v6)
2005 PEVEIT Unit ARM System Design Thumb instruction set v5 15
MANCHEstER
1824
The University of Manchester
t Notes: r in Thumb code shift operations are separate from general ALU functions
in ARM code a shift can be combined with an
r ALU function in a single instruction r all data processing operations on the Lo registers set the condition codes
those on the Hi registers do not, apart from CMP which only changes the condition codes
MANCHEstER
1824
The University of Manchester
MANCHEstER
1824
The University of Manchester
15 12 11 10 8 7 1 1 0 0 L Rn 15 10 9 8 7 1 0 1 1 1 1 LR
MANCHEstER
1824
The University of Manchester
MANCHEstER
1824
The University of Manchester
t BLX works in two stages; (rst is same as BL) 15 12 11 10 11101 10-bit offset
(1) BLX <label>
H=0: LR := PC + signextend(offset << 12) H=1: PC := (LR + (offset << 2)) AND FFFFFFFC LR := oldPC + 3 T flag := 0
(2) BLX Rm
MANCHEstER
1824
The University of Manchester
r CPY r SXTB/SXTH/UXTB/UXTH
Sign extension (no shifts)
r REV/REV16/REVSH
Byte swaps
r SETEND r CPSIE/CPSID
Interrupt enable/disables (no mode changes)
MANCHEstER
1824
The University of Manchester
ARM/Thumb interworking
r the routine can then be called from both ARM and Thumb code
2005 PEVEIT Unit ARM System Design Thumb instruction set v5 22
MANCHEstER
1824
The University of Manchester
ARM/Thumb interworking
r ARM v4T
from ARM ADR ADR BX return_addr ... from Thumb LDR MOV BX ...
2005 PEVEIT Unit ARM System Design
; ; here + 4 ;
Thumb instruction set v5 23
MANCHEstER
1824
The University of Manchester
t Outline: r the Thumb programmers model r Thumb instructions Thumb implementation r Thumb applications
MANCHEstER
1824
The University of Manchester
Thumb decoding
MANCHEstER
1824
The University of Manchester
zero shift
immediate value
31 28 27 26 25 24 21 20 19 16 15 12 11 8 7 1 1 1 0 0 0 1 0 1 0 0 1 0 Rd 0 Rd 0 0 0 0
0 imm8
MANCHEstER
1824
The University of Manchester
t Outline: r the Thumb programmers model r Thumb instructions r Thumb implementation Thumb applications
MANCHEstER
1824
The University of Manchester
Thumb applications
MANCHEstER
1824
The University of Manchester
Thumb applications
t For the best performance: r use 32-bit memory and ARM code t For best cost and power-efciency: r use 16-bit memory and Thumb code t In a typical embedded system: r use ARM code in 32-bit on-chip memory for small speedcritical routines r use Thumb code in 16-bit off-chip memory for large noncritical control routines
2005 PEVEIT Unit ARM System Design Thumb instruction set v5 29
MANCHEstER
1824
The University of Manchester
t Explore further the ARM software development tools r Write Thumb assembly programs r Check that they work as expected