简体   繁体   English

如何从汇编程序访问Windows x64的C数组?

[英]How to access C arrays from assembler for Windows x64?

I've written an assembler function to speed up a few things for image processing (images are created with CreateDIBSection). 我编写了一个汇编程序函数来加速图像处理的一些事情(使用CreateDIBSection创建图像)。

For Win32 the assembler code works without problems, but for Win64 I get a crash as soon as I try to access my array data. 对于Win32,汇编程序代码可以正常工作,但对于Win64,一旦我尝试访问我的数组数据,我就会崩溃。

I put the relevant info in a struct and my assembler function gets a pointer to this struct. 我将相关信息放在结构中,我的汇编程序函数获取指向此结构的指针。 The struct pointer is put into ebx/rbx and with indexing I read the data from the struct. 结构指针放在ebx / rbx中,通过索引我从结构中读取数据。

Any idea what I am doing wrong? 知道我做错了什么吗? I use nasm together with Visual Studio 2008 and for Win64 I set "default rel". 我将nasm与Visual Studio 2008一起使用,对于Win64,我设置了“default rel”。

C++ code: C ++代码:

struct myData {
  tUInt32 ulParam1;
  void* pData;
};

CallMyAssemblerFunction(&myData);

Assembler Code: 汇编代码:

Win32: Win32的:

...
  push ebp;
  mov ebp,esp
  mov ebx, [ebp + 8]; pointer to our struct
  mov eax, [ebx]; ulParam1
  mov esi, [ebx + 4]; pData, 4 byte pointer

  movd xmm0, [esi];
...

Win64: Win64平台:

...
  mov rbx, rcx; pointer to our struct
  mov eax, [rbx]; ulParam1
  mov rsi, [rbx + 4]; pData, 8 byte pointer

  movd xmm0, [rsi]; CRASH!
...

Quite probably, the pData field is at [rbx + 8] , not [rbx + 4] . 很可能, pData字段位于[rbx + 8] ,而不是[rbx + 4] The compiler inserts some extra space ("padding") between ulParam1 and pData so that pData is 8-byte aligned (which makes accesses faster). 编译器在ulParam1pData之间插入一些额外的空格(“填充”),以便pData是8字节对齐的(这使得访问更快)。

Take a look at your struncture in memory. 看看你的记忆中的穿刺。 May be offset is different in x64 可能是x64中的偏移量不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM