将数组传递给 x86 asm 中的函数

发布于 2024-12-02 16:10:23 字数 519 浏览 2 评论 0原文

我正在学习 x86 asm 并使用 masm,并且正在尝试编写一个与以下 c 函数具有等效签名的函数:

void func(double a[], double b[], double c[], int len);

我不确定如何实现它?

asm 文件将被编译成 win32 DLL。

为了让我明白如何做到这一点,有人可以帮我将这个非常简单的函数翻译成asm吗:

void func(double a[], double b[], double c[], int len)
{
  // a, b, and c have the same length, given by len
  for (int i = 0; i < length; i++)
    c[i] = a[i] + b[i];
}

我尝试用C编写一个这样的函数,编译它,并使用OllyDbg查看exe中相应的反汇编代码,但是我甚至在其中找不到我的功能。

谢谢您。

I'm learning x86 asm and using masm, and am trying to write a function which has the equivalent signature to the following c function:

void func(double a[], double b[], double c[], int len);

I'm not sure how to implement it?

The asm file will be compiled into a win32 DLL.

So that I can understand how to do this, can someone please translate this very simple function into asm for me:

void func(double a[], double b[], double c[], int len)
{
  // a, b, and c have the same length, given by len
  for (int i = 0; i < length; i++)
    c[i] = a[i] + b[i];
}

I tried writing a function like this in C, compiling it, and looking at the corresponding disassembled code in the exe using OllyDbg but I couldn't even find my function in it.

Thank you kindly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2024-12-09 16:10:23

我已经有一段时间没有写 x86 了,但我可以给你一个如何写 x86 的一般概念。由于我手边没有汇编程序,所以这是用记事本编写的。

func proc a:DWORD, b:DWORD, c:DWORD, len:DWORD

  mov eax, len
  test eax, eax
  jnz @f
  ret

    @@:

  push ebx
  push esi

  xor eax, eax

  mov esi, a
  mov ebx, b
  mov ecx, c

    @@:

  mov edx, dword ptr ds:[ebx+eax*4]
  add edx, dword ptr ds:[ecx+eax*4]
  mov [esi+eax*4], edx
  cmp eax, len
  jl @b

  pop esi
  pop ebx

  ret  

func endp

上面的函数符合 stdcall,如果您的参数是整数,则大致是您将如何转换为 x86。不幸的是,您正在使用双打。循环是相同的,但您需要使用 FPU 堆栈和操作码来进行算术运算。我已经有一段时间没有使用它了,不幸的是我记不起这些说明了。

I haven't written x86 for a while but I can give you a general idea of how to do it. Since I don't have an assembler handy, this is written in notepad.

func proc a:DWORD, b:DWORD, c:DWORD, len:DWORD

  mov eax, len
  test eax, eax
  jnz @f
  ret

    @@:

  push ebx
  push esi

  xor eax, eax

  mov esi, a
  mov ebx, b
  mov ecx, c

    @@:

  mov edx, dword ptr ds:[ebx+eax*4]
  add edx, dword ptr ds:[ecx+eax*4]
  mov [esi+eax*4], edx
  cmp eax, len
  jl @b

  pop esi
  pop ebx

  ret  

func endp

The above function conforms to stdcall and is approximately how you would translate to x86 if your arguments were integers. Unfortunately, you are using doubles. The loop would be the same but you'd need to use the FPU stack and opcodes for doing the arithmetic. I haven't used that for a while and couldn't remember the instructions off the top of my head unfortunately.

泪之魂 2024-12-09 16:10:23

您必须传递数组的内存地址。考虑以下代码:

.data?
array1 DWORD 4 DUP(?)

.code
         main PROC

                      push LENGTHOF array1
                      push OFFSET array1
                      call arrayFunc             
         main ENDP

         arrayFunc PROC
                                   push ebp
                                   mov ebp, esp
                                   push edi

                                   mov edi, [ebp+08h] 
                                   mov ecx, [ebp+0Ch]
                                   L1:

                                  ;reference each element of given array by [edi]
                                  ;add "TYPE" *array* to edi to increment
                                   loop L1:
                                   pop edi
                                   pop ebp
                                   ret 8
         arrayFunc ENDP
         END main

我编写这段代码只是为了让您理解这个概念。我将它留给您来弄清楚如何正确计算寄存器的使用以实现程序的目标。

You have to pass the memory addresses of the arrays. Consider the following code:

.data?
array1 DWORD 4 DUP(?)

.code
         main PROC

                      push LENGTHOF array1
                      push OFFSET array1
                      call arrayFunc             
         main ENDP

         arrayFunc PROC
                                   push ebp
                                   mov ebp, esp
                                   push edi

                                   mov edi, [ebp+08h] 
                                   mov ecx, [ebp+0Ch]
                                   L1:

                                  ;reference each element of given array by [edi]
                                  ;add "TYPE" *array* to edi to increment
                                   loop L1:
                                   pop edi
                                   pop ebp
                                   ret 8
         arrayFunc ENDP
         END main

I just wrote this code for you to understand the concept. I leave it to you to figure out how to properly figure the usage of registers in order to achieve your program's goals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文