nasm aat函数 x86
xxx.asm:
%define p1 ebp+8
%define p2 ebp+12
%define p3 ebp+16
section .text
global dllmain
dllmain:
mov eax,1
ret 12
aat:
push ebp
mov ebp,esp
; 函数必须保留所有寄存器,但eax,ecx和edx除外
; esp则必须根据调用约定进行更新
mov ecx,[p1] ; array ptr
mov edx,[p2] ; index
mov eax,[p3] ; size
mul edx ; eax=eax*edx
lea eax,[ecx+eax]
mov esp,ebp
pop ebp
ret 12
c++:
#include <iostream>
#include <Windows.h>
typedef PVOID (CALLBACK* aat_t)(PVOID pArray, size_t index, size_t size);
aat_t aat;
struct Player
{
size_t id;
DWORD hp;
DWORD mp;
};
int main()
{
HMODULE myDLL = LoadLibraryA("xxx.dll");
aat = (aat_t)GetProcAddress(myDLL, "aat");
int a[] = { 1,2,3 };
printf("%d\n", *(int*)aat(a, 2, sizeof(int))); // 3
Player b[] = {
Player{1, 10, 20},
Player{2, 50, 60},
Player{3, 90, 20},
};
Player* it = (Player*)aat(b, 0, sizeof(Player));
printf("[%d]: %d %d\n", it->id, it->hp, it->mp); // [1]: 10 20
it = (Player*)aat(b, 1, sizeof(Player));
printf("[%d]: %d %d\n", it->id, it->hp, it->mp); // [2]: 50 60
it = (Player*)aat(b, 2, sizeof(Player));
printf("[%d]: %d %d\n", it->id, it->hp, it->mp); // [3]: 90 20
return 0;
}