[英]C variable usage in assembly, how to choose 32bit operands in arm aarch64
I am trying to use C variables in assembly. 我试图在程序集中使用C变量。 The purpose is to read a 32bit memory and assign it to a C variable. 目的是读取32位存储器并将其分配给C变量。
uint32_t ASMRegRd32(uint32_t addr) {
uint32_t data;
asm volatile (
"ldr %0, [%1]" "\n"
: "=r" (data)
: "r"(addr)
);
return data;
}
Sadly being on AARCH64, above ldr
instruction is returning a 64bit value, as compiler is choosing a 64bit operand Xt. 遗憾的是,在AARCH64上, ldr
指令返回64位值,因为编译器正在选择64位操作数Xt。
How can I restrict it to use a 32bit operand? 如何限制它使用32位操作数?
Should this work? 这有用吗?
ldr w0, [%1];
mov %0, w0;
You use the constraint template modifier "w". 您使用约束模板修饰符“w”。
asm (
"ldr %w[DST],[%[SRC]]" "\n"
: [DST] "=r" (data) // 32 bit variable
: [SRC] "X" (addr) // source address variable (expands into a x register)
);
Maybe you should change your API like below if you don't want to recompile the code in A32 compiler. 如果您不想在A32编译器中重新编译代码,也许应该像下面那样更改API。
uint32_t ASMRegRd32(uint64_t addr) {
uint32_t data;
asm volatile (
"ldrh w0, [%1]\n"
: "=&r" (data)
: "r"(addr)
: "cc"
);
return data;
}
because the default register is 64-bit, so the var addr conversion from 64-bit to 32-bit will make the program core dump. 因为默认寄存器是64位,所以从64位到32位的var addr转换将使程序核心转储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.