其他分享
首页 > 其他分享> > AArch64 X/W宽度通用寄存器之间的关系

AArch64 X/W宽度通用寄存器之间的关系

作者:互联网

 

 

 

AArch64 X/W宽度通用寄存器之间的关系

操作W宽度的寄存器,其高32位将会置0,只有低32位有效。比如往先往X10里写一个64bit的数UL(-2),再往W10里写一个32bit的数1<<31,之后再读X10,读到的值是0x80000000,如下面的测试code:

    unsigned long val64 = UL(-2);
    unsigned int val32 = 1<<31;
    unsigned long result = 0;
    
    asm volatile("mov x10, %x1\n"
                 "mov w10, %w2\n"
                 "mov %x0, x10":  "=r"(result) : "r"(val64), "r"(val32));

    pr_emerg("result is %#lx.\n", result);

 

执行结果为:

result is 0x80000000.

 

使用通过寄存器的AArch64内联汇编示例

   unsigned long val64 = UL(-2);
    unsigned int val32 = 1<<31;
    unsigned long result = 0;
    
    asm volatile("mov x10, %x1\n"
                 "mov w10, %w2\n"
                 "mov %x0, x10":  "=r"(result) : "r"(val64), "r"(val32));

    pr_emerg("result is %#lx.\n", result);

 

注意:

1. %n里指定x和w,x、w分别表示操作数为64bit、32bit

2. 如果输入操作数已经有显示初始话设值,则其attribute里不能加Z属性,否则编译会报错;但如果它是函数形参,则它的值可能为0,所以可以加Z属性,此时编译不会报错。Z属性的含义是:

A constant with value zero, printed as the zero register (XZR or WZR). Useful when combined with r (see Using multiple alternative operand constraints) to represent an operand that can be either a general-purpose register or the zero register.

 

标签:通用寄存器,AArch64,register,unsigned,UL,zero,宽度
来源: https://www.cnblogs.com/aspirs/p/15913137.html