Can we use Address of operator “&” inline GCC ARM

2019-08-12 07:13发布

问题:

Can we use Address of operator "&" inline GCC ARM assembly? If yes then I have a structure core_regx and I need to pass the address of a member r0 of that strucutre into the below mentioned code:

asm volatile("ldr r3, [%0,#0]":: "r" (&(core_reg->r0)));

Please check if this code is correct or not.

回答1:

Yes, you certainly can use &. However, I would suggest that your assembler specifiers may have some issues and better options.

asm volatile("ldr r3, %0":: "m" (core_reg->r0) : "r3");

You definitely should add r3 to the clobber list. Also, the "m" specifier is probably better. If core_reg is already in r0, the compiler can use the offset of r0 member and generate code such as,

       add r0, r0, #12    ; assuming r0 is core_reg.
       ldr r3, [r0]

The compiler knows the relation between core_reg and core_reg->r0. At least "m" works well with some versions of arm-xxx-gcc. Run objdump --disassemble on the code the compiler generates to verify it is doing what you want.

Edit: The GCC manual has lots of information, such as Gcc assembler contraints, Machine specific and General Info. There are many tutorials on the Internet such as the ARM assembler cookbook, which is one of the best.



标签: c gcc assembly arm