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

General • Re: Assign variables to specific addresses?

$
0
0

Code:

extern volatile SomeType my_var; asm("my_var = 0x20010000");
If I understand things correctly this is actually slower than it needs to be. The volatile tells the compiler that the variable may change not the destination. So for each access of the destination the compiler is forced to load the variable and then load the destination. Where as if you tell the compiler the destination is volatile then the pointer can stay in a register.
Whether or not the variable needs to be volatile at all depends on the application, but this construct isn't adding any extra volatility.

So far as the compiler is concerned, this is just a normal volatile variable (that it expects the linker to fill in the address), and the compiler is entitled to keep the address of the variable in a register if it wants to. The asm is simply forcing the linker to place the variable at a fixed address - it can't have any effect on the compiler's code generation - it doesn't even know that the asm statement has anything to do with the preceding variable declaration: they've been placed here on the same line for human readability, but they could equally have been miles away or in a different file altogether.

Statistics: Posted by arg001 — Sun Mar 23, 2025 12:30 pm



Viewing all articles
Browse latest Browse all 8013

Trending Articles