Search code examples
windowsassemblymasmmasm32

passing arrays to functions in x86 asm


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.


Solution

  • 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.