PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Thumb instructions, BX instruction

I just started doing some low level code for my ipaq 4700 and Im getting some curious errors from microsoft's ARMASM compiler.

Coming from a gameboy advance background, I was used to doing c-like switch statements in arm assembler with the following (gas syntzx for gcc):

Code:

ldr r2, jumplist
ldr r3, [r2, r0, lsl #2]
bx r3 ; branch to case statement

jumplist:
dd jump1
dd jump2
dd jump3
.... so on

jump1:
... code

jump2:
... more code



this is a very standard way of doing switch statements in gameboy advanced development, but for some reason microsoft doesnt seem to agree with that view. When I do an asm listing of a switch statement in C, the bx <reg> method is changed with an add pc, pc, <reg> scheme. Acceptable, but incredibly difficult to code by hand, since now instead of labels, I have to code relative offsets. Might be ok for a simple switch, but in the routine I'm writing I need to juggle quite a few complex switch statements and I sure as hell am not going to do the offset arithmetic by hand.

Why is the BX instruction causing link errors? When I comment it out, not a peep from the compiler. The moment it appears, I get all kinds of noise about mixed arm/thumb compiled modules. And what happened to the thumb instruction set for that matter..if bx cannot be used, how the hell do you get to the 16bit instructions..or do they even exist on the pxa270? I thought all arm processors do both instructions sets, regardless of intel's enhancements.

When I switch the project to thumb compilation, the error with the bx instruction goes away, but the device now complains that the application is not valid. Im getting a bad feeling because if I cannot use the BX or thumb instructions, then all my work is for nothing...I need full access to all instructions, not just a subset.

If there is an alternative ide/compiler suite (eg GCC) I can use that does proper thumb/arm interop calls, please let me know, Im getting the feeling eVC did not get it right.
I thought all arm processors do both instructions sets, regardless of intel's enhancements.
Nope. StrongARM doesn't, for example.

Try specifying the "/QRinterwork-return" and "/QRarch4T" switches for clarm (I've never bothered to try using Thumb on a PocketPC so that may not help).

Might be ok for a simple switch, but in the routine I'm writing I need to juggle quite a few complex switch statements and I sure as hell am not going to do the offset arithmetic by hand.

Most ARM compilers, including the later Microsoft ones will synthesize a branch table from a "densely" populated switch statement in C.

I need full access to all instructions, not just a subset.
There's really not all that much of a need for using Thumb on PocketPCs anyway (in my opinion at least). I would (and do) just stick to full 32-bit with their extra capabilities. :P (admittedly I don't have a Thumb codebase to use).
MrMister wrote:If there is an alternative ide/compiler suite (eg GCC) I can use that does proper thumb/arm interop calls, please let me know, Im getting the feeling eVC did not get it right.
There are at least 3 different versions of GCC compiler for PocketPC, I don't know how they support thumb instructions.
Assuming I'm writing code in C, yes, I'd love to let clarm take care of the mess, but the routine I'm writing is in pure arm assembler. The thing thats killing me is that every platform I've ever worked on always had a jump instruction to an absolute address, which is the BX opcode in arm assembler. Would mov pc, <reg> allow me to do an absolute jump? I wanted to avoid that but seems I dont have much of a choice.
Sure you can modify the processor counter. If you've ever returned from a routine that didn't stack the processor counter then you've done it already (mov pc, r14).

Assuming I'm writing code in C, yes, I'd love to let clarm take care of the mess, but the routine I'm writing is in pure arm assembler.

Ok, so your routine is entered in 32-bit mode and always exits in 32-bit mode?
If I'm not mistaken there is no absolute jump or branch instruction in arm assembler.

When I brach or jump over large distances I do it the lazy way and write:

ldr pc, =SomeSymbol

Where SomeSymbol works for any symbols. Regardless if they are inside the same code-module or imported. This will synthesize a load instruction from memory and generate the 32 bit address somewhere near the instruction in the literal pool automatically.

Not a clever solution since it requires a memory access, but works and maintains readability.
torus wrote:If I'm not mistaken there is no absolute jump or branch instruction in arm assembler.


If you read my last reply, I said there is the BX instruction.

From a gba reference I have:

ARM.3: Branch and Exchange (BX)

Opcode Format

Bit Expl.
31-28 Condition
27-4 Must be "0001.0010.1111.1111.1111.0001" for this instruction
Opcode (fixed)
BX{cond} Rn ;PC = Rn
3-0 Rn - Operand Register (R0-R14)

Switching to THUMB Mode: Set Bit 0 of the value in Rn to 1, program continues then at Rn-1 in THUMB mode.
Results in undefined behaviour if using R15 (PC+8 itself) as operand.
Execution Time: 2S + 1N
Return: No flags affected.

On the arm7tdmi, this instruction allows a developer to branch directly to an absolute address stored in the operand register, plus it switch the processor between 32bit/16bit instruction sets.

Im not concerned much by not having thumb instructions, but the bx opcode itslef would make my code MUCH easier to write.

To Refractor: yes its 32bit, called from c++.

heres a link to the reference: http://www.work.de/nocash/gbatek.htm#arminstructionset
It's GBA specific, but its a very handy reference for arm/thumb assembler as well.
My reference sais that the BX instruction takes a register (and only a register) as an argument..

Strange.
Im not concerned much by not having thumb instructions, but the bx opcode itslef would make my code MUCH easier to write.

For which part? Just for the branch table? Basically I'm not understanding why you couldn't cope without the BX instruction. :S
Where is the problem with just loading the PC (ie. "mov pc,r3" instead of "bx r3")? You really dont need BX unless you want to switch to thumb (which you do not want anyway).

Tala
Thanks for the input guys...I might be annoying at times , but I'm also pretty stubborn, hard to teach an old dog new tricks, etc.

I managed to get the add pc, pc, reg method working, and its fine now. Basically wrote a high speed font routine with precompiled glyphs (massively unrolled loops) and it works great. If interested Ill share the obj/asm file, its a simple 8x8 dos font for text mode stuff.