PocketMatrix forums

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

Assembler directive help

Hi Gurus :D

I have written some asm that will be relocatable but am stuck on a problem accessing a table as follows

ASM_table DCB 1,2,3,4,5

and am referencing it using

ldr r5,=ASM_table
ldrb r1,[r5,r0]

This produces a pc relative lookup, but uses a fixed address at the end of the code to get the table, ie

ldr r5,[pc,#0x222]

this is not really acceptable to relocatable code, as it uses a fixed addess within a lookup table. What I would ideally like (and I'm not sure of the way of doing this is)

add r5,pc,-60

which will give me the address of the table, but without having to do a lookup and is fully relocateable, providing the table stays the same distance away from the code.

I don't see why this isn't possible, but I dont know the syntax on how to get the assembler to produce this code, rather than the first code which it currently produces.

I hope I've explained that clearly enough :)

Thanks

Glen.
GlenC wrote:
ldr r5,=ASM_table
ldrb r1,[r5,r0]


it's been a long time since I coded my last ARM assembly function but I think that you need an adr r5,=ASM_table, instead of ldr r5,=ASM_table

sorry if this is inacurate, I can't test it right now.
Hi Kak,

Thanks for taking the time to reply, but that didn't work :(

Cheers

Glen.
Hi all,

To answer my own question, here is the solution :)

Code:
add r5,pc,#(ASM_Table-.-8)


The . being the current pc counter, and the -8 to take into account the advancement of the pc counter.

This then produces the correct code, without an assembler generated indirection lookup. It should also be fully relocateable.

Cheers

Glen.
I fail to see how your first version is not relocatable. Infact the first solution is even better, since the lookup for the table would be in the code section (and thus has a constant distance from the code), while the table itself is usually in the data/bss section and may be moved.

Tala
Hi Tala,

The reason the first version is not suitable for relocation is the code generated by the assember will hard code the table refernce to a specific address

ie

Table address is say at 0x12345678

the code

ldr r5,=ASM_table

will actually produce

ldr r5,[pc,#0x222]

then at location

pc+0x222 DCW 0x12345678

The DCW produced by the assembler is hard coded to the table address, therefore it is not relocatable.

The data stays in the assembler code section, as it's dynamically loaded.

Glen.
then at location

pc+0x222 DCW 0x12345678


Hi GlenC,
thats exactly what i mean. I am sure the assembler will generate a relocation table entry fot that address. So the Linker/Loader is able to relocate the data and patch the DCW accordingly.

Tala
Hi,

This code is not suitable, as it needs to be relocatable, that is a hard coded address, and therefore not relocatable.

With my improved suggestion, I can take the block of code with data embedded and place it anywhere in memory and it will run just fine. 99% of arm code is relative based addressing, but that particular piece of generated code is using a fixed address, which is not acceptable.

I want to be able to say org at 0x1000, then block copy the code/data combination to any physical address and call it from there.

Glen.
I slowly get the idea what you are trying to achieve ... you do not want to relocate the code at link/load time but dynamically (at runtime)? So we are talking self modifying code here?
Well in this case you are right.

Tala