Robert Chapman

don't optimize my assembler code!

Discussion created by Robert Chapman on Jul 26, 2011
Latest reply on Jul 26, 2011 by Robert Chapman

I have a function which is trying to figure out where it is in memory and calculate the offset from where it was compiled so that it can bootstrap itself properly. I have added this assembly code to the startup C function with the simple goal of calculating the offset between compiled location and runtime location:

__here__:

   lea    -2(PC), a1    // get running address of label

   lea    __here__, a0  // get compiled address of label

   sub    a0, a1        // get relative offset

The first instruction gets the runtime address. The second instruction gets the compile time address. The third instruction calculates the offset. If the code is run where it is compiled, the offset will be zero. If it is anywhere else, then the offset will not be zero. What happens when I compile the code is that the second instruction gets compiled as a pc-relative instruction so that it is like the first instruction instead of compiling as an absolute addressing mode instruction as I requested! Optimize my C but don't optimize my assembler! How can I get the compiler to compile absolute addressing mode instructions when I ask it to? Optionally, is there another way of figuring out the offset between where my code was compiled for and where it is running? Thanks.

Outcomes