This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Switching to and from ARM/Thumb


This mail is somewhat off-topic for binutils. If you know of a better
mailing list, please point me in the right direction.

I'm writing a pair of macros that generate inline ARM assembler to
switch to ARM mode from Thumb mode, preform some operation, and then
switch back to Thumb mode. Their use looks something like this:

#define cli()							\
	do {							\
	  unsigned long temp;					\
	  __asm__ __volatile__(					\
arm(cli) \
"	mrs	%0, cpsr		@ cli\n"		\
"	orr	%0, %0, #128\n"					\
"	msr	cpsr, %0"					\
thumb(cli,%0) \
	  : "=r" (temp)						\
	  :							\
	  : "memory");						\
	} while(0)

The arm macro switches to ARM mode, the thumb macro switches to Thumb
mode using the temporary register %0.

I defined the two macros like so:

#define str(x) #x
#define xstr(x) str(x)
#define L(x) #x "_" xstr(__LINE__)

/** Switch to Arm mode. */
#define arm(x)               \
".balignw 4, 0x46c0 @ nop\n" \
"	bx pc\n"                 \
"	nop\n"                   \
".code 32\n"                 \
L(x) "_arm:\n"

/** Switch to Thumb mode. */
#define thumb(x,r) "\n"   \
"	add " #r ", pc, #1\n" \
"	bx " #r "\n"          \
".code 16\n"              \
".thumb_func\n"           \
L(x) "_thumb:"

Sadly, they simply don't work. I don't, currently, have a low-level
enough debugger to find out where it's going wrong. The following
nastier macros do, however, work!

/** Switch to Arm mode. */
#define arm(x) \
	"bl " L(x) "\n" \
"	b " L(x) "_thumb\n" \
".balign 4\n" \
L(x) ":\n" \
"	bx pc\n" \
"	nop\n" \
".code 32\n" \
L(x) "_arm:\n"
#endif

/** Switch to Thumb mode. */
#define thumb(x,r) "\n"   \
"	bx lr\n"              \
".code 16\n"              \
".thumb_func\n"           \
L(x) "_thumb:"

Does any bright soul see why the first pair of macros don't work?

While I'm on the topic, to which address does the following branch jump?

.code 16
0: nop
2: bx pc
4: nop
6: nop
.code 32
8: nop

Thanks for your help,
Shaun


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]