1
votes

I have trouble with some inline assembly code. I'm trying to load items from local static array into registers on ARM platform. Unfortunately I have no idea how to tell GCC that it should pass pointer on array to register. This register will be used for indirect acess to array.

// should return argv[1]
int test() {
    int argv[4] = {4, 3, 2, 1};
    int out;

    __asm__ volatile (
        "ldr r0, %[ARGV]" "\n\t"
        "mov r1, #4" "\n\t"
        "ldr r2, [r0, r1]" "\n\t"
        "mov %[OUT], r2"
        : [OUT] "=r" (out)
        : [ARGV] "m" (argv)   //  <==== i don't know which constraint put here :/
        : "r0", "r1", "r2"
    );

    return out;
}

Now the GCC throw error and I have no idea how to fix it:

Assembler messages:
Error: invalid offset, value too big (0xFFFFFFFC)

Thx

EDIT: I have compiled it with Android NDK (arm-linux-androideabi-g++)

2
This assembles, links and runs fine for me using clang. Not sure what's going on for you. You sure it's in this code that it's erroring?mattjgalloway
I forget to tell that I compile it with Android NDK (arm-linux-androideabi-g++)zdenek
Does it work for you if you set the constraint of argv to "rm" instead?mattjgalloway
Yeh I can't quite succinctly answer this yet, but I'm pretty sure there's a subtlety here that we're missing. Must have been luck that it worked with clang for me.mattjgalloway
I've tried convert argv pointer into int (int i_argv = (int)argv;) and pass i_argv instead of argv and it working! But i don't think this is a solution. It's only a hack :/zdenek

2 Answers

1
votes

I think it should work like this:

[ARGV] "r" (argv)

That says "load the address of the array into a register".

2
votes

You don't need to move ARGV or OUT to/from registers, that is what the register constraints handle for you.

"mov r1, #4\n\t"
"ldr %[OUT], [%[ARGV], r1]\n\t"
: [OUT] "=r" (out)
: [ARGV] "r" (argv)
: "r1"

note: this code does have problems when compiled with too high optimization settings. ( i don't know how to solve that, except: use -O0 )