#MASM(x64)でdllを作る
Windows 10 Pro (64bit)
Visual Studio 2022 Community
dlltest.asm
comment *
ml64 /c /Fl dlltest.asm
link /dll /entry:DllMain dlltest
*
.code
DllMain proc
mov eax, 1
ret
DllMain endp
myadd proc export
mov rax, rcx
add rax, rdx
ret
myadd endp
mysub proc export
mov rax, rcx
sub rax, rdx
ret
mysub endp
end
dev.bat
@echo off
path %path%;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x64
path %path%;C:\Windows\Microsoft.NET\Framework64\v4.0.30319
cmd
##C#でテスト
x64版のcsc.exeにpathを通す。
dlltest.cs
// csc dlltest.cs
using System;
using System.Runtime.InteropServices;
class Program {
[DllImport("dlltest.dll")]
static extern int myadd(int a, int b);
[DllImport("dlltest.dll")]
static extern int mysub(int a, int b);
static void Main() {
Console.WriteLine("add = {0}", myadd(1, 2));
Console.WriteLine("sub = {0}", mysub(1, 2));
}
}
#CPUIDサンプル
cpuid.asm
comment *
ml64 /c /Fl cpuid.asm
link /dll /entry:DllMain cpuid
*
.code
DllMain proc
mov eax, 1
ret
DllMain endp
getcpuid proc export
push rbx
mov r8, rcx
mov eax, edx
cpuid
mov [r8 ], eax
mov [r8+ 4], ebx
mov [r8+ 8], ecx
mov [r8+12], edx
pop rbx
ret
getcpuid endp
end
cpuid.cs
// csc cpuid.cs
using System;
using System.Runtime.InteropServices;
using System.Text;
class Program {
[DllImport("cpuid.dll")]
static extern void getcpuid(byte[] p, uint eax);
static void Main() {
var ascii = new ASCIIEncoding();
var b = new byte[16];
for (uint i = 0; i < 3; i++) {
getcpuid(b, 0x80000002 + i);
Console.WriteLine("[{0}]", ascii.GetString(b));
}
}
}
出力例
C:\Projects\MASM\cpuid>cpuid
[Intel(R) Celeron]
[(R) N4000 CPU @ ]
[1.10GHz ]
#参考
https://qiita.com/Stosstruppe/items/f0be3a9cededf3e8a62a