Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
gnu assembler / thumb / how do I access .data?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3104

PostPosted: Sat Sep 22, 2018 9:14 am    Post subject: gnu assembler / thumb / how do I access .data? Reply with quote

I'm playing with a microcontroller, trying some bare-metal assembler programming.
I came to the point where I actually want to use some binary data. The big question is:
How do I access stuff in .data section?

Here's the snippet of code:
Code:
        ldr r6, mystring
loop:
        str r3, [r5]           @ set Port C
        ldr r1, = DELAY
        ror r3, r3, #1
delay1:
        subs r1, 1
        bne delay1

        str r2, [r5]            @ clear Port C
        ldr r1, = DELAY
delay2:
        subs r1, 1
        bne delay2

        b loop                 @ continue forever

_generic_handler:                        @ if any int gets triggered, just hang in a loop
        add r0, 1
        add r1, 1
        b _generic_handler:

.global _start

.data
mystring:
.string "hello world"

It starts with the line on which the assembler fails.
Quote:
Error: cannot represent T32_OFFSET_IMM relocation in this object file format

Assembler invoked with a command:
Quote:
arm-none-eabi-as "${src}.asm" -o "${src}".elf


Any tips on this? (Beside "use C instead" ;) )
Back to top
View user's profile Send private message
Akkara
Bodhisattva
Bodhisattva


Joined: 28 Mar 2006
Posts: 6702
Location: &akkara

PostPosted: Sat Sep 22, 2018 12:12 pm    Post subject: Re: gnu assembler / thumb / how do I access .data? Reply with quote

I'm not sure what microcontroller you're using so this advice might not be fully applicable, but hopefully there's enough to nudge in the right direction.

szatox wrote:
Any tips on this? (Beside "use C instead" ;) )

Use C :). Note, however, I did not say "instead".
try.c:
char const string[] = "hello world";

char const *get()
{
    return string;
}

Then compile that gcc -O2 -S -o - try.c and see how the compiler does it. Then do likewise.

gcc -O2 -c try.c && objdump -d try.o can be instructive to look at as well.

I suspect that your problem may be that memory-load instruction uses register-plus-offset addressing, and that the offset is limited in size to something less than 32 bits. If that is the case, you'll need to create a "const" pointer to your string in program memory somewhere near where you are using it, then use "PC"-relative addressing + offset to load the actual location from there. (Note: that's a "const" pointer to the string, living in the ".text" space mingled with the opcodes (putting it after a return or unconditional branch is common), not a pointer to a "const" string. A "const" pointer to a "const" string is even better unless you need to write into the string's memory (see below).)

Sometimes there's a register pointing to the base of the global data pool where the string and other static constants live, and one uses offsets off this base register. It still needs to be initialized using one of the other methods but you only have to do it once.

Also, unless you intend to store into the string (changing it at runtime), you probably want to put it into read-only memory. That would be the .rodata section if I recall. Or you can put your "const" string intermingled with the opcodes and not need a load opcode at all - a simple PC + offset addition generates the address in that case. Just put it after a 'return' or branch where normal execution can't get to, otherwise you'll have some interesting debugging to do.

Final note: all this is very architecture-dependent. If I recall, ARM likes to create const-pointers that are intermingled with opcodes, while something like the AVR chips can't easily access data that's stored in the instruction stream. X86 has long offsets and just uses the address directly, or possibly as a PC-relative for position-independence (-fPIC flag). So it all depends. I don't recall exactly because I tend to use assembler for inner critical calculation loops and leave the memory allocation questions to the compiler.
_________________
Many think that Dilbert is a comic. Unfortunately it is a documentary.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum