【问题标题】:Accessing element in array with assembly使用程序集访问数组中的元素
【发布时间】:2014-12-03 11:25:55
【问题描述】:

假设如下函数:

void asmFunction(const int *first, ...) {
    __asm {
        xor eax, eax
        add eax, [first][0]
        ; ...
    }
}

这样称呼它:

int first[] =  { 0, 0, 0, 0, 0, 0, 0, 5, 7, 6, 2, 4, 3, 5 };
asmFunction2(first, ...);

据我了解,第二条流水线必须将数组first0元素添加到eax。但是,正在添加一个随机值。调试时,first[0] 必须等于 0。代码有什么问题?

我在 64 位机器上使用 Visual Studio 2013 编写代码。

【问题讨论】:

  • 我不是专家,但不应该是add eax, [first + 0]吗?使用[],您可以取消引用first 指针/地址。对于元素 1:add eax, [first + 4]
  • @meaning-matters 据我所知,[first][0] 的含义与[first + 0] 相同,但我不确定。

标签: c visual-c++ assembly x86 inline-assembly


【解决方案1】:

这是一种奇怪的语法,可能无法满足您的要求。如果您反汇编生成的代码,您很可能会看到类似add eax, [ebp+8] 的内容。添加的随机值应该是first的值,它是一个指针。你已经有效地完成了eax += first。要获取元素,您需要一个间接级别,即eax += *first。例如这可以工作:

mov edx, [first]
add eax, [edx]

【讨论】:

  • 有趣。不幸的是,目前我无法对其进行测试。 add eax, [[first]] 语法是可接受的还是错误的?
  • x86 不支持双重间接,因此即使某些汇编程序接受这种形式,它也不会工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
  • 2016-02-23
  • 1970-01-01
相关资源
最近更新 更多