nasm 函数返回一个数组 x86

getArguments.asm:

extern VirtualAlloc

section .text
  global dllmain
  export getArguments

dllmain:
  mov eax,1
  ret 12

getArguments:
  push ebp
  mov ebp,esp

  push 0x40   ; PAGE_EXECUTE_READWRITE
  push 0x3000 ; MEM_COMMIT | MEM_RESERVE
  push 8      ; size
  push 0      ; lpAddress
  call VirtualAlloc
  mov dword [eax],1   ; index 0
  mov dword [eax+4],2 ; index 1

  mov esp,ebp
  pop ebp
  ret

build.fil:

/entry:dllmain 
/dll
Kernel32.dll

getArguments.obj

build.bat:

nasm -f win32 getArguments.asm
golink @build.fil

c++:

#include <iostream>
#include <Windows.h>

typedef int* (CALLBACK* f_t)();
f_t f;

int main()
{
  HMODULE mydll = LoadLibraryA("getArguments.dll");
  if (mydll == NULL) return 0;

  f = (f_t)GetProcAddress(mydll, "getArguments");
  int* r = f();

  printf("%d\n", r[0]); // 1
  printf("%d\n", r[1]); // 2

  return 0;
}

See alse:

posted @ 2020-12-28 22:10  Ajanuw  阅读(118)  评论(0编辑  收藏  举报