Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8015

General • Re: Assign variables to specific addresses?

$
0
0
What is this sequence supposed to do ?
I have discovered a better, more flexible, and more elegant way than using '#define'. Credit goes to 'user6409471' and 'fearless_fool' on StackOverflow.

Code:

extern volatile uint32_t u32_one; asm("u32_one = 0x20010000");extern volatile uint32_t u32_two; asm("u32_two = 0x20010004");
It just stores 0x20010000 / 0x20010004 values at some otherwise meaningless locations, and then uses them like pointers.

200003b8:20010000 .word0x20010000
200003bc:20010004 .word0x20010004


2000017a:2401 movsr4, #1
2000017c:4b8e ldrr3, [pc, #568]@ (200003b8 <.J11+0xae>)
2000017e:601c strr4, [r3, #0]
20000180:4b8e ldrr3, [pc, #568]@ (200003bc <.J11+0xb2>)
20000182:2202 movsr2, #2
20000184:601a strr2, [r3, #0]
This is how GCC / ARM implement literal pools. Also note the PC-relative addressing, because the pool is usually dumped immediately after the function. There are assembler directives to do so.
In this example, memory cell at 0x200003B8 holds a literal - the address of the actual variable.
Any global variable is accessed in this way - first a pointer is loaded from the literal pool.

Statistics: Posted by cntnib — Sun Mar 23, 2025 9:04 am



Viewing all articles
Browse latest Browse all 8015

Trending Articles