ARM VERSION 1.2 User Manual

Page 46

Advertising
background image

Writing ARM and Thumb Assembly Language

2-34

Copyright © 2000, 2001 ARM Limited. All rights reserved.

ARM DUI 0068B

Converting to Thumb

Example 2-8 shows the implementation of the jump table converted to Thumb code.

Most of the Thumb version is the same as the ARM code. The differences are
commented in the Thumb version.

In Thumb state, you cannot:

increment the base register of

LDR

and

STR

instructions

load a value into the pc using an

LDR

instruction

do an inline shift of a value held in a register.

Example 2-8 Thumb code jump table

AREA Jump, CODE, READONLY
CODE16 ; Following code is Thumb code
num EQU 2
ENTRY
start
MOV r0, #0
MOV r1, #3
MOV r2, #2
BL arithfunc
stop MOV r0, #0x18
LDR r1, =0x20026
SWI 0xAB ; Thumb semihosting SWI
arithfunc
CMP r0, #num
BHS exit ; MOV pc, lr cannot be conditional
ADR r3, JumpTable
LSL r0, r0, #2 ; 3 instructions needed to replace
LDR r0, [r3,r0] ; LDR pc, [r3,r0,LSL#2]
MOV pc, r0
ALIGN ; Ensure that the table is aligned on a
; 4-byte boundary
JumpTable
DCD DoAdd
DCD DoSub

DoAdd ADD r0, r1, r2
exit MOV pc, lr
DoSub SUB r0, r1, r2
MOV pc, lr
END

Advertising