内联汇编中的内存偏移

内联汇编中的内存偏移

本文介绍了内联汇编中的内存偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 A64 汇编程序中,有多种指定地址的方法.

In A64 assembler, there are different ways to specify addresses.

/*
[base{,#0}]                 Simple register (exclusive) - Immediate Offset
[base{,#imm}]               Offset - Immediate Offset
[base,Xm{,LSL #imm}]        Offset - Register Offset
[base,Wm,(S|U)XTW {#imm}]   Offset - Extended Register Offset
[base,#imm]!                Pre-indexed - Immediate Offset
[base],#imm                 Post-indexed - Immediate Offset
label                       PC-relative (literal) load - Immediate Offset
*/

我想在内联汇编程序中使用偏移 - 立即偏移".


I would like to use "Offset - Immediate Offset" in inline assembler.

__asm__("ldp x8, x9, %0, 16 \n\t"
        :
        : "m" (*somePointer)
        : "x8", "x9");

这被翻译成

ldp x8, x9, [x0], 16

我的目标是达到

ldp x8, x9, [x0, 16]

如何使用内联汇编器编写此类指令?

推荐答案

我没有用于测试的 ARM 64 位工具链,但您应该能够执行以下操作:

I don't have an ARM 64-bit toolchain to test this with, but you should be able to do something like this:

asm("ldp x8, x9, %0\n\t"
    :
    : "Ump" (*((__int128 *) somePointer + 1))
    : "x8", "x9");

Ump 约束将内存操作数限制为整数 LDP 指令允许的操作数,否则它的工作方式与 m 约束相同.如果 somePointer 已经是一个指向 128 位类型的指针,你可以使用 somePointer[1] 作为操作数.

The Ump constraint limits the memory operand to those permitted by the integer LDP instruction, otherwise it works like the m constraint. If somePointer is already a pointer to a 128-bit type you can just use somePointer[1] as the operand.

如果上述方法不起作用,那么 David Wohlferd 的建议应该:

If the above doesn't work then David Wohlferd's suggestion should:

asm("ldp x8, x9, [%0, %1]"
    :
    : "r" (somePointer), "i"(16), "m" (*somePointer)
    : "x8", "x9");

这篇关于内联汇编中的内存偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 19:22