将数组传递给x86 asm中的函数 [英] passing arrays to functions in x86 asm
问题描述
我正在学习x86 asm并使用masm,并试图编写一个具有与以下c函数等效的签名的函数:
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);
我不确定如何实现?
asm文件将被编译为win32 DLL.
The asm file will be compiled into a win32 DLL.
为了让我能理解该怎么做,有人可以帮我将这个非常简单的功能转换为asm:
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];
}
我试图用C编写这样的函数,进行编译,然后使用OllyDbg在exe中查看相应的反汇编代码,但是我什至找不到它的函数.
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.
谢谢.
推荐答案
我已经有一段时间没有写x86了,但是我可以给你一个大致的思路.由于我没有方便的汇编器,因此这是在记事本中编写的.
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
上面的函数符合stdcall,大约是参数为整数时如何转换为x86的方法.不幸的是,您正在使用双打.循环是相同的,但是您需要使用FPU堆栈和操作码来进行算术运算.我有一段时间没有使用它了,不幸的是我不记得上面的指示.
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.
这篇关于将数组传递给x86 asm中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!