【问题标题】:How to use 32-bit w registers in ARM aarch64 GCC inline assembly?如何在 ARM aarch64 GCC 内联汇编中使用 32 位 w 寄存器?
【发布时间】:2020-04-11 04:42:32
【问题描述】:

我可以使用 64 位寄存器,例如:

#include <assert.h>
#include <inttypes.h>

int main(void) {
    uint64_t io = 1;
    __asm__ (
        "add %[io], %[io], 1;"
        : [io] "+r" (io)
        :
        :
    );
    assert(io == 2);
}

编译和反汇编:

aarch64-linux-gnu-gcc -ggdb3 -o main.out main.c
gdb-multiarch -batch -ex 'disassemble/rs main' main.out

按预期到一个 64 位寄存器:

6           __asm__ (
   0x0000000000000744 <+16>:    a0 0f 40 f9     ldr     x0, [x29, #24]
   0x0000000000000748 <+20>:    00 04 00 91     add     x0, x0, #0x1
   0x000000000000074c <+24>:    a0 0f 00 f9     str     x0, [x29, #24]

如何改用 w0 等 32 位寄存器?

在 Ubuntu 18.04、GCC 7.4.0 上测试。

【问题讨论】:

标签: gcc arm inline-assembly arm64


【解决方案1】:

可以通过在%前面添加w来完成,例如:

#include <assert.h>
#include <inttypes.h>

int main(void) {
    uint32_t io = 1;
    __asm__ (
        "add %w[io], %w[io], 1;"
        : [io] "+r" (io)
        :
        :
    );
    assert(io == 2);
}

现在反汇编为所需的 32 位版本:

6           __asm__ (
   0x0000000000000744 <+16>:    a0 1f 40 b9     ldr     w0, [x29, #28]
   0x0000000000000748 <+20>:    00 04 00 11     add     w0, w0, #0x1
   0x000000000000074c <+24>:    a0 1f 00 b9     str     w0, [x29, #28]

这可能是从http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.100067_0612_00_en/qjl1517569411293.html 中猜到的,因为 ARM 编译器 6 基于 LLVM,而 LLVM 语法主要基于 GCC。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多