简体   繁体   中英

Work with array in gcc inline assembler (ARM)

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++)

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 )

I think it should work like this:

[ARGV] "r" (argv)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM